authorRepository = new InMemoryAuthorRepository(); $this->followRepository = new InMemoryFollowRepository(); $this->countFollowersQueryHandler = new CountFollowersQueryHandler($this->authorRepository, $this->followRepository); } /** @test */ public function givenFollowersCountForANonExistingAuthorWhenCountIsRequestedThenAnExceptionShouldBeThrown(): void { $this->expectException(AuthorDoesNotExist::class); $authorId = AuthorTestDataBuilder::anAuthorIdentity()->id; ($this->countFollowersQueryHandler)( new CountFollowersQuery($authorId) ); } /** @test */ public function givenFollowersCountWhenCountIsRequestedThenItShouldReturnTheTotalNumberOfFollowers(): void { $author = AuthorTestDataBuilder::anAuthor()->build(); $follower1 = AuthorTestDataBuilder::anAuthor()->build(); $follower2 = AuthorTestDataBuilder::anAuthor()->build(); $follower3 = AuthorTestDataBuilder::anAuthor()->build(); $this->authorRepository->add($author); $this->authorRepository->add($follower1); $this->authorRepository->add($follower2); $this->authorRepository->add($follower3); $authorId = $author->authorId()->id; $followCommandHandler = new FollowCommandHandler($this->authorRepository, $this->followRepository); ($followCommandHandler)(new FollowCommand($follower1->authorId()->id, $authorId)); ($followCommandHandler)(new FollowCommand($follower2->authorId()->id, $authorId)); ($followCommandHandler)(new FollowCommand($follower3->authorId()->id, $authorId)); $totalNumberOfFollowers = ($this->countFollowersQueryHandler)( new CountFollowersQuery($authorId) ); $this->assertSame(3, $totalNumberOfFollowers); } }