From 643c6db0808c009eec235d5ebe6d9452b519ab00 Mon Sep 17 00:00:00 2001 From: Carlos Buenosvinos Date: Mon, 14 Feb 2022 17:51:57 +0100 Subject: [PATCH] Chapter5: Adding Tests for Redis thin layer --- .../DomainModel/Follow/FollowRepository.php | 6 +++ .../Follow/DoctrineOrmFollowRepository.php | 7 ++++ .../Follow/InMemoryFollowRepository.php | 6 +++ .../CountFollowersQueryHandler.php | 4 +- .../DomainModel/Follow/FollowRepository.php | 12 ------ .../DoctrineOrmFollowRepository.php | 23 ----------- .../CountFollowersQueryHandlerTest.php | 39 +++++++++---------- 7 files changed, 40 insertions(+), 57 deletions(-) delete mode 100644 src/Cheeper/Chapter5/DomainModel/Follow/FollowRepository.php delete mode 100644 src/Cheeper/Chapter5/Infrastructure/Persistence/DoctrineOrmFollowRepository.php diff --git a/src/Cheeper/Chapter4/DomainModel/Follow/FollowRepository.php b/src/Cheeper/Chapter4/DomainModel/Follow/FollowRepository.php index 72d50cd..dfc6909 100644 --- a/src/Cheeper/Chapter4/DomainModel/Follow/FollowRepository.php +++ b/src/Cheeper/Chapter4/DomainModel/Follow/FollowRepository.php @@ -10,9 +10,15 @@ use Cheeper\AllChapters\DomainModel\Author\AuthorId; interface FollowRepository { public function numberOfFollowersFor(AuthorId $authorId): int; + public function add(Follow $follow): void; + public function ofFromAuthorIdAndToAuthorId(AuthorId $fromAuthorId, AuthorId $toAuthorId): ?Follow; + /** @return Follow[] */ public function toAuthorId(AuthorId $authorId): array; + + /** @return Follow[] */ + public function fromAuthorId(AuthorId $authorId): array; } //end-snippet diff --git a/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/DoctrineOrmFollowRepository.php b/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/DoctrineOrmFollowRepository.php index a3232c5..296a2c7 100644 --- a/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/DoctrineOrmFollowRepository.php +++ b/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/DoctrineOrmFollowRepository.php @@ -42,5 +42,12 @@ final class DoctrineOrmFollowRepository implements FollowRepository ->getRepository(Follow::class) ->findBy(['toAuthorId' => $authorId]); } + + public function fromAuthorId(AuthorId $authorId): array + { + return $this->em + ->getRepository(Follow::class) + ->findBy(['fromAuthorId' => $authorId]); + } } //end-snippet diff --git a/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/InMemoryFollowRepository.php b/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/InMemoryFollowRepository.php index 092a30c..fe0e559 100644 --- a/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/InMemoryFollowRepository.php +++ b/src/Cheeper/Chapter4/Infrastructure/DomainModel/Follow/InMemoryFollowRepository.php @@ -8,6 +8,7 @@ use Cheeper\AllChapters\DomainModel\Author\AuthorId; use Cheeper\AllChapters\DomainModel\Follow\FollowId; use Cheeper\Chapter4\DomainModel\Follow\Follow; use Cheeper\Chapter4\DomainModel\Follow\FollowRepository; +use Cheeper\Chapter5\DomainModel\Follow\NumberOfFollowers; use function Functional\head; use function Functional\reduce_left; use function Functional\select; @@ -63,4 +64,9 @@ final class InMemoryFollowRepository implements FollowRepository { return select($this->collection, fn (Follow $f): bool => $f->toAuthorId()->equals($authorId)); } + + public function fromAuthorId(AuthorId $authorId): array + { + return select($this->collection, fn (Follow $f): bool => $f->fromAuthorId()->equals($authorId)); + } } diff --git a/src/Cheeper/Chapter5/Application/Author/Query/CountFollowersQueryHandler/WithRepositoriesAccess/CountFollowersQueryHandler.php b/src/Cheeper/Chapter5/Application/Author/Query/CountFollowersQueryHandler/WithRepositoriesAccess/CountFollowersQueryHandler.php index 9c0528c..dec18b1 100644 --- a/src/Cheeper/Chapter5/Application/Author/Query/CountFollowersQueryHandler/WithRepositoriesAccess/CountFollowersQueryHandler.php +++ b/src/Cheeper/Chapter5/Application/Author/Query/CountFollowersQueryHandler/WithRepositoriesAccess/CountFollowersQueryHandler.php @@ -9,7 +9,7 @@ use Cheeper\AllChapters\DomainModel\Author\AuthorId; use Cheeper\Chapter4\DomainModel\Author\AuthorRepository; use Cheeper\Chapter5\Application\Author\Query\CountFollowersQueryHandler\CountFollowersQuery; use Cheeper\Chapter5\Application\Author\Query\CountFollowersQueryHandler\CountFollowersResponse; -use Cheeper\Chapter5\DomainModel\Follow\FollowRepository; +use Cheeper\Chapter4\DomainModel\Follow\FollowRepository; //snippet count-followers-handler final class CountFollowersQueryHandler @@ -29,7 +29,7 @@ final class CountFollowersQueryHandler throw AuthorDoesNotExist::withAuthorIdOf($authorId); } - $followersCount = $this->followRepository->ofAuthorId($authorId)?->followers() ?? 0; + $followersCount = count($this->followRepository->toAuthorId($authorId)); // Other option would be with a counter method in the Repository // $followersCount = $this->followers->countOfAuthorId($authorId)); diff --git a/src/Cheeper/Chapter5/DomainModel/Follow/FollowRepository.php b/src/Cheeper/Chapter5/DomainModel/Follow/FollowRepository.php deleted file mode 100644 index e3bb5be..0000000 --- a/src/Cheeper/Chapter5/DomainModel/Follow/FollowRepository.php +++ /dev/null @@ -1,12 +0,0 @@ -expectException(AuthorDoesNotExist::class); $this->expectExceptionMessage('Author "3409a21d-83b3-471e-a4f1-cf6748af65d2" does not exist'); - $nonExistingAuthorId = '3409a21d-83b3-471e-a4f1-cf6748af65d2'; + $authorId = '3409a21d-83b3-471e-a4f1-cf6748af65d2'; $queryHandler = new CountFollowersQueryHandler( - $this->buildFollowRepositoryMockReturning(null), + $this->buildFollowRepositoryMockReturning([]), $this->buildAuthorRepositoryMockReturning(null) ); $queryHandler->__invoke( - CountFollowersQuery::ofAuthor($nonExistingAuthorId) + CountFollowersQuery::ofAuthor($authorId) ); } @@ -42,20 +40,13 @@ final class CountFollowersQueryHandlerTest extends TestCase { $authorId = '3409a21d-83b3-471e-a4f1-cf6748af65d2'; $authorUsername = 'buenosvinos'; + $authorEmail = 'carlos.buenosvinos@gmail.com'; $authorFollowers = 0; + $queryHandler = new CountFollowersQueryHandler( - $this->buildFollowRepositoryMockReturning( - new NumberOfFollowers( - Uuid::fromString($authorId), - 0 - ) - ), + $this->buildFollowRepositoryMockReturning([]), $this->buildAuthorRepositoryMockReturning( - Author::signUp( - AuthorId::fromString($authorId), - UserName::pick('buenosvinos'), - EmailAddress::from('carlos.buenosvinos@gmail.com') - ) + $this->buildSampleAuthor($authorId, $authorUsername, $authorEmail) ) ); @@ -72,7 +63,7 @@ final class CountFollowersQueryHandlerTest extends TestCase $this->assertEquals($expectedReponse, $actualResponse); } - private function buildAuthorRepositoryMockReturning($fakeReturn) { + private function buildAuthorRepositoryMockReturning(?Author $fakeReturn) { $mock = $this->createStub(AuthorRepository::class); $mock->method('ofId')->willReturn( @@ -82,10 +73,10 @@ final class CountFollowersQueryHandlerTest extends TestCase return $mock; } - private function buildFollowRepositoryMockReturning($fakeReturn) { + private function buildFollowRepositoryMockReturning(array $fakeReturn) { $mock = $this->createStub(FollowRepository::class); - $mock->method('ofAuthorId')->willReturn( + $mock->method('fromAuthorId')->willReturn( $fakeReturn ); @@ -93,4 +84,12 @@ final class CountFollowersQueryHandlerTest extends TestCase } // @TODO: What happen if the connection is not right? + private function buildSampleAuthor(string $authorId, string $authorUsername, string $authorEmail): Author + { + return Author::signUp( + AuthorId::fromString($authorId), + UserName::pick($authorUsername), + EmailAddress::from($authorEmail) + ); + } }