Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

named constructor FrozenClock::fromUTC() #35

Merged
merged 1 commit into from Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
}
}