Last active
March 15, 2019 20:34
-
-
Save ciaranmcnulty/a23e90006f2b9338771b7299c362c235 to your computer and use it in GitHub Desktop.
Contract tests example
This file contains 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 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 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 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 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(/* ... */, '[email protected]'); | |
$this->userRepository->create($user); | |
$this->assertEquals($this->userRepository->findByEmail('[email protected]'); | |
} | |
function testItReturnsNullWhenNoUser() | |
{ | |
$this->assertNull($this->userRepository->findByEmail('[email protected]')); | |
} | |
function testItDoesNotAllowDuplicateEmails() | |
{ | |
$user = new User(/* ... */, '[email protected]'); | |
$this->userRepository->create($user); | |
$this->setExpectedException(DuplicateUserException::class); | |
$user2 = new User(/* ... */, '[email protected]'); | |
$this->userRepository->create($user2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment