Last active
March 15, 2019 20:34
Contract tests example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DoctrineUserRepositoryTest extends UserRepositoryTest | |
{ | |
public function setUp() | |
{ | |
// do something to bootstrap doctrine and set up DB fixtures | |
$this->userRepository = new DoctrineRepository(/* lots of dependencies */); | |
} | |
public function tearDown() | |
{ | |
// do something to clean up DB | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class InMemoryUserRepository implements UserRepository | |
{ | |
private $users = []; | |
public function create(User $user) : void | |
{ | |
if ($this->findByEmail($user->email())) { | |
throw new DuplicateUserException(); | |
} | |
$this->users[] = $user; | |
} | |
public function findByEmail(string $email) : ?User | |
{ | |
foreach ($users as $user) { | |
if ($user->email() == $email) { | |
return $user; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class InMemoryUserRepositoryTest extends UserRepositoryTest | |
{ | |
public function setUp() | |
{ | |
$this->userRepository = new InMemoryUserRepository(); | |
} | |
// don't put any tests here | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface UserRepository | |
{ | |
/** @throws DuplicateUserException */ | |
public function create(User $user) : void; | |
public function findByEmail(string $email) : ?User; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Contract tests for any well-behaved UserRepository */ | |
abstract class UserRepositoryTest extends TestCase | |
{ | |
/** @var UserRepository */ | |
protected $userRepository; | |
function testItStoresAUser() | |
{ | |
$user = new User(/* ... */, 'ciaran@crania.uk'); | |
$this->userRepository->create($user); | |
$this->assertEquals($this->userRepository->findByEmail('ciaran@crania.uk'); | |
} | |
function testItReturnsNullWhenNoUser() | |
{ | |
$this->assertNull($this->userRepository->findByEmail('nobody@doesNotExist.com')); | |
} | |
function testItDoesNotAllowDuplicateEmails() | |
{ | |
$user = new User(/* ... */, 'ciaran@crania.uk'); | |
$this->userRepository->create($user); | |
$this->setExpectedException(DuplicateUserException::class); | |
$user2 = new User(/* ... */, 'ciaran@crania.uk'); | |
$this->userRepository->create($user2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment