Skip to content

Commit a097a72

Browse files
committed
Add support for MongoDB-based UTCDateTime clock
1 parent 3623eb5 commit a097a72

5 files changed

Lines changed: 87 additions & 0 deletions

File tree

src/Clock/AbstractClock.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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);

src/Clock/MongoUTCClock.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

src/Clock/PsrMongoUTCClock.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

tests/Clock/ManualClockTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Recruiter\Clock;
66

7+
use MongoDB\BSON\UTCDateTime as MongoUTCDateTime;
78
use PHPUnit\Framework\Attributes\CoversClass;
89
use PHPUnit\Framework\Attributes\UsesClass;
910
use 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'));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)