Más contenido relacionado La actualidad más candente (20) Similar a PHPSpec - the only Design Tool you need - 4Developers (20) PHPSpec - the only Design Tool you need - 4Developers9. ‘The key in making great and
growable systems is much more to
design how its
modules communicate
rather than what their internal
properties and behaviors should be.’
Alan Kay
18. 4 Rules of Simple Design
1. Passes its tests
2. Minimizes duplication
3. Maximizes clarity
4. Has fewer elements
42. class
CustomerRepositoryTest
extends
PHPUnit_Framework_TestCase
{
function
testClassExists()
{
$customerRepository
=
new
CustomerRepository;
!
$customer
=
$customerRepository-‐>findById(5);
$this-‐>assertInstanceOf('Customer',
$customer);
}
}
52. What method can do?
return a value
modify state
delegate
throw an exception
63. class
CustomerRepositorySpec
extends
ObjectBehavior
{
function
it_loads_user_preferences()
{
$customer
=
$this-‐>findById(5);
!
$customer-‐>shouldBeAnInstanceOf('Customer');
}
}
86. function
let(EntityManager
$entityManager)
{
$this-‐>beConstructedWith($entityManager);
}
!
function
it_returns_customer_by_id()
{
$customer
=
$this-‐>findById(5);
!
$customer-‐>shouldBeAnInstanceOf('Customer');
}
}
87. function
let(EntityManager
$entityManager)
{
$this-‐>beConstructedWith($entityManager);
}
!
function
it_returns_customer_by_id()
{
$customer
=
$this-‐>findById(5);
!
$customer-‐>shouldBeAnInstanceOf('Customer');
}
}
92. function
let(Logger
$logger)
{
$this-‐>beConstructedWith($logger);
}
!
function
it_returns_customer_by_id(Logger
$logger)
{
$logger-‐>debug('DB
queried')
-‐>shouldBeCalled();
!
$this-‐>findById(5);
}
93. function
let(Logger
$logger)
{
$this-‐>beConstructedWith($logger);
}
!
function
it_returns_customer_by_id(Logger
$logger)
{
$logger-‐>debug('DB
queried')
-‐>shouldBeCalled();
!
$this-‐>findById(5);
}
95. function
let(Logger
$logger)
{
$this-‐>beConstructedWith($logger);
}
!
function
it_returns_customer_by_id(Logger
$logger)
{
$this-‐>findById(5);
!
$logger-‐>debug('DB
queried')
-‐>shouldHaveBeenCalled();
}
96. function
let(Logger
$logger)
{
$this-‐>beConstructedWith($logger);
}
!
function
it_returns_customer_by_id(Logger
$logger)
{
$this-‐>findById(5);
!
$logger-‐>debug('DB
queried')
-‐>shouldHaveBeenCalled();
}
98. function
let(SecurityContext
$securityContext)
{
$this-‐>beConstructedWith($securityContext);
}
!
function
it_loads_user_preferences(
GetResponseEvent
$event,
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type('Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
99. function
let(SecurityContext
$securityContext)
{
$this-‐>beConstructedWith($securityContext);
}
!
function
it_loads_user_preferences(
GetResponseEvent
$event,
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type('Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
100. function
let(SecurityContext
$securityContext)
{
$this-‐>beConstructedWith($securityContext);
}
!
function
it_loads_user_preferences(
GetResponseEvent
$event,
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type('Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
101. function
let(SecurityContext
$securityContext)
{
$this-‐>beConstructedWith($securityContext);
}
!
function
it_loads_user_preferences(
GetResponseEvent
$event,
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type('Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
102. function
let(SecurityContext
$securityContext)
{
$this-‐>beConstructedWith($securityContext);
}
!
function
it_loads_user_preferences(
GetResponseEvent
$event,
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type('Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
107. function
it_loads_user_preferences(
GetResponseEvent
$event,
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type('Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
108. function
it_returns_user_from_token(
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$this-‐>getUser()-‐>shouldRetun($user);
}
!
109. public
function
__construct(SecurityContext
$securityContext){
$this-‐>securityContext
=
$securityContext;
}
!
public
function
getUser()
{
$token
=
$this-‐>securityContext-‐>getToken();
if
($token
instanceof
TokenInterface)
{
return
$token-‐>getUser();
}
!
return
null;
}
110. function
it_loads_user_preferences(
GetResponseEvent
$event,
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type('Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
111. function
it_loads_user_preferences(
GetResponseEvent
$event,
DomainSecurityContext
$securityContext,
User
$user)
{
$securityContext-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type(‘Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
113. public
function
__construct(SecurityContext
$securityContext){
$this-‐>securityContext
=
$securityContext;
}
!
public
function
getUser()
{
$token
=
$this-‐>securityContext-‐>getToken();
if
($token
instanceof
TokenInterface)
{
return
$token-‐>getUser();
}
!
return
null;
}
115. function
it_loads_user_preferences(
GetResponseEvent
$event,
DomainSecurityContext
$securityContext,
User
$user)
{
$securityContext-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type(‘Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
116. function
it_loads_user_preferences(
GetResponseEvent
$event,
DomainSecurityContextInterface
$securityContext,
User
$user)
{
$securityContext-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type(‘Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}