Skip to content

Commit

Permalink
Merge pull request #51 from lcobucci/add-named-constructors-to-system…
Browse files Browse the repository at this point in the history
…-clock

Add named constructors to system clock
  • Loading branch information
lcobucci authored Aug 27, 2020
2 parents d024e40 + f159909 commit 70e3a8f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ function filterData(Clock $clock, array $objects): array
}

// Object that will return the current time based on the given timezone
$clock = new SystemClock(new DateTimeZone('UTC'));
// $clock = SystemClock::fromSystemTimezone();
// $clock = SystemClock::fromUTC();
$clock = new SystemClock(new DateTimeZone('America/Sao_Paulo'));

// Test object that always returns a fixed time object
$clock = new FrozenClock(
new DateTimeImmutable('2017-05-07 18:49:30')
);

// Or creating a frozen clock from the current time on UTC
// $clock = FrozenClock::fromUTC();

$objects = [
(object) ['expiresAt' => new DateTimeImmutable('2017-12-31 23:59:59')],
(object) ['expiresAt' => new DateTimeImmutable('2017-06-30 23:59:59')],
Expand Down
2 changes: 1 addition & 1 deletion src/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(DateTimeImmutable $now)
$this->now = $now;
}

public static function fromUTC(): FrozenClock
public static function fromUTC(): self
{
return new self(new DateTimeImmutable('now', new DateTimeZone('UTC')));
}
Expand Down
10 changes: 10 additions & 0 deletions src/SystemClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public function __construct(?DateTimeZone $timezone = null)
$this->timezone = $timezone ?? new DateTimeZone(date_default_timezone_get());
}

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

public static function fromSystemTimezone(): self
{
return new self(new DateTimeZone(date_default_timezone_get()));
}

public function now(): DateTimeImmutable
{
return new DateTimeImmutable('now', $this->timezone);
Expand Down
11 changes: 3 additions & 8 deletions test/FrozenClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,15 @@ public function nowSetChangesTheObject(): void
* @test
*
* @covers \Lcobucci\Clock\FrozenClock::fromUTC
* @covers \Lcobucci\Clock\FrozenClock::__construct
*
* @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();
$now = $clock->now();

self::assertGreaterThanOrEqual($lower->getTimestamp(), $now->getTimestamp());
self::assertLessThanOrEqual($upper->getTimestamp(), $now->getTimestamp());
self::assertEquals('UTC', $now->getTimezone()->getName());
self::assertSame('UTC', $now->getTimezone()->getName());
}
}
32 changes: 32 additions & 0 deletions test/SystemClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,36 @@ public function nowShouldRespectTheProvidedTimezone(): void
self::assertGreaterThanOrEqual($lower, $now);
self::assertLessThanOrEqual($upper, $now);
}

/**
* @test
*
* @covers \Lcobucci\Clock\SystemClock::fromUTC
* @covers \Lcobucci\Clock\SystemClock::__construct
*
* @uses \Lcobucci\Clock\SystemClock::now
*/
public function fromUTCCreatesAnInstanceUsingUTCAsTimezone(): void
{
$clock = SystemClock::fromUTC();
$now = $clock->now();

self::assertSame('UTC', $now->getTimezone()->getName());
}

/**
* @test
*
* @covers \Lcobucci\Clock\SystemClock::fromSystemTimezone
* @covers \Lcobucci\Clock\SystemClock::__construct
*
* @uses \Lcobucci\Clock\SystemClock::now
*/
public function fromSystemTimezoneCreatesAnInstanceUsingTheDefaultTimezoneInSystem(): void
{
$clock = SystemClock::fromSystemTimezone();
$now = $clock->now();

self::assertSame(date_default_timezone_get(), $now->getTimezone()->getName());
}
}

0 comments on commit 70e3a8f

Please sign in to comment.