Repository is supposed to generate IDs

This commit is contained in:
Dominik Liebler
2018-06-14 17:45:45 +02:00
parent 4cb353fe3e
commit 2ab82dbfbf
6 changed files with 78 additions and 44 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace DesignPatterns\More\Repository\Tests;
use DesignPatterns\More\Repository\MemoryStorage;
use DesignPatterns\More\Repository\Domain\Post;
use DesignPatterns\More\Repository\PostRepository;
use PHPUnit\Framework\TestCase;
class PostRepositoryTest extends TestCase
{
/**
* @var PostRepository
*/
private $repository;
protected function setUp()
{
$this->repository = new PostRepository(new MemoryStorage());
}
public function testCanGenerateId()
{
$this->assertEquals(1, $this->repository->generateId());
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Post with id 42 does not exist
*/
public function testThrowsExceptionWhenTryingToFindPostWhichDoesNotExist()
{
$this->repository->findById(42);
}
public function testCanPersistPostDraft()
{
$postId = $this->repository->generateId();
$post = Post::draft($postId, 'Repository Pattern', 'Design Patterns PHP');
$this->repository->save($post);
$this->assertEquals($postId, $this->repository->findById($postId)->getId());
}
}

View File

@@ -1,22 +0,0 @@
<?php
namespace DesignPatterns\More\Repository\Tests;
use DesignPatterns\More\Repository\MemoryStorage;
use DesignPatterns\More\Repository\Domain\Post;
use DesignPatterns\More\Repository\PostRepository;
use PHPUnit\Framework\TestCase;
class RepositoryTest extends TestCase
{
public function testCanPersistAndFindPost()
{
$repository = new PostRepository(new MemoryStorage());
$post = new Post(null, 'Repository Pattern', 'Design Patterns PHP');
$repository->save($post);
$this->assertEquals(1, $post->getId());
$this->assertEquals($post->getId(), $repository->findById(1)->getId());
}
}