Skip to content

Commit

Permalink
Add clock implementation (#47)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Kolb <[email protected]>
  • Loading branch information
christian-kolb and Christian Kolb authored May 6, 2024
1 parent 653a356 commit de94dc2
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.11.0

- Added `Clock` implementation with `SystemClock` and `FixedClock`.

## 0.10.0

- Added `is*InTimeZone(Time | Date | Month | Year $equalTo, \DateTimeZone $timeZone): bool` methods to `Moment`.
Expand Down
12 changes: 8 additions & 4 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade guide

## From 0.10.* to 0.11.0

No breaking changes.

## From 0.9.* to 0.10.0

No breaking changes (just deprecations).
Expand Down Expand Up @@ -38,19 +42,19 @@ When using `MomentType`, you need to migrate the database column to support mill

## From 0.6.* to 0.7.0

No breaking changes
No breaking changes.

## From 0.5.* to 0.6.0

No breaking changes
No breaking changes.

## From 0.4.* to 0.5.0

You can remove `YearNormalizer` from your normalizers if you registered it manually.

## From 0.3.* to 0.4.0

No breaking changes
No breaking changes.

## From 0.2.* to 0.3.0

Expand Down Expand Up @@ -85,4 +89,4 @@ if ($date->day === 15) {

## From 0.1.* to 0.2.0

No breaking changes
No breaking changes.
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.7'

services:

php-8.2:
Expand Down
12 changes: 12 additions & 0 deletions src/Clock/Clock.php
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;
}
28 changes: 28 additions & 0 deletions src/Clock/FrozenClock.php
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;
}
}
19 changes: 19 additions & 0 deletions src/Clock/SystemClock.php
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'),
));
}
}
41 changes: 41 additions & 0 deletions tests/Clock/FrozenClockTest.php
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());
}
}
29 changes: 29 additions & 0 deletions tests/Clock/SystemClockTest.php
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'));
}
}

0 comments on commit de94dc2

Please sign in to comment.