diff --git a/src/Aggregate/AggregateChanged.php b/src/Aggregate/AggregateChanged.php index f66aa37b..79ff3a0c 100644 --- a/src/Aggregate/AggregateChanged.php +++ b/src/Aggregate/AggregateChanged.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Aggregate; use DateTimeImmutable; -use Patchlevel\EventSourcing\Clock; use function json_decode; use function json_encode; @@ -123,6 +122,6 @@ public function serialize(): array protected function createRecordDate(): DateTimeImmutable { - return Clock::createDateTimeImmutable(); + return new DateTimeImmutable(); } } diff --git a/tests/Unit/Aggregate/AggregateChangedTest.php b/tests/Unit/Aggregate/AggregateChangedTest.php index 6ec44592..e7bb61d3 100644 --- a/tests/Unit/Aggregate/AggregateChangedTest.php +++ b/tests/Unit/Aggregate/AggregateChangedTest.php @@ -7,7 +7,6 @@ use DateTimeImmutable; use Error; use Patchlevel\EventSourcing\Aggregate\AggregateException; -use Patchlevel\EventSourcing\Clock; use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; @@ -17,16 +16,6 @@ class AggregateChangedTest extends TestCase { - protected function setUp(): void - { - Clock::reset(); - } - - protected function tearDown(): void - { - Clock::reset(); - } - public function testCreateEvent(): void { $id = ProfileId::fromString('1'); @@ -50,9 +39,6 @@ public function testCreateEvent(): void public function testRecordNow(): void { - $currentDate = new DateTimeImmutable('2020-01-01 12:00:00'); - Clock::freeze($currentDate); - $id = ProfileId::fromString('1'); $email = Email::fromString('d.a.badura@gmail.com'); @@ -62,7 +48,7 @@ public function testRecordNow(): void self::assertEquals($id, $recordedEvent->profileId()); self::assertEquals($email, $recordedEvent->email()); self::assertEquals(1, $recordedEvent->playhead()); - self::assertEquals($currentDate, $recordedEvent->recordedOn()); + self::assertInstanceOf(DateTimeImmutable::class, $recordedEvent->recordedOn()); self::assertEquals( [ 'profileId' => '1', @@ -88,14 +74,14 @@ public function testEventAlreadyBeenRecorded(): void public function testSerialize(): void { - $currentDate = new DateTimeImmutable('2020-01-01 12:00:00'); - Clock::freeze($currentDate); - $id = ProfileId::fromString('1'); $email = Email::fromString('d.a.badura@gmail.com'); $event = ProfileCreated::raise($id, $email); + + $beforeRecording = new DateTimeImmutable(); $recordedEvent = $event->recordNow(1); + $afterRecording = new DateTimeImmutable(); $serializedEvent = $recordedEvent->serialize(); @@ -117,7 +103,11 @@ public function testSerialize(): void self::assertEquals('{"profileId":"1","email":"d.a.badura@gmail.com"}', $serializedEvent['payload']); self::assertArrayHasKey('recordedOn', $serializedEvent); - self::assertEquals($currentDate, $serializedEvent['recordedOn']); + self::assertDateTimeImmutableBetween( + $beforeRecording, + $afterRecording, + $serializedEvent['recordedOn'], + ); } public function testSerializeNotRecorded(): void @@ -200,4 +190,13 @@ public function testDeserializeAndSerialize(): void $event->payload() ); } + + private static function assertDateTimeImmutableBetween( + DateTimeImmutable $fromExpected, + DateTimeImmutable $toExpected, + DateTimeImmutable $actual + ): void { + self::assertGreaterThanOrEqual($fromExpected, $actual); + self::assertLessThanOrEqual($toExpected, $actual); + } }