-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Christian Kolb <[email protected]>
- Loading branch information
1 parent
653a356
commit de94dc2
Showing
8 changed files
with
141 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
version: '3.7' | ||
|
||
services: | ||
|
||
php-8.2: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
interface Clock | ||
{ | ||
public function now(): Moment; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
final class FrozenClock implements Clock | ||
{ | ||
private Moment $moment; | ||
|
||
public function __construct(?Moment $moment = null) | ||
{ | ||
$this->moment = $moment ?? Moment::fromDateTime(new \DateTimeImmutable('now')); | ||
} | ||
|
||
public function freeze(Moment $moment): void | ||
{ | ||
$this->moment = $moment; | ||
} | ||
|
||
#[\Override] | ||
public function now(): Moment | ||
{ | ||
return $this->moment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
final readonly class SystemClock implements Clock | ||
{ | ||
#[\Override] | ||
public function now(): Moment | ||
{ | ||
return Moment::fromDateTime(new \DateTimeImmutable( | ||
'now', | ||
new \DateTimeZone('UTC'), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(FrozenClock::class)] | ||
final class FrozenClockTest extends TestCase | ||
{ | ||
#[Test] | ||
public function construction_works(): void | ||
{ | ||
// -- Arrange | ||
$moment = Moment::fromStringInTimeZone('2024-05-06 08:00:00', new \DateTimeZone('Europe/Berlin')); | ||
|
||
// -- Act | ||
$clock = new FrozenClock($moment); | ||
|
||
// -- Assert | ||
self::assertSame($moment, $clock->now()); | ||
} | ||
|
||
#[Test] | ||
public function freeze_works(): void | ||
{ | ||
// -- Arrange | ||
$clock = new FrozenClock(); | ||
$moment = Moment::fromStringInTimeZone('2024-05-06 08:00:00', new \DateTimeZone('Europe/Berlin')); | ||
|
||
// -- Act | ||
$clock->freeze($moment); | ||
|
||
// -- Assert | ||
self::assertSame($moment, $clock->now()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Clock; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Only very little that's possible to test here, so we do what's possible. | ||
*/ | ||
#[CoversClass(SystemClock::class)] | ||
final class SystemClockTest extends TestCase | ||
{ | ||
#[Test] | ||
public function now_works(): void | ||
{ | ||
// -- Arrange | ||
$clock = new SystemClock(); | ||
|
||
// -- Act | ||
$moment = $clock->now(); | ||
|
||
// -- Assert | ||
self::assertEquals($moment->dateTime->getTimezone(), new \DateTimeZone('UTC')); | ||
} | ||
} |