All tests running until Chapter6, increasing coverage

This commit is contained in:
Carlos Buenosvinos
2022-02-14 10:49:10 +01:00
parent 99f08b88c1
commit f9d9b962b3
6 changed files with 90 additions and 31 deletions

BIN
infection.phar Normal file → Executable file

Binary file not shown.

11
infection.phar.asc Normal file
View File

@@ -0,0 +1,11 @@
-----BEGIN PGP SIGNATURE-----
iQEzBAABCAAdFiEExtdsMp663i+5xFjPxQlZhkk7SqAFAmHcC9UACgkQxQlZhkk7
SqDaMQgAn+sCCxq46/RqN0tDNTTbKfr4CfPSQtfemo83lmN3wwn8uDfpQEkp5aMw
4gswcGYY4m4/FgPJKM5OeiTcodOkwLoAZhAQRLR2Nztq5o2QRLVObikxqDj85OfE
P5lw89IJhBoDJPdE5wKLJPUEAn5kKmO9WbPlBx00ExSq/3DYhiVNQaHkvPTwMv04
OVGoRrrVsu2m7StJBI6Hrq5iQqpAfBMPV6Qc/O/ebeGR3eYick0MowEVarmwVVkS
Kdo0MKGjxnXKvCLi4MkxvtthlsF3osNpb44XPyPGbxq0bILYU0qmWJaD3XVs33qw
ydii9WCSFwdvqtft7DPnAU4/ZeXOcw==
=Oa2R
-----END PGP SIGNATURE-----

View File

@@ -24,33 +24,47 @@ final class SignUpCommandHandler
public function __invoke(SignUpCommand $command): void
{
$userName = UserName::pick($command->userName());
$author = $this->authors->ofUserName($userName);
$authorId = AuthorId::fromString($command->authorId());
$email = EmailAddress::from($command->email());
$author = $this->authors->ofUserName($userName);
$this->checkAuthorDoesNotAlreadyExistByUsername($author, $userName);
$author = $this->authors->ofId($authorId);
$this->checkAuthorDoesNotAlreadyExistById($author, $authorId);
$inputWebsite = $command->website();
$website = null !== $inputWebsite ? Website::fromString($inputWebsite) : null;
$inputBirthDate = $command->birthDate();
$birthDate = null !== $inputBirthDate ? BirthDate::fromString($inputBirthDate) : null;
$author = Author::signUp(
$authorId,
$userName,
$email,
$command->name(),
$command->biography(),
$command->location(),
$website,
$birthDate
);
$this->authors->add($author);
}
private function checkAuthorDoesNotAlreadyExistByUsername(?Author $author, UserName $userName): void
{
if (null !== $author) {
throw AuthorAlreadyExists::withUserNameOf($userName);
}
}
$authorId = AuthorId::fromString($command->authorId());
$email = new EmailAddress($command->email());
$inputWebsite = $command->website();
$website = null !== $inputWebsite ? new Website($inputWebsite) : null;
$inputBirthDate = $command->birthDate();
$birthDate = null !== $inputBirthDate ? new BirthDate($inputBirthDate) : null;
$this->authors->add(
Author::signUp(
$authorId,
$userName,
$email,
$command->name(),
$command->biography(),
$command->location(),
$website,
$birthDate
)
);
private function checkAuthorDoesNotAlreadyExistById(?Author $author, AuthorId $authorId): void
{
if (null !== $author) {
throw AuthorAlreadyExists::withIdOf($authorId);
}
}
}
//end-snippet

View File

@@ -22,15 +22,13 @@ final class InMemoryAuthorRepository implements AuthorRepository
public function ofId(AuthorId $authorId): ?Author
{
return $this->authors[$authorId->id()] ?? null;
$candidate = head(
select($this->authors, fn (Author $u): bool => $u->authorId()->equals($authorId))
);
if (null === $candidate) {
return $candidate;
}
return $candidate;
return $candidate ?? null;
}
public function ofUserName(UserName $userName): ?Author

View File

@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Cheeper\Chapter6\Infrastructure\Application;
use Cheeper\AllChapters\DomainModel\DomainEvent;
use Cheeper\Chapter6\Application\Event\EventBus;
use Cheeper\Chapter6\Application\EventBus;
//snippet in-memory-event-bus
final class InMemoryEventBus implements EventBus

View File

@@ -23,7 +23,7 @@ final class SignUpCommandHandlerTest extends TestCase
}
/** @test */
public function givenAUserNameThatAlreadyBelongsToAnExistingUserWhenSignUpThenAnExceptionShouldBeThrown(): void
public function givenAnAuthorNameThatAlreadyBelongsToAnExistingUserWhenSignUpThenAnExceptionShouldBeThrown(): void
{
$this->expectException(AuthorAlreadyExists::class);
$this->expectExceptionMessage('Author with name "johndoe" already exists');
@@ -60,7 +60,7 @@ final class SignUpCommandHandlerTest extends TestCase
}
/** @test */
public function givenValidUserDataWithOnlyMandatoryFieldsWhenSignUpThenAValidUserShouldBeCreated(): void
public function givenValidAuthorDataWithOnlyMandatoryFieldsWhenSignUpThenAValidUserShouldBeCreated(): void
{
$signUpHandler = new SignUpCommandHandler($this->authorRepository);
@@ -87,7 +87,7 @@ final class SignUpCommandHandlerTest extends TestCase
}
/** @test */
public function givenValidUserDataWhenSignUpThenAValidUserShouldBeCreated(): void
public function givenValidAuthorDataWhenSignUpThenAValidUserShouldBeCreated(): void
{
$signUpHandler = new SignUpCommandHandler($this->authorRepository);
@@ -122,4 +122,40 @@ final class SignUpCommandHandlerTest extends TestCase
$this->assertSame($website, $actualAuthor->website()->toString());
$this->assertSame($birthDate, $actualAuthor->birthDate()->date()->format('Y-m-d'));
}
/** @test */
public function givenAnAuthorIdThatAlreadyBelongsToAnExistingUserWhenSignUpThenAnExceptionShouldBeThrown(): void
{
$this->expectException(AuthorAlreadyExists::class);
// $this->expectExceptionMessage('Author with name "johndoe" already exists');
$signUpHandler = new SignUpCommandHandler($this->authorRepository);
$authorId = '0c57e704-3982-4c90-9f3f-e00d5ea546ac';
$signUpHandler(
new SignUpCommand(
$authorId,
'johndoe',
'johndoe@example.com',
'John Doe',
'The usual profile example',
'Madrid',
'https://example.com/',
(new DateTimeImmutable())->format('Y-m-d')
)
);
$signUpHandler(
new SignUpCommand(
$authorId,
'new_johndoe',
'johndoe@example.com',
'John Doe',
'The usual profile example',
'Madrid',
'https://example.com/',
(new DateTimeImmutable())->format('Y-m-d')
)
);
}
}