Skip to content

Commit

Permalink
Add named constructor FrozenClock::fromUTC()
Browse files Browse the repository at this point in the history
  • Loading branch information
slepic authored and lcobucci committed Aug 24, 2020
1 parent db18c21 commit b64c0a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Lcobucci\Clock;

use DateTimeImmutable;
use DateTimeZone;

final class FrozenClock implements Clock
{
Expand All @@ -14,6 +15,11 @@ public function __construct(DateTimeImmutable $now)
$this->now = $now;
}

public static function fromUTC(): FrozenClock
{
return new self(new DateTimeImmutable('now', new DateTimeZone('UTC')));
}

public function setTo(DateTimeImmutable $now): void
{
$this->now = $now;
Expand Down
21 changes: 21 additions & 0 deletions test/FrozenClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,25 @@ public function nowSetChangesTheObject(): void
self::assertNotSame($oldNow, $clock->now());
self::assertSame($newNow, $clock->now());
}

/**
* @test
*
* @covers \Lcobucci\Clock\FrozenClock::fromUTC
*
* @uses \Lcobucci\Clock\FrozenClock::__construct
* @uses \Lcobucci\Clock\FrozenClock::now
*/
public function fromUTCCreatesClockFrozenAtCurrentSystemTimeInUTC(): void
{
$lower = new DateTimeImmutable();
$clock = FrozenClock::fromUTC();
$upper = new DateTimeImmutable();

$now = $clock->now();

self::assertGreaterThanOrEqual($lower->getTimestamp(), $now->getTimestamp());
self::assertLessThanOrEqual($upper->getTimestamp(), $now->getTimestamp());
self::assertEquals('UTC', $now->getTimezone()->getName());
}
}

0 comments on commit b64c0a4

Please sign in to comment.