mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-06-28 20:23:34 +02:00
50 lines
1.4 KiB
PHP
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());
|
|
}
|
|
}
|