Skip to content

Commit

Permalink
Merge pull request #35 from slepic/frozen-clock-from-utc
Browse files Browse the repository at this point in the history
named constructor FrozenClock::fromUTC()
  • Loading branch information
lcobucci committed Aug 24, 2020
2 parents db18c21 + b64c0a4 commit e370af6
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
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
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 e370af6

Please sign in to comment.