Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recorded clock trait #137

Merged
merged 2 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Aggregate/ClockRecordDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Aggregate;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock;

/**
* @psalm-require-extends AggregateChanged
*/
trait ClockRecordDate
{
protected function createRecordDate(): DateTimeImmutable
{
return Clock::createDateTimeImmutable();
}
}
29 changes: 29 additions & 0 deletions tests/Unit/Aggregate/AggregateChangedWithClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisitedWithClock;
use PHPUnit\Framework\TestCase;

class AggregateChangedWithClockTest extends TestCase
{
public function testRecordedAt(): void
{
$date = new DateTimeImmutable();

Clock::freeze($date);

$profile1 = ProfileId::fromString('1');
$profile2 = ProfileId::fromString('2');

$event = ProfileVisitedWithClock::raise($profile1, $profile2);
$recordedEvent = $event->recordNow(1);

self::assertSame($date, $recordedEvent->recordedOn());
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Fixture/ProfileVisitedWithClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Fixture;

use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
use Patchlevel\EventSourcing\Aggregate\ClockRecordDate;

/**
* @template-extends AggregateChanged<array{visitorId: string}>
*/
final class ProfileVisitedWithClock extends AggregateChanged
{
use ClockRecordDate;

public static function raise(ProfileId $visitedId, ProfileId $visitorId): self
{
return new self(
$visitedId->toString(),
[
'visitorId' => $visitorId->toString(),
]
);
}

public function visitedId(): ProfileId
{
return ProfileId::fromString($this->aggregateId);
}

public function visitorId(): ProfileId
{
return ProfileId::fromString($this->payload['visitorId']);
}
}