Files
DesignPatternsPHP/More/Repository/Tests/PostRepositoryTest.php
Dominik Liebler a73c253ffc update to phpunit8
2019-08-17 23:05:15 +02:00

50 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace DesignPatterns\More\Repository\Tests;
use DesignPatterns\More\Repository\Domain\PostId;
use DesignPatterns\More\Repository\Domain\PostStatus;
use DesignPatterns\More\Repository\InMemoryPersistence;
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(): void
{
$this->repository = new PostRepository(new InMemoryPersistence());
}
public function testCanGenerateId()
{
$this->assertEquals(1, $this->repository->generateId()->toInt());
}
public function testThrowsExceptionWhenTryingToFindPostWhichDoesNotExist()
{
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Post with id 42 does not exist');
$this->repository->findById(PostId::fromInt(42));
}
public function testCanPersistPostDraft()
{
$postId = $this->repository->generateId();
$post = Post::draft($postId, 'Repository Pattern', 'Design Patterns PHP');
$this->repository->save($post);
$this->repository->findById($postId);
$this->assertEquals($postId, $this->repository->findById($postId)->getId());
$this->assertEquals(PostStatus::STATE_DRAFT, $post->getStatus()->toString());
}
}