Enable chapter 6 and 7

This commit is contained in:
Carlos Buenosvinos
2022-02-14 18:17:51 +01:00
parent 643c6db080
commit ccb6a2bbc1
9 changed files with 26 additions and 169 deletions

View File

@@ -25,6 +25,7 @@
<directory>tests/Cheeper/Tests/Chapter4</directory>
<directory>tests/Cheeper/Tests/Chapter5</directory>
<directory>tests/Cheeper/Tests/Chapter6</directory>
<directory>tests/Cheeper/Tests/Chapter7</directory>
</testsuite>
</testsuites>
<extensions>

View File

@@ -1,45 +0,0 @@
<?php
declare(strict_types=1);
namespace Cheeper\AllChapters\Infrastructure\Persistence;
use Cheeper\AllChapters\DomainModel\Author\AuthorId;
use Cheeper\Chapter4\DomainModel\Follow\Follow;
use Cheeper\Chapter4\DomainModel\Follow\FollowRepository;
use Doctrine\ORM\EntityManagerInterface;
//snippet doctrine-orm-follows
final class DoctrineOrmFollows implements FollowRepository
{
public function __construct(
private EntityManagerInterface $em
) {
}
public function numberOfFollowersFor(AuthorId $authorId): int
{
return 13;
}
public function add(Follow $follow): void
{
$this->em->persist($follow);
}
public function ofFromAuthorIdAndToAuthorId(AuthorId $fromAuthorId, AuthorId $toAuthorId): ?Follow
{
return $this->em->getRepository(Follow::class)->findOneBy([
'fromAuthorId' => $fromAuthorId,
'toAuthorId' => $toAuthorId,
]);
}
public function toAuthorId(AuthorId $authorId): array
{
return $this->em->getRepository(Follow::class)->findBy([
'toAuthorId' => $authorId
]);
}
}
//end-snippet

View File

@@ -1,66 +0,0 @@
<?php
declare(strict_types=1);
namespace Cheeper\AllChapters\Infrastructure\Persistence;
use Cheeper\AllChapters\DomainModel\Author\AuthorId;
use Cheeper\AllChapters\DomainModel\Follow\Follow;
use Cheeper\AllChapters\DomainModel\Follow\FollowId;
use Cheeper\AllChapters\DomainModel\Follow\Follows;
use function Functional\head;
use function Functional\reduce_left;
use function Functional\select;
final class InMemoryFollows implements Follows
{
/** @var array<string, Follow> */
public array $collection = [];
public function ofId(FollowId $followId): ?Follow
{
$candidate = head(
select($this->collection, fn (Follow $u): bool => $u->followId()->equals($followId))
);
if (null === $candidate) {
return null;
}
return $candidate;
}
public function add(Follow $follow): void
{
$candidate = head(
select($this->collection, fn (Follow $u): bool => $u->fromAuthorId()->equals($follow->fromAuthorId()) && $u->toAuthorId()->equals($follow->toAuthorId()))
);
if ((null !== $candidate && $candidate != $follow) || null === $candidate) {
$this->collection[$follow->followId()->toString()] = $follow;
}
}
public function numberOfFollowersFor(AuthorId $authorId): int
{
return reduce_left(
$this->collection,
fn (Follow $f, string $key, array $collection, int $initial): int => $initial + ($f->fromAuthorId()->equals($authorId) ? 1 : 0),
0
);
}
public function ofFromAuthorIdAndToAuthorId(AuthorId $fromAuthorId, AuthorId $toAuthorId): ?Follow
{
$candidate = head(
select($this->collection, fn (Follow $u): bool => $u->fromAuthorId()->equals($fromAuthorId) && $u->toAuthorId()->equals($toAuthorId))
);
return $candidate ?? null;
}
public function toAuthorId(AuthorId $authorId): array
{
return select($this->collection, fn (Follow $f): bool => $f->toAuthorId()->equals($authorId));
}
}

View File

@@ -65,15 +65,6 @@ final class SignUpCommand
{
return $this->birthDate;
}
public static function fromArray(array $array): self
{
return new self(
$array['author_id'] ?? '',
$array['username'] ?? '',
$array['email'] ?? '',
);
}
//end-ignore
}
//end-snippet

View File

@@ -19,7 +19,7 @@ final class DoctrineOrmFollowRepository implements FollowRepository
public function numberOfFollowersFor(AuthorId $authorId): int
{
return 13;
return count($this->toAuthorId($authorId));
}
public function add(Follow $follow): void

View File

@@ -8,7 +8,6 @@ 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;

View File

@@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Cheeper\Chapter5\DomainModel\Follow;
final class FollowersCounterResource
{
public function __construct(
public string $userId,
public string $userName,
public int $counter,
) {
}
}

View File

@@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace Cheeper\Chapter5\DomainModel\Follow;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\UuidInterface;
#[ORM\Table(name: "chapter5_followers")]
class NumberOfFollowers
{
public function __construct(
#[ORM\Column(type: "uuid_binary")]
#[ORM\GeneratedValue(strategy: "NONE")]
#[ORM\Id]
private UuidInterface $userId,
#[ORM\Column(type: "integer")]
private int $followers
) {
}
public function userId(): UuidInterface
{
return $this->userId;
}
public function followers(): int
{
return $this->followers;
}
}

View File

@@ -43,9 +43,33 @@ final class SignUpCommandBuilderTest extends TestCase
->name('John Doe')
->website('https://johndoe.com')
->biography('An example author')
->birthDate('31/01/1983')
->location('California')
->build();
//end-snippet
$this->assertInstanceOf(SignUpCommand::class, $command);
}
/** @test */
public function itBuildsOptionalFieldsAndCanUpdateMandatoryOnesBeforeBuilding(): void
{
$location = 'California';
$name = 'John Doe';
$newUserName = 'new_johndoe';
$command = SignUpCommandBuilderMock::builder(Uuid::uuid4()->toString(), 'johndoe', 'test@email.com')
->name($name)
->website('https://johndoe.com')
->biography('An example author')
->birthDate('31/01/1983')
->location($location)
->username($newUserName)
->email('john@doe.domain')
->build();
$this->assertInstanceOf(SignUpCommand::class, $command);
$this->assertSame($command->location(), $location);
$this->assertSame($command->username(), $newUserName);
}
}