Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit 9eb8140

Browse files
Ensure that the DomainMessage recordedOn has microseconds and is always timezone UTC
1 parent c8c29f2 commit 9eb8140

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/Domain/DomainMessage.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function __construct($id, $version, $payload, \DateTimeImmutable $recorde
3737
{
3838
$this->id = $id;
3939
$this->payload = $payload;
40-
$this->recordedOn = $recordedOn;
40+
$this->recordedOn = $recordedOn->setTimezone(new \DateTimeZone('UTC'));
4141
$this->version = $version;
4242
}
4343

4444
/**
45-
* @return AggregateIdInterface
45+
* @return string
4646
*/
4747
public function getId()
4848
{
@@ -81,7 +81,9 @@ public function getType()
8181
*/
8282
public static function recordNow($id, $version, DomainEventInterface $payload)
8383
{
84-
return new DomainMessage($id, $version, $payload, new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
84+
$recordedOn = \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
85+
86+
return new DomainMessage($id, $version, $payload, $recordedOn);
8587
}
8688

8789
/**

tests/Domain/DomainMessageTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public function itShouldCreateAUuidFromConstructor(
2525
) {
2626
$message = new DomainMessage($aggregateId, $version, $payload, $date);
2727
$this->assertInstanceOf(DomainMessage::class, $message);
28-
$this->assertEquals($aggregateId, $message->getId());
29-
$this->assertEquals($version, $message->getVersion());
30-
$this->assertEquals($payload, $message->getPayload());
28+
$this->assertSame($aggregateId, $message->getId());
29+
$this->assertSame($version, $message->getVersion());
30+
$this->assertSame($payload, $message->getPayload());
3131
$this->assertEquals($date, $message->getRecordedOn());
32+
$this->assertEquals(new \DateTimeZone('UTC'), $message->getRecordedOn()->getTimezone());
3233
}
3334

3435
/**
@@ -47,14 +48,23 @@ public function itShouldCreateAUuidFromNamedConstructor(
4748
) {
4849
$message = DomainMessage::recordNow($aggregateId, $version, $payload);
4950
$this->assertInstanceOf(DomainMessage::class, $message);
51+
52+
$this->assertNotEmpty((int)$message->getRecordedOn()->format('u'), 'Expected microseconds to be set');
53+
$this->assertEquals(new \DateTimeZone('UTC'), $message->getRecordedOn()->getTimezone());
5054
}
5155

5256
public function messageProvider()
5357
{
5458
return [
5559
[AggregateId::generate(), 1, new SomethingHappened(), new \DateTimeImmutable()],
5660
[AggregateId::generate(), 100, new SomethingHappened(), new \DateTimeImmutable()],
57-
[AggregateId::generate(), 9999999, new SomethingHappened(), new \DateTimeImmutable()]
61+
[AggregateId::generate(), 9999999, new SomethingHappened(), new \DateTimeImmutable()],
62+
[
63+
AggregateId::generate(),
64+
9999999,
65+
new SomethingHappened(),
66+
\DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true)))
67+
]
5868
];
5969
}
6070
}

0 commit comments

Comments
 (0)