From 2ab82dbfbfe9e09e1aa5d344870e428997dc65cf Mon Sep 17 00:00:00 2001 From: Dominik Liebler Date: Thu, 14 Jun 2018 17:45:45 +0200 Subject: [PATCH] Repository is supposed to generate IDs --- More/Repository/Domain/Post.php | 20 +++++---- More/Repository/MemoryStorage.php | 14 ++++--- More/Repository/PostRepository.php | 18 ++++---- More/Repository/Storage.php | 4 +- More/Repository/Tests/PostRepositoryTest.php | 44 ++++++++++++++++++++ More/Repository/Tests/RepositoryTest.php | 22 ---------- 6 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 More/Repository/Tests/PostRepositoryTest.php delete mode 100644 More/Repository/Tests/RepositoryTest.php diff --git a/More/Repository/Domain/Post.php b/More/Repository/Domain/Post.php index 779e938..25cc604 100644 --- a/More/Repository/Domain/Post.php +++ b/More/Repository/Domain/Post.php @@ -5,7 +5,7 @@ namespace DesignPatterns\More\Repository\Domain; class Post { /** - * @var int|null + * @var int */ private $id; @@ -19,6 +19,15 @@ class Post */ private $text; + public static function draft(int $id, string $title, string $text): Post + { + return new self( + $id, + $title, + $text + ); + } + public static function fromState(array $state): Post { return new self( @@ -29,22 +38,17 @@ class Post } /** - * @param int|null $id + * @param int $id * @param string $text * @param string $title */ - public function __construct($id, string $title, string $text) + private function __construct(int $id, string $title, string $text) { $this->id = $id; $this->text = $text; $this->title = $title; } - public function setId(int $id) - { - $this->id = $id; - } - public function getId(): int { return $this->id; diff --git a/More/Repository/MemoryStorage.php b/More/Repository/MemoryStorage.php index d6173c9..6463b21 100644 --- a/More/Repository/MemoryStorage.php +++ b/More/Repository/MemoryStorage.php @@ -14,20 +14,22 @@ class MemoryStorage implements Storage */ private $lastId = 0; - public function persist(array $data): int + public function generateId(): int { $this->lastId++; - $data['id'] = $this->lastId; - $this->data[$this->lastId] = $data; - return $this->lastId; } + public function persist(array $data) + { + $this->data[$this->lastId] = $data; + } + public function retrieve(int $id): array { if (!isset($this->data[$id])) { - throw new \OutOfRangeException(sprintf('No data found for ID %d', $id)); + throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id)); } return $this->data[$id]; @@ -36,7 +38,7 @@ class MemoryStorage implements Storage public function delete(int $id) { if (!isset($this->data[$id])) { - throw new \OutOfRangeException(sprintf('No data found for ID %d', $id)); + throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id)); } unset($this->data[$id]); diff --git a/More/Repository/PostRepository.php b/More/Repository/PostRepository.php index 6a9d450..e559492 100644 --- a/More/Repository/PostRepository.php +++ b/More/Repository/PostRepository.php @@ -25,12 +25,17 @@ class PostRepository $this->persistence = $persistence; } + public function generateId(): int + { + return $this->persistence->generateId(); + } + public function findById(int $id): Post { - $arrayData = $this->persistence->retrieve($id); - - if (is_null($arrayData)) { - throw new \InvalidArgumentException(sprintf('Post with ID %d does not exist', $id)); + try { + $arrayData = $this->persistence->retrieve($id); + } catch (\OutOfBoundsException $e) { + throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id), 0, $e); } return Post::fromState($arrayData); @@ -38,11 +43,10 @@ class PostRepository public function save(Post $post) { - $id = $this->persistence->persist([ + $this->persistence->persist([ + 'id' => $post->getId(), 'text' => $post->getText(), 'title' => $post->getTitle(), ]); - - $post->setId($id); } } diff --git a/More/Repository/Storage.php b/More/Repository/Storage.php index befe1ab..1bbd1ed 100644 --- a/More/Repository/Storage.php +++ b/More/Repository/Storage.php @@ -4,7 +4,9 @@ namespace DesignPatterns\More\Repository; interface Storage { - public function persist(array $data): int; + public function generateId(): int; + + public function persist(array $data); public function retrieve(int $id): array; diff --git a/More/Repository/Tests/PostRepositoryTest.php b/More/Repository/Tests/PostRepositoryTest.php new file mode 100644 index 0000000..b1b85c3 --- /dev/null +++ b/More/Repository/Tests/PostRepositoryTest.php @@ -0,0 +1,44 @@ +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()); + } +} diff --git a/More/Repository/Tests/RepositoryTest.php b/More/Repository/Tests/RepositoryTest.php deleted file mode 100644 index 4c3fa53..0000000 --- a/More/Repository/Tests/RepositoryTest.php +++ /dev/null @@ -1,22 +0,0 @@ -save($post); - - $this->assertEquals(1, $post->getId()); - $this->assertEquals($post->getId(), $repository->findById(1)->getId()); - } -}