mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 04:00:18 +02:00
Repository is supposed to generate IDs
This commit is contained in:
44
More/Repository/Tests/PostRepositoryTest.php
Normal file
44
More/Repository/Tests/PostRepositoryTest.php
Normal 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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user