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

Commit 3bb0483

Browse files
Ensure Snapshots createdAt has microseconds and is always timezone UTC
1 parent 9eb8140 commit 3bb0483

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/EventStore/Snapshot/Snapshot.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
$this->aggregateId = $aggregateId;
4444
$this->aggregate = $aggregate;
4545
$this->version = $version;
46-
$this->createdAt = $createdAt;
46+
$this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC'));
4747
}
4848

4949
/**
@@ -55,7 +55,9 @@ public function __construct(
5555
*/
5656
public static function take(AggregateIdInterface $aggregateId, AggregateRootInterface $aggregate, $version)
5757
{
58-
return new static($aggregateId, $aggregate, $version, new \DateTimeImmutable());
58+
$dateTime = \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
59+
60+
return new static($aggregateId, $aggregate, $version, $dateTime);
5961
}
6062

6163
/**
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace HelloFresh\Tests\Engine\EventStore\Snapshot;
4+
5+
use HelloFresh\Engine\Domain\AggregateId;
6+
use HelloFresh\Engine\Domain\AggregateIdInterface;
7+
use HelloFresh\Engine\Domain\AggregateRootInterface;
8+
use HelloFresh\Engine\EventStore\Snapshot\Snapshot;
9+
use HelloFresh\Tests\Engine\Mock\AggregateRoot;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class SnapshotTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
* @dataProvider messageProvider
17+
* @param AggregateIdInterface $aggregateId
18+
* @param AggregateRootInterface $aggregate
19+
* @param $version
20+
* @param \DateTimeImmutable $date
21+
* @internal param $payload
22+
*/
23+
public function itShouldCreateASnapshotFromConstructor(
24+
AggregateIdInterface $aggregateId,
25+
AggregateRootInterface $aggregate,
26+
$version,
27+
\DateTimeImmutable $date
28+
) {
29+
$message = new Snapshot($aggregateId, $aggregate, $version, $date);
30+
31+
$this->assertInstanceOf(Snapshot::class, $message);
32+
$this->assertSame($aggregateId, $message->getAggregateId());
33+
$this->assertSame($aggregate, $message->getAggregate());
34+
$this->assertSame($version, $message->getVersion());
35+
$this->assertSame(get_class($aggregate), $message->getType());
36+
$this->assertEquals($date, $message->getCreatedAt());
37+
$this->assertEquals(new \DateTimeZone('UTC'), $message->getCreatedAt()->getTimezone());
38+
}
39+
40+
/**
41+
* @test
42+
* @dataProvider messageProvider
43+
* @param AggregateIdInterface $aggregateId
44+
* @param AggregateRootInterface $aggregate
45+
* @param string $version
46+
*/
47+
public function itShouldCreateASnapshotFromNamedConstructor(
48+
AggregateIdInterface $aggregateId,
49+
AggregateRootInterface $aggregate,
50+
$version
51+
) {
52+
$snapshot = Snapshot::take($aggregateId, $aggregate, $version);
53+
54+
$this->assertInstanceOf(Snapshot::class, $snapshot);
55+
$this->assertNotEmpty((int)$snapshot->getCreatedAt()->format('u'), 'Expected microseconds to be set');
56+
$this->assertEquals(new \DateTimeZone('UTC'), $snapshot->getCreatedAt()->getTimezone());
57+
}
58+
59+
public function messageProvider()
60+
{
61+
return [
62+
[
63+
AggregateId::generate(),
64+
AggregateRoot::create(AggregateId::generate(), 'v1000'),
65+
'1000',
66+
new \DateTimeImmutable()
67+
],
68+
[
69+
AggregateId::generate(),
70+
AggregateRoot::create(AggregateId::generate(), 'v1'),
71+
'1',
72+
\DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true)))
73+
]
74+
];
75+
}
76+
}

0 commit comments

Comments
 (0)