Skip to content

Commit

Permalink
Make FrozenClock mutable through setter
Browse files Browse the repository at this point in the history
Because the FrozenClock is often injected in other objects during
testing, being able to change the current time on the FrozenClock
without reinjecting it in the object under test is quite useful.
  • Loading branch information
Frank Koornstra committed Jan 19, 2018
1 parent 2f772f4 commit dd2a647
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function __construct(DateTimeImmutable $now)
$this->now = $now;
}

public function setTo(DateTimeImmutable $now): void
{
$this->now = $now;
}

public function now(): DateTimeImmutable
{
return $this->now;
Expand Down
19 changes: 19 additions & 0 deletions test/FrozenClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,23 @@ public function nowShouldReturnAlwaysTheSameObject()
self::assertSame($now, $clock->now());
self::assertSame($now, $clock->now());
}

/**
* @test
*
* @covers \Lcobucci\Clock\FrozenClock::setTo
* @uses \Lcobucci\Clock\FrozenClock::__construct()
* @uses \Lcobucci\Clock\FrozenClock::now
*/
public function nowSetChangesTheObject()
{
$oldNow = new DateTimeImmutable();
$clock = new FrozenClock($oldNow);

$newNow = new DateTimeImmutable();
$clock->setTo($newNow);

self::assertNotSame($oldNow, $clock->now());
self::assertSame($newNow, $clock->now());
}
}

0 comments on commit dd2a647

Please sign in to comment.