cheepRepository = new InMemoryCheepRepository(); $this->authorRepository = new InMemoryAuthorRepository(); $this->eventBus = new InMemoryEventBus(); } /** * @test */ public function throwsExceptionWhenAuthorDoesNotExist(): void { $this->expectException(AuthorDoesNotExist::class); $this->expectExceptionMessage('Author "b547cf31-a0d2-4d26-aa77-8901fbdc0549" does not exist'); $authorId = 'b547cf31-a0d2-4d26-aa77-8901fbdc0549'; $cheepId = Uuid::uuid4()->toString(); $this->postNewCheep( $authorId, $cheepId, 'A message' ); } /** @test */ public function throwsExceptionWhenAuthorIdIsNotUuid(): void { $this->expectException(\InvalidArgumentException::class); $this->postNewCheep( 'not-an-uuid', Uuid::uuid4()->toString(), 'A message' ); } /** @test */ public function throwsExceptionWhenCheepIdIsNotUuid(): void { $this->expectException(\InvalidArgumentException::class); $this->postNewCheep( Uuid::uuid4()->toString(), 'not-an-uuid', 'A message' ); } /** @test */ public function throwsExceptionWhenCheepMessageIsEmpty(): void { $this->expectException(\InvalidArgumentException::class); $this->postNewCheep( Uuid::uuid4()->toString(), Uuid::uuid4()->toString(), '' ); } /** @test */ public function cheepIsPersistedSuccessfully(): void { $authorId = '3409a21d-83b3-471e-a4f1-cf6748af65d2'; $authorUsername = 'buenosvinos'; $authorEmail = 'carlos.buenosvinos@gmail.com'; $author = $this->buildSampleAuthor($authorId, $authorUsername, $authorEmail); $this->authorRepository->add($author); $cheepId = Uuid::uuid4()->toString(); $this->postNewCheep( $author->authorId()->id(), $cheepId, 'A message' ); $cheep = $this->cheepRepository->ofId(CheepId::fromString($cheepId)); $this->assertNotNull($cheep); $events = $this->eventBus->events(); /** @var CheepPosted $cheepPosted */ $cheepPosted = first($events); $this->assertCount(1, $events); $this->assertSame(CheepPosted::class, $cheepPosted::class); $this->assertSame($authorId, $cheepPosted->authorId()); $this->assertSame($cheepId, $cheepPosted->cheepId()); } private function postNewCheep( string $authorId, string $cheepId, string $message ): void { $this->eventBus->reset(); (new PostCheepCommandHandler( $this->authorRepository, $this->cheepRepository, $this->eventBus ))( PostCheepCommand::fromArray([ 'author_id' => $authorId, 'cheep_id' => $cheepId, 'message' => $message, ]) ); } private function buildSampleAuthor(string $authorId, string $authorUsername, string $authorEmail): Author { return Author::signUp( AuthorId::fromString($authorId), UserName::pick($authorUsername), EmailAddress::from($authorEmail) ); } } //end-snippet