More Related Content Similar to Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need (20) More from Kacper Gunia (8) Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need14. ‘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
23. 4 Rules of Simple Design
1. Passes its tests
2. Minimizes duplication
3. Maximizes clarity
4. Has fewer elements
46. class
CustomerRepositoryTest
extends
PHPUnit_Framework_TestCase
{
function
testFindCustomerById()
{
$customerRepository
=
new
CustomerRepository;
!
$customer
=
$customerRepository-‐>findById(5);
$this-‐>assertInstanceOf('Customer',
$customer);
}
}
55. What method can do?
return a value
modify state
delegate
throw an exception
65. class
CustomerRepositorySpec
extends
ObjectBehavior
{
function
it_loads_user_preferences()
{
$customer
=
$this-‐>findById(5);
!
$customer-‐>shouldBeAnInstanceOf('Customer');
}
}
88. function
let(EntityManager
$entityManager)
{
$this-‐>beConstructedWith($entityManager);
}
!
function
it_returns_customer_by_id()
{
$customer
=
$this-‐>findById(5);
!
$customer-‐>shouldBeAnInstanceOf('Customer');
}
}
89. function
let(EntityManager
$entityManager)
{
$this-‐>beConstructedWith($entityManager);
}
!
function
it_returns_customer_by_id()
{
$customer
=
$this-‐>findById(5);
!
$customer-‐>shouldBeAnInstanceOf('Customer');
}
}
94. 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)
{
$logger-‐>debug('DB
queried')
-‐>shouldBeCalled();
!
$this-‐>findById(5);
}
97. 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(Logger
$logger)
{
$this-‐>beConstructedWith($logger);
}
!
function
it_returns_customer_by_id(Logger
$logger)
{
$this-‐>findById(5);
!
$logger-‐>debug('DB
queried')
-‐>shouldHaveBeenCalled();
}
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);
}
103. 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);
}
104. 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);
}
109. 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);
}
110. function
it_returns_user_from_token(
SecurityContext
$securityContext,
TokenInterface
$token,
User
$user)
{
$securityContext-‐>getToken()-‐>willReturn($token);
$token-‐>getUser()-‐>willReturn($user);
!
$this-‐>getUser()-‐>shouldReturn($user);
}
!
111. public
function
__construct(SecurityContext
$securityContext){
$this-‐>securityContext
=
$securityContext;
}
!
public
function
getUser()
{
$token
=
$this-‐>securityContext-‐>getToken();
if
($token
instanceof
TokenInterface)
{
return
$token-‐>getUser();
}
!
return
null;
}
112. 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);
}
113. function
it_loads_user_preferences(
GetResponseEvent
$event,
DomainSecurityContext
$securityContext,
User
$user)
{
$securityContext-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type(‘Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
115. public
function
__construct(SecurityContext
$securityContext){
$this-‐>securityContext
=
$securityContext;
}
!
public
function
getUser()
{
$token
=
$this-‐>securityContext-‐>getToken();
if
($token
instanceof
TokenInterface)
{
return
$token-‐>getUser();
}
!
return
null;
}
117. function
it_loads_user_preferences(
GetResponseEvent
$event,
DomainSecurityContext
$securityContext,
User
$user)
{
$securityContext-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type(‘Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}
118. function
it_loads_user_preferences(
GetResponseEvent
$event,
DomainSecurityContextInterface
$securityContext,
User
$user)
{
$securityContext-‐>getUser()-‐>willReturn($user);
!
$user-‐>setPreferences(Argument::type(‘Preferences'))
-‐>shouldBeCalled();
!
$this-‐>handle($event);
}