105 lines
3.2 KiB
PHP
105 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Cheeper\Tests\Chapter7\Application\Cheep\Event;
|
|
|
|
use Cheeper\AllChapters\DomainModel\Author\AuthorId;
|
|
use Cheeper\AllChapters\DomainModel\Cheep\CheepDate;
|
|
use Cheeper\AllChapters\DomainModel\Cheep\CheepId;
|
|
use Cheeper\AllChapters\DomainModel\Cheep\CheepMessage;
|
|
use Cheeper\AllChapters\DomainModel\Follow\FollowId;
|
|
use Cheeper\Chapter7\Application\Cheep\Event\CheepPostedEventHandler;
|
|
use Cheeper\Chapter7\DomainModel\Cheep\CheepPosted;
|
|
use Cheeper\Chapter7\DomainModel\Follow\Follow;
|
|
use Cheeper\Chapter7\DomainModel\Follow\FollowRepository;
|
|
use Cheeper\Chapter7\Infrastructure\Application\InMemoryProjectionBus;
|
|
use DateTimeImmutable;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class CheepPostedEventHandlerTest extends TestCase
|
|
{
|
|
/**
|
|
* @test
|
|
* @Given Non Existing Customer Or Without
|
|
* @When When
|
|
* @Then
|
|
*/
|
|
public function authorNonExistingOrWithoutFollowers(): void
|
|
{
|
|
$emptyFollowersList = [];
|
|
$followRepository = $this->createMock(FollowRepository::class);
|
|
$followRepository->expects($this->once())->method('toAuthorId')->willReturn($emptyFollowersList);
|
|
|
|
$projectionBus = new InMemoryProjectionBus();
|
|
|
|
$eventHandler = new CheepPostedEventHandler(
|
|
$followRepository,
|
|
$projectionBus
|
|
);
|
|
|
|
$eventHandler(
|
|
CheepPosted::create(
|
|
CheepId::nextIdentity(),
|
|
AuthorId::nextIdentity(),
|
|
CheepMessage::write('Hello World!'),
|
|
new CheepDate(
|
|
(new DateTimeImmutable(
|
|
'now',
|
|
new \DateTimeZone('UTC')
|
|
))->format('Y-m-d H:i:s')
|
|
)
|
|
)
|
|
);
|
|
|
|
$this->assertCount(0, $projectionBus->projections());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
* @Given Non Existing Customer Or Without
|
|
* @When When
|
|
* @Then
|
|
*/
|
|
public function authorWithMultipleFollowers(): void
|
|
{
|
|
$rockStarAuthorId = AuthorId::nextIdentity();
|
|
|
|
$followersList = [];
|
|
for ($i = 0; $i < 10; $i++) {
|
|
$followersList[] = Follow::fromAuthorToAuthor(
|
|
FollowId::nextIdentity(),
|
|
AuthorId::nextIdentity(),
|
|
$rockStarAuthorId
|
|
);
|
|
}
|
|
|
|
$followRepository = $this->createMock(FollowRepository::class);
|
|
$followRepository->expects($this->once())->method('toAuthorId')->willReturn($followersList);
|
|
|
|
$projectionBus = new InMemoryProjectionBus();
|
|
|
|
$eventHandler = new CheepPostedEventHandler(
|
|
$followRepository,
|
|
$projectionBus
|
|
);
|
|
|
|
$eventHandler(
|
|
CheepPosted::create(
|
|
CheepId::nextIdentity(),
|
|
$rockStarAuthorId,
|
|
CheepMessage::write('Hello World!'),
|
|
new CheepDate(
|
|
(new DateTimeImmutable(
|
|
'now',
|
|
new \DateTimeZone('UTC')
|
|
))->format('Y-m-d H:i:s')
|
|
)
|
|
)
|
|
);
|
|
|
|
$projections = $projectionBus->projections();
|
|
$this->assertCount(10, $projections);
|
|
}
|
|
}
|