Testing Repositories

In order to be sure that the Repository will work in production, we'll need to test its implementation. To do this, we have to test the boundaries of the system, making sure that our expectations are correct.

In the case of a Doctrine test, the setup will be a little bit more sophisticated:

use DoctrineDBALTypesType;
use DoctrineORMEntityManager;
use DoctrineORMTools;
use DomainModelPost;

class DoctrinePostRepositoryTest extends PHPUnit_Framework_TestCase
{
private $postRepository;

public function setUp()
{
$this->postRepository = $this->createPostRepository();
}

private function createPostRepository()
{
$this->addCustomTypes();
$em = $this->initEntityManager();
$this->initSchema($em);

return new PrecociousDoctrinePostRepository($em);
}

private function addCustomTypes()
{
if (!Type::hasType('post_id')) {
Type::addType(
'post_id',
'InfrastructurePersistenceDoctrineTypesPostIdType'
);
}

if (!Type::hasType('body')) {
Type::addType(
'body',
'InfrastructurePersistenceDoctrineTypesBodyType'
);
}
}

protected function initEntityManager()
{
return EntityManager::create(
['url' => 'sqlite:///:memory:'],
ToolsSetup::createXMLMetadataConfiguration(
['/Path/To/Infrastructure/Persistence/Doctrine/Mapping'],
$devMode = true
)
);
}

private function initSchema(EntityManager $em)
{
$tool = new ToolsSchemaTool($em);
$tool->createSchema([
$em->getClassMetadata('DomainModelPost')
]);
}

// ...
}

class PrecociousDoctrinePostRepository extends DoctrinePostRepository
{
public function persist(Post $aPost)
{
parent::persist($aPost);

$this->em->flush();
}

public function remove(Post $aPost)
{
parent::remove($aPost);

$this->em->flush();
}
}

Once we have this environment set up, we can continue to test the Repository's behavior:

class DoctrinePostRepositoryTest extends PHPUnit_Framework_TestCase
{
// ...

/**
* @test
*/
public function itShouldRemovePost()
{
$post = $this->persistPost('irrelevant body');

$this->postRepository->remove($post);

$this->assertPostExist($post->id());
}

private function assertPostExist($id)
{
$result = $this->postRepository->postOfId($id);
$this->assertNull($result);
}

private function persistPost(
$body,
DateTimeImmutable $createdAt = null
) {
$this->postRepository->add(
$post = new Post(
$this->postRepository->nextIdentity(),
new Body($body),
$createdAt
)
);

return $post;
}
}

Following our earlier assertion, if we save a Post, we expect to find it in the exact same state.

Now we can move on to test finding the latest posts by specifying a given date:

class DoctrinePostRepositoryTest extends PHPUnit_Framework_TestCase
{
// ...

/**
* @test
*/
public function itShouldFetchLatestPosts()
{
$this->persistPost(
'a year ago', new DateTimeImmutable('-1 year')
);
$this->persistPost(
'a month ago', new DateTimeImmutable('-1 month')
);
$this->persistPost(
'few hours ago', new DateTimeImmutable('-3 hours')
);
$this->persistPost(
'few minutes ago', new DateTimeImmutable('-2 minutes')
);

$posts = $this->postRepository->latestPosts(
new DateTimeImmutable('-24 hours')
);

$this->assertCount(2, $posts);
$this->assertEquals(
'few hours ago', $posts[0]->body()->content()
);
$this->assertEquals(
'few minutes ago', $posts[1]->body()->content()
);
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.119.114