Marco Pivetta Foreword

This commit is contained in:
Carlos Buenosvinos
2022-01-31 08:06:00 +01:00
parent fb6b14ad85
commit fd17ed2191
9 changed files with 97043 additions and 92 deletions

96945
bin/phpunit.phar Normal file

File diff suppressed because one or more lines are too long

View File

@@ -76,9 +76,9 @@ final class SignUpCommandBuilder
return $this; return $this;
} }
public function build(): SignUp public function build(): SignUpCommand
{ {
return new SignUp( return new SignUpCommand(
$this->authorId, $this->authorId,
$this->userName, $this->userName,
$this->email, $this->email,

View File

@@ -2,20 +2,20 @@
declare(strict_types=1); declare(strict_types=1);
namespace Cheeper\Tests\AllChapters\Application\Command\Author; namespace Cheeper\Tests\Chapter4\Application\Author\Command;
use Cheeper\AllChapters\Application\Command\Author\FollowCommand;
use Cheeper\AllChapters\Application\Command\Author\FollowCommandHandler;
use Cheeper\AllChapters\DomainModel\Author\AuthorDoesNotExist; use Cheeper\AllChapters\DomainModel\Author\AuthorDoesNotExist;
use Cheeper\AllChapters\DomainModel\Author\AuthorId; use Cheeper\AllChapters\DomainModel\Author\AuthorId;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors; use Cheeper\Chapter4\Application\Author\Command\FollowCommand;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryFollows; use Cheeper\Chapter4\Application\Author\Command\FollowCommandHandler;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommandHandler;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommand;
use Cheeper\Tests\Helper\SendsCommands; use Cheeper\Tests\Helper\SendsCommands;
use DateTimeImmutable; use DateTimeImmutable;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
final class FollowHandlerTest extends TestCase final class FollowCommandHandlerTest extends TestCase
{ {
use SendsCommands; use SendsCommands;
@@ -48,6 +48,24 @@ final class FollowHandlerTest extends TestCase
$this->followAuthor('test', 'test2'); $this->followAuthor('test', 'test2');
} }
private function signUpAuthorWith(string $authorId, string $userName, string $email, string $name, string $biography, string $location, string $website, string $birthDate): void
{
(new SignUpCommandHandler(
$this->authors
))(
new SignUpCommand(
$authorId,
$userName,
$email,
$name,
$biography,
$location,
$website,
$birthDate
)
);
}
/** @test */ /** @test */
public function authorsCanFollowOtherAuthors(): void public function authorsCanFollowOtherAuthors(): void
{ {
@@ -82,4 +100,11 @@ final class FollowHandlerTest extends TestCase
$followers = $this->follows->numberOfFollowersFor(AuthorId::fromUuid($authorId)); $followers = $this->follows->numberOfFollowersFor(AuthorId::fromUuid($authorId));
$this->assertSame(1, $followers); $this->assertSame(1, $followers);
} }
private function followAuthor(string $followee, string $followed): void
{
(new FollowCommandHandler($this->authors, $this->follows))(
FollowCommand::anAuthor($followed, $followee)
);
}
} }

View File

@@ -2,14 +2,14 @@
declare(strict_types=1); declare(strict_types=1);
namespace Cheeper\Tests\AllChapters\Application\Command\Author; namespace Cheeper\Tests\Chapter4\Application\Author\Command;
use Cheeper\AllChapters\Application\Command\Author\SignUp as SignUpCommand; use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommand;
use Cheeper\AllChapters\Application\Command\Author\SignUpCommandBuilder; use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommandBuilder;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
final class SignUp { final class SignUpCommandBuilderMock {
//snippet builder-method //snippet builder-method
public static function builder( public static function builder(
string $authorId, string $authorId,
@@ -29,7 +29,7 @@ final class SignUpBuilderTest extends TestCase
{ {
//snippet basic-builder-usage //snippet basic-builder-usage
$authorId = Uuid::uuid4()->toString(); $authorId = Uuid::uuid4()->toString();
$command = SignUp::builder($authorId, 'johndoe', 'test@email.com')->build(); $command = SignUpCommandBuilderMock::builder($authorId, 'johndoe', 'test@email.com')->build();
//end-snippet //end-snippet
$this->assertInstanceOf(SignUpCommand::class, $command); $this->assertInstanceOf(SignUpCommand::class, $command);
@@ -39,7 +39,7 @@ final class SignUpBuilderTest extends TestCase
public function itBuildsOptionalFields(): void public function itBuildsOptionalFields(): void
{ {
//snippet sophisticated-builder-usage //snippet sophisticated-builder-usage
$command = SignUp::builder(Uuid::uuid4()->toString(), 'johndoe', 'test@email.com') $command = SignUpCommandBuilderMock::builder(Uuid::uuid4()->toString(), 'johndoe', 'test@email.com')
->name('John Doe') ->name('John Doe')
->website('https://johndoe.com') ->website('https://johndoe.com')
->biography('An example author') ->biography('An example author')

View File

@@ -2,19 +2,19 @@
declare(strict_types=1); declare(strict_types=1);
namespace Cheeper\Tests\AllChapters\Application\Command\Author\SignUpWithEvents; namespace Cheeper\Tests\Chapter4\Application\Author\Command\SignUpWithEvents;
use Cheeper\AllChapters\Application\Command\Author\SignUp;
use Cheeper\AllChapters\Application\Command\Author\SignUpWithEvents\SignUpHandler;
use Cheeper\Chapter6\Infrastructure\Application\Event\InMemoryEventBus;
use Cheeper\AllChapters\DomainModel\Author\AuthorAlreadyExists; use Cheeper\AllChapters\DomainModel\Author\AuthorAlreadyExists;
use Cheeper\AllChapters\DomainModel\Author\NewAuthorSigned; use Cheeper\AllChapters\DomainModel\Author\NewAuthorSigned;
use Cheeper\AllChapters\DomainModel\Author\UserName; use Cheeper\AllChapters\DomainModel\Author\UserName;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors; use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithEvents\SignUpCommandHandler;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommand;
use Cheeper\Chapter6\Infrastructure\Application\Event\InMemoryEventBus;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
final class SignUpHandlerTest extends TestCase final class SignUpCommandHandlerTest extends TestCase
{ {
/** @test */ /** @test */
public function givenAUserNameThatAlreadyBelongsToAnExistingUserWhenSignUpThenAnExceptionShouldBeThrown(): void public function givenAUserNameThatAlreadyBelongsToAnExistingUserWhenSignUpThenAnExceptionShouldBeThrown(): void
@@ -25,13 +25,13 @@ final class SignUpHandlerTest extends TestCase
$authors = new InMemoryAuthors(); $authors = new InMemoryAuthors();
$eventBus = new InMemoryEventBus(); $eventBus = new InMemoryEventBus();
$signUpHandler = new SignUpHandler( $signUpHandler = new SignUpCommandHandler(
$authors, $authors,
$eventBus $eventBus
); );
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
'johndoe', 'johndoe',
'johndoe@example.com', 'johndoe@example.com',
@@ -46,7 +46,7 @@ final class SignUpHandlerTest extends TestCase
$eventBus->reset(); $eventBus->reset();
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
'johndoe', 'johndoe',
'johndoe@example.com', 'johndoe@example.com',
@@ -64,7 +64,7 @@ final class SignUpHandlerTest extends TestCase
{ {
$authors = new InMemoryAuthors(); $authors = new InMemoryAuthors();
$eventBus = new InMemoryEventBus(); $eventBus = new InMemoryEventBus();
$signUpHandler = new SignUpHandler( $signUpHandler = new SignUpCommandHandler(
$authors, $authors,
$eventBus $eventBus
); );
@@ -72,7 +72,7 @@ final class SignUpHandlerTest extends TestCase
$userName = 'johndoe'; $userName = 'johndoe';
$email = 'johndoe@example.com'; $email = 'johndoe@example.com';
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
$userName, $userName,
$email, $email,
@@ -99,7 +99,7 @@ final class SignUpHandlerTest extends TestCase
{ {
$authors = new InMemoryAuthors(); $authors = new InMemoryAuthors();
$eventBus = new InMemoryEventBus(); $eventBus = new InMemoryEventBus();
$signUpHandler = new SignUpHandler( $signUpHandler = new SignUpCommandHandler(
$authors, $authors,
$eventBus $eventBus
); );
@@ -113,7 +113,7 @@ final class SignUpHandlerTest extends TestCase
$birthDate = (new \DateTimeImmutable())->format('Y-m-d'); $birthDate = (new \DateTimeImmutable())->format('Y-m-d');
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
$userName, $userName,
$email, $email,
@@ -148,7 +148,7 @@ final class SignUpHandlerTest extends TestCase
$authors = new InMemoryAuthors(); $authors = new InMemoryAuthors();
$eventBus = new InMemoryEventBus(); $eventBus = new InMemoryEventBus();
$signUpHandler = new SignUpHandler( $signUpHandler = new SignUpCommandHandler(
$authors, $authors,
$eventBus $eventBus
); );
@@ -162,7 +162,7 @@ final class SignUpHandlerTest extends TestCase
$birthDate = (new \DateTimeImmutable())->format('Y-m-d'); $birthDate = (new \DateTimeImmutable())->format('Y-m-d');
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
$userName, $userName,
$email, $email,
@@ -183,7 +183,7 @@ final class SignUpHandlerTest extends TestCase
$authors = new InMemoryAuthors(); $authors = new InMemoryAuthors();
$eventBus = new InMemoryEventBus(); $eventBus = new InMemoryEventBus();
$signUpHandler = new SignUpHandler( $signUpHandler = new SignUpCommandHandler(
$authors, $authors,
$eventBus $eventBus
); );
@@ -197,7 +197,7 @@ final class SignUpHandlerTest extends TestCase
$birthDate = (new \DateTimeImmutable())->format('Y-m-d'); $birthDate = (new \DateTimeImmutable())->format('Y-m-d');
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
$userName, $userName,
$email, $email,

View File

@@ -2,18 +2,18 @@
declare(strict_types=1); declare(strict_types=1);
namespace Cheeper\Tests\AllChapters\Application\Command\Author; namespace Cheeper\Tests\Chapter4\Application\Author\Command\SignUpWithoutEvents;
use Cheeper\AllChapters\Application\Command\Author\SignUp;
use Cheeper\AllChapters\Application\Command\Author\SignUpCommandHandler;
use Cheeper\AllChapters\DomainModel\Author\AuthorAlreadyExists; use Cheeper\AllChapters\DomainModel\Author\AuthorAlreadyExists;
use Cheeper\AllChapters\DomainModel\Author\Authors; use Cheeper\AllChapters\DomainModel\Author\Authors;
use Cheeper\AllChapters\DomainModel\Author\UserName; use Cheeper\AllChapters\DomainModel\Author\UserName;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors; use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommand;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommandHandler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
final class SignUpHandlerTest extends TestCase final class SignUpCommandHandlerTest extends TestCase
{ {
/** @test */ /** @test */
public function givenAUserNameThatAlreadyBelongsToAnExistingUserWhenSignUpThenAnExceptionShouldBeThrown(): void public function givenAUserNameThatAlreadyBelongsToAnExistingUserWhenSignUpThenAnExceptionShouldBeThrown(): void
@@ -27,7 +27,7 @@ final class SignUpHandlerTest extends TestCase
$signUpHandler = new SignUpCommandHandler($authors); $signUpHandler = new SignUpCommandHandler($authors);
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
'johndoe', 'johndoe',
'johndoe@example.com', 'johndoe@example.com',
@@ -41,7 +41,7 @@ final class SignUpHandlerTest extends TestCase
//end-snippet //end-snippet
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
'johndoe', 'johndoe',
'johndoe@example.com', 'johndoe@example.com',
@@ -65,7 +65,7 @@ final class SignUpHandlerTest extends TestCase
$email = 'johndoe@example.com'; $email = 'johndoe@example.com';
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
$userName, $userName,
$email $email
@@ -99,7 +99,7 @@ final class SignUpHandlerTest extends TestCase
$birthDate = (new \DateTimeImmutable())->format('Y-m-d'); $birthDate = (new \DateTimeImmutable())->format('Y-m-d');
$signUpHandler( $signUpHandler(
new SignUp( new SignUpCommand(
Uuid::uuid4()->toString(), Uuid::uuid4()->toString(),
$userName, $userName,
$email, $email,

View File

@@ -2,24 +2,20 @@
declare(strict_types=1); declare(strict_types=1);
namespace Cheeper\Tests\AllChapters\Application\Command\Cheep; namespace Cheeper\Tests\Chapter4\Application\Cheep\Command;
use Cheeper\AllChapters\Application\Command\Cheep\PostCheep;
use Cheeper\AllChapters\Application\Command\Cheep\PostCheepHandler;
use Cheeper\AllChapters\DomainModel\Author\AuthorDoesNotExist; use Cheeper\AllChapters\DomainModel\Author\AuthorDoesNotExist;
use Cheeper\AllChapters\DomainModel\Author\Authors;
use Cheeper\AllChapters\DomainModel\Cheep\CheepId; use Cheeper\AllChapters\DomainModel\Cheep\CheepId;
use Cheeper\AllChapters\DomainModel\Cheep\CheepPosted; use Cheeper\AllChapters\DomainModel\Cheep\CheepPosted;
use Cheeper\AllChapters\DomainModel\Cheep\Cheeps; use Cheeper\Chapter4\Application\Cheep\Command\PostCheepCommand;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors; use Cheeper\Chapter4\Application\Cheep\Command\PostCheepCommandHandler;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryCheeps;
use Cheeper\Tests\AllChapters\DomainModel\Author\AuthorTestDataBuilder; use Cheeper\Tests\AllChapters\DomainModel\Author\AuthorTestDataBuilder;
use Cheeper\Tests\Helper\SendsCommands; use Cheeper\Tests\Helper\SendsCommands;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
//snippet post-cheep-handler-test //snippet post-cheep-handler-test
final class PostCheepHandlerTest extends TestCase final class PostCheepCommandHandlerTest extends TestCase
{ {
use SendsCommands; use SendsCommands;
@@ -102,12 +98,12 @@ final class PostCheepHandlerTest extends TestCase
string $cheepId, string $cheepId,
string $message string $message
): void { ): void {
(new PostCheepHandler( (new PostCheepCommandHandler(
$this->authors, $this->authors,
$this->cheeps, $this->cheeps,
$this->eventBus $this->eventBus
))( ))(
PostCheep::fromArray([ PostCheepCommand::fromArray([
'author_id' => $authorId, 'author_id' => $authorId,
'cheep_id' => $cheepId, 'cheep_id' => $cheepId,
'message' => $message, 'message' => $message,

View File

@@ -2,12 +2,14 @@
declare(strict_types=1); declare(strict_types=1);
namespace Cheeper\Tests\AllChapters\Application\Command\Cheep; namespace Cheeper\Tests\Chapter4\Application\Cheep\Command;
use Cheeper\AllChapters\Application\Command\Cheep\UpdateCheepMessage;
use Cheeper\AllChapters\Application\Command\Cheep\UpdateCheepMessageHandler;
use Cheeper\AllChapters\DomainModel\Cheep\CheepDoesNotExist; use Cheeper\AllChapters\DomainModel\Cheep\CheepDoesNotExist;
use Cheeper\AllChapters\DomainModel\Cheep\CheepId; use Cheeper\AllChapters\DomainModel\Cheep\CheepId;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommand;
use Cheeper\Chapter4\Application\Author\Command\SignUpWithoutEvents\SignUpCommandHandler;
use Cheeper\Chapter4\Application\Cheep\Command\UpdateCheepMessageCommand;
use Cheeper\Chapter4\Application\Cheep\Command\UpdateCheepMessageCommandHandler;
use Cheeper\Tests\Helper\SendsCommands; use Cheeper\Tests\Helper\SendsCommands;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
@@ -52,12 +54,30 @@ final class UpdateCheepMessageHandlerTest extends TestCase
$this->assertSame("new cheep message", $cheep->cheepMessage()->message()); $this->assertSame("new cheep message", $cheep->cheepMessage()->message());
} }
private function signUpAuthorWith(string $authorId, string $userName, string $email, string $name, string $biography, string $location, string $website, string $birthDate): void
{
(new SignUpCommandHandler(
$this->authors
))(
new SignUpCommand(
$authorId,
$userName,
$email,
$name,
$biography,
$location,
$website,
$birthDate
)
);
}
private function updateCheepMessage(string $cheepId, string $message): void private function updateCheepMessage(string $cheepId, string $message): void
{ {
$updateCheepMessageHandler = new UpdateCheepMessageHandler($this->cheeps); $updateCheepMessageHandler = new UpdateCheepMessageCommandHandler($this->cheeps);
$updateCheepMessageHandler( $updateCheepMessageHandler(
new UpdateCheepMessage( new UpdateCheepMessageCommand(
$cheepId, $cheepId,
$message $message
) )

View File

@@ -5,24 +5,14 @@ declare(strict_types=1);
namespace Cheeper\Tests\Helper; namespace Cheeper\Tests\Helper;
use Cheeper\AllChapters\Application\Command\Author\FollowCommand; use Cheeper\Chapter4\Application\Cheep\Command\PostCheepCommand;
use Cheeper\AllChapters\Application\Command\Author\FollowCommandHandler; use Cheeper\Chapter4\Application\Cheep\Command\PostCheepCommandHandler;
use Cheeper\AllChapters\Application\Command\Author\SignUp;
use Cheeper\AllChapters\Application\Command\Author\SignUpCommandHandler;
use Cheeper\AllChapters\Application\Command\Cheep\PostCheep;
use Cheeper\AllChapters\Application\Command\Cheep\PostCheepHandler;
use Cheeper\Chapter6\Application\Event\EventBus;
use Cheeper\Chapter6\Infrastructure\Application\Event\InMemoryEventBus; use Cheeper\Chapter6\Infrastructure\Application\Event\InMemoryEventBus;
use Cheeper\AllChapters\DomainModel\Author\Authors; use Cheeper\AllChapters\DomainModel\Author\Authors;
use Cheeper\AllChapters\DomainModel\Cheep\Cheeps; use Cheeper\AllChapters\DomainModel\Cheep\Cheeps;
use Cheeper\AllChapters\DomainModel\DomainEvent;
use Cheeper\AllChapters\DomainModel\Follow\Follows;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors; use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryAuthors;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryCheeps; use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryCheeps;
use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryFollows; use Cheeper\AllChapters\Infrastructure\Persistence\InMemoryFollows;
use DateTimeImmutable;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
trait SendsCommands trait SendsCommands
{ {
@@ -57,36 +47,11 @@ trait SendsCommands
$this->eventBus = new InMemoryEventBus(); $this->eventBus = new InMemoryEventBus();
} }
private function signUpAuthorWith(string $authorId, string $userName, string $email, string $name, string $biography, string $location, string $website, string $birthDate): void
{
(new SignUpCommandHandler(
$this->authors
))(
new SignUp(
$authorId,
$userName,
$email,
$name,
$biography,
$location,
$website,
$birthDate
)
);
}
private function followAuthor(string $followee, string $followed): void
{
(new FollowCommandHandler($this->authors, $this->follows))(
FollowCommand::anAuthor($followed, $followee)
);
}
//snippet post-new-cheep-tests //snippet post-new-cheep-tests
private function postNewCheep(string $authorId, string $cheepId, string $message): void private function postNewCheep(string $authorId, string $cheepId, string $message): void
{ {
(new PostCheepHandler($this->authors, $this->cheeps, $this->eventBus))( (new PostCheepCommandHandler($this->authors, $this->cheeps, $this->eventBus))(
PostCheep::fromArray([ PostCheepCommand::fromArray([
'author_id' => $authorId, 'author_id' => $authorId,
'cheep_id' => $cheepId, 'cheep_id' => $cheepId,
'message' => $message, 'message' => $message,