From a90392861e7cac22673d5de1005e4ee6edef930b Mon Sep 17 00:00:00 2001 From: LVoogd Date: Sun, 18 Feb 2024 12:09:30 +0100 Subject: [PATCH] Add `findOrThrow` method to article repository --- .../Feed/Repository/InMemoryArticleRepository.php | 7 +++++++ src/Feed/Domain/Article/ArticleRepository.php | 6 ++++++ .../Article/Exception/ArticleNotFoundException.php | 13 +++++++++++++ .../Doctrine/Article/DoctrineArticleRepository.php | 6 ++++++ .../Article/DoctrineArticleRepositoryTest.php | 14 ++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 src/Feed/Domain/Article/Exception/ArticleNotFoundException.php diff --git a/src-dev/Feed/Repository/InMemoryArticleRepository.php b/src-dev/Feed/Repository/InMemoryArticleRepository.php index 2bb293d..34607ef 100644 --- a/src-dev/Feed/Repository/InMemoryArticleRepository.php +++ b/src-dev/Feed/Repository/InMemoryArticleRepository.php @@ -5,6 +5,7 @@ use App\Feed\Domain\Article\Article; use App\Feed\Domain\Article\ArticleId; use App\Feed\Domain\Article\ArticleRepository; +use App\Feed\Domain\Article\Exception\ArticleNotFoundException; use App\Feed\Domain\Article\Url\Url; final class InMemoryArticleRepository implements ArticleRepository @@ -26,6 +27,12 @@ public function find(ArticleId $id): ?Article return $this->entities[(string) $id] ?? null; } + + public function findOrThrow(ArticleId $id): Article + { + return $this->find($id) ?? throw ArticleNotFoundException::withArticleId($id); + } + public function count(): int { return count($this->entities); diff --git a/src/Feed/Domain/Article/ArticleRepository.php b/src/Feed/Domain/Article/ArticleRepository.php index 243e460..83f2130 100644 --- a/src/Feed/Domain/Article/ArticleRepository.php +++ b/src/Feed/Domain/Article/ArticleRepository.php @@ -2,6 +2,7 @@ namespace App\Feed\Domain\Article; +use App\Feed\Domain\Article\Exception\ArticleNotFoundException; use App\Feed\Domain\Article\Url\Url; interface ArticleRepository @@ -10,6 +11,11 @@ public function save(Article ...$articles): void; public function find(ArticleId $id): ?Article; + /** + * @throws ArticleNotFoundException + */ + public function findOrThrow(ArticleId $id): Article; + public function count(): int; /** diff --git a/src/Feed/Domain/Article/Exception/ArticleNotFoundException.php b/src/Feed/Domain/Article/Exception/ArticleNotFoundException.php new file mode 100644 index 0000000..6150bb8 --- /dev/null +++ b/src/Feed/Domain/Article/Exception/ArticleNotFoundException.php @@ -0,0 +1,13 @@ +findWithoutLocking($id); } + public function findOrThrow(ArticleId $id): Article + { + return $this->find($id) ?? throw ArticleNotFoundException::withArticleId($id); + } + public function count(): int { $count = $this->createQueryBuilder('a') diff --git a/tests/Functional/Feed/Infrastructure/Persistence/Doctrine/Article/DoctrineArticleRepositoryTest.php b/tests/Functional/Feed/Infrastructure/Persistence/Doctrine/Article/DoctrineArticleRepositoryTest.php index 9cf487a..d95d8e6 100644 --- a/tests/Functional/Feed/Infrastructure/Persistence/Doctrine/Article/DoctrineArticleRepositoryTest.php +++ b/tests/Functional/Feed/Infrastructure/Persistence/Doctrine/Article/DoctrineArticleRepositoryTest.php @@ -2,6 +2,8 @@ namespace Functional\Feed\Infrastructure\Persistence\Doctrine\Article; +use App\Feed\Domain\Article\ArticleId; +use App\Feed\Domain\Article\Exception\ArticleNotFoundException; use App\Feed\Infrastructure\Persistence\Doctrine\Article\DoctrineArticleRepository; use DateTime; use Dev\Feed\Factory\ArticleFactory; @@ -102,4 +104,16 @@ public function it_should_count_the_articles(): void // Assert self::assertSame(3, $count); } + + /** + * @test + */ + public function it_should_throw_when_trying_to_find_a_non_existing_article_id(): void + { + // Assert + self::expectException(ArticleNotFoundException::class); + + // Act + $this->repository->findOrThrow(new ArticleId('ddb01803-ef13-4195-bed1-3320a6b443ba')); + } }