File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,6 +13,11 @@ public function asUTC(): UTCClock
1313 return new PsrUTCClock ($ this );
1414 }
1515
16+ public function asMongoUTC (): MongoUTCClock
17+ {
18+ return new PsrMongoUTCClock ($ this );
19+ }
20+
1621 public function asMicrotime (): MicrotimeClock
1722 {
1823 return new PsrMicrotimeClock ($ this );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Recruiter \Clock ;
6+
7+ use MongoDB \BSON \UTCDateTime ;
8+
9+ interface MongoUTCClock
10+ {
11+ public function now (): UTCDateTime ;
12+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Recruiter \Clock ;
6+
7+ use MongoDB \BSON \UTCDateTime ;
8+ use Psr \Clock \ClockInterface ;
9+
10+ final readonly class PsrMongoUTCClock implements MongoUTCClock
11+ {
12+ public function __construct (private ClockInterface $ wrapped )
13+ {
14+ }
15+
16+ public function now (): UTCDateTime
17+ {
18+ return new UTCDateTime ($ this ->wrapped ->now ());
19+ }
20+ }
Original file line number Diff line number Diff line change 44
55namespace Recruiter \Clock ;
66
7+ use MongoDB \BSON \UTCDateTime as MongoUTCDateTime ;
78use PHPUnit \Framework \Attributes \CoversClass ;
89use PHPUnit \Framework \Attributes \UsesClass ;
910use Recruiter \DateTime \UTCDateTime ;
@@ -144,6 +145,18 @@ public function testConversionToUTC(): void
144145 );
145146 }
146147
148+ public function testConversionToMongoUTC (): void
149+ {
150+ $ fixedTime = new \DateTimeImmutable ('2023-10-01 12:00:00 ' , new \DateTimeZone ('Europe/Berlin ' ));
151+ $ clock = new ManualClock ($ fixedTime )->asMongoUTC ();
152+
153+ $ this ->assertEquals (
154+ new MongoUTCDateTime (1_696_154_400_000 ),
155+ $ clock ->now (),
156+ 'Clock should convert fixed time to MongoDB UTCDateTime correctly ' ,
157+ );
158+ }
159+
147160 public function testStopWatch (): void
148161 {
149162 $ clock = new ManualClock (new \DateTimeImmutable ('2023-10-01 12:00:00 ' ));
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Clock ;
6+
7+ use MongoDB \BSON \UTCDateTime ;
8+ use PHPUnit \Framework \Attributes \CoversClass ;
9+ use PHPUnit \Framework \Attributes \UsesClass ;
10+ use PHPUnit \Framework \MockObject \Exception ;
11+ use PHPUnit \Framework \TestCase ;
12+ use Psr \Clock \ClockInterface ;
13+ use Recruiter \Clock \PsrMongoUTCClock ;
14+
15+ #[CoversClass(PsrMongoUTCClock::class)]
16+ #[UsesClass(UTCDateTime::class)]
17+ class PsrMongoUTCClockTest extends TestCase
18+ {
19+ /**
20+ * @throws Exception
21+ */
22+ public function testItCanWrapExistingPSRClock (): void
23+ {
24+ $ original = $ this ->createMock (ClockInterface::class);
25+ $ clock = new PsrMongoUTCClock ($ original );
26+
27+ $ original ->expects ($ this ->once ())
28+ ->method ('now ' )
29+ ->willReturn (new \DateTimeImmutable ('1989-11-09T18:57:00 Europe/Berlin ' ))
30+ ;
31+
32+ $ this ->assertEquals (
33+ new UTCDateTime (626_637_420_000 ),
34+ $ clock ->now (),
35+ );
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments