Skip to content

Commit

Permalink
[LOAN-3602] Added mutable now (#4)
Browse files Browse the repository at this point in the history
* [LOAN-3602] Added mutable now.

A mutable \DateTime will help with converting a codebase to using an immutable option in stages.

* [LOAN-3602] Renamed constant

Co-authored-by: Ben Challis <[email protected]>

* [LOAN-3602] Added todo comments for 7.3 bump work

Co-authored-by: Ben Challis <[email protected]>

* Fix ups re: new CI pipeline checks
  • Loading branch information
Rob Treacy authored and ben-challis committed Sep 12, 2019
1 parent 9bc7d04 commit 72df79f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface Clock
{
public function now(): \DateTimeImmutable;

public function nowMutable(): \DateTime;
}
11 changes: 11 additions & 0 deletions lib/FixedClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
final class FixedClock implements Clock
{
private const ISO8601_MICROSECONDS_FORMAT = 'Y-m-d\TH:i:s.uP';

/**
* @var \DateTimeImmutable
*/
Expand All @@ -23,4 +25,13 @@ public function now(): \DateTimeImmutable
{
return $this->now;
}

public function nowMutable(): \DateTime
{
// @TODO Use \DateTime::createFromImmutable when PHP version is >=7.3
$instance = \DateTime::createFromFormat(self::ISO8601_MICROSECONDS_FORMAT, $this->now->format(self::ISO8601_MICROSECONDS_FORMAT));
\assert($instance instanceof \DateTime);

return $instance;
}
}
5 changes: 5 additions & 0 deletions lib/PersistedFixedClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function now(): \DateTimeImmutable
return $this->delegate->now();
}

public function nowMutable(): \DateTime
{
return $this->delegate->nowMutable();
}

private function load(): void
{
$path = $this->getSerializationFilePath();
Expand Down
11 changes: 11 additions & 0 deletions lib/SystemClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ final class SystemClock implements Clock
{
private const DEFAULT_TIMEZONE = 'UTC';

private const ISO8601_MICROSECONDS_FORMAT = 'Y-m-d\TH:i:s.uP';

/**
* @var \DateTimeZone
*/
Expand All @@ -25,4 +27,13 @@ public function now(): \DateTimeImmutable
{
return new \DateTimeImmutable('now', $this->timeZone);
}

public function nowMutable(): \DateTime
{
// @TODO Use \DateTime::createFromImmutable when PHP version is >=7.3
$instance = \DateTime::createFromFormat(self::ISO8601_MICROSECONDS_FORMAT, $this->now()->format(self::ISO8601_MICROSECONDS_FORMAT));
\assert($instance instanceof \DateTime);

return $instance;
}
}
4 changes: 4 additions & 0 deletions tests/unit/FixedClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ public function it_always_returns_the_given_time(): void
$this->assertSame($timeString, $clock->now()->format($timeFormat));
$this->assertSame($timeString, $clock->now()->format($timeFormat));
$this->assertSame($timeString, $clock->now()->format($timeFormat));

$this->assertSame($timeString, $clock->nowMutable()->format($timeFormat));
$this->assertSame($timeString, $clock->nowMutable()->format($timeFormat));
$this->assertSame($timeString, $clock->nowMutable()->format($timeFormat));
}
}
31 changes: 29 additions & 2 deletions tests/unit/SystemClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public function exampleTimeZoneNames(): iterable
yield ['Europe/Paris'];
}

public function nowMethodProvider(): iterable
{
yield ['now'];
yield ['nowMutable'];
}

/**
* @test
* @dataProvider exampleTimeZoneNames
Expand All @@ -38,15 +44,36 @@ public function it_defaults_timezone_to_utc(): void

/**
* @test
* @dataProvider nowMethodProvider
*/
public function it_gives_the_current_system_time(): void
public function it_gives_the_current_system_time(string $nowMethod): void
{
$now = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
$clock = new SystemClock(new \DateTimeZone('UTC'));

$difference = $clock->now()->getTimestamp() - $now->getTimestamp();
$difference = $clock->{$nowMethod}()->getTimestamp() - $now->getTimestamp();

$this->assertGreaterThanOrEqual(0, $difference);
$this->assertLessThanOrEqual(1, $difference);
}

/**
* @test
*/
public function now_returns_DateTimeImmutable(): void
{
$clock = new SystemClock(new \DateTimeZone('UTC'));

$this->assertInstanceOf(\DateTimeImmutable::class, $clock->now());
}

/**
* @test
*/
public function nowMutable_returns_DateTime(): void
{
$clock = new SystemClock(new \DateTimeZone('UTC'));

$this->assertInstanceOf(\DateTime::class, $clock->nowMutable());
}
}

0 comments on commit 72df79f

Please sign in to comment.