Skip to content

Commit

Permalink
Add modify and default constructor to FrozenClock
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrossi committed Jul 4, 2024
1 parent 9aa7acc commit 7f6238c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

final class FrozenClock implements Clock
{
public function __construct(private DateTimeImmutable $now)
public function __construct(private DateTimeImmutable $now = new DateTimeImmutable())
{
}

Expand All @@ -22,6 +22,11 @@ public function setTo(DateTimeImmutable $now): void
$this->now = $now;
}

public function modify(string $modify): void
{
$this->now = $this->now->modify($modify);
}

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 @@ -34,6 +34,25 @@ public function nowSetChangesTheObject(): void
self::assertSame($newNow, $clock->now());
}

#[Test]
public function modifyChangesTheObject(): void
{
$oldNow = new DateTimeImmutable();
$newNow = $oldNow->modify('+1 day');

$clock = new FrozenClock($oldNow);

Check failure on line 43 in test/FrozenClockTest.php

View workflow job for this annotation

GitHub Actions / Check Coding Standards (locked, 8.2, ubuntu-latest)

Equals sign not aligned correctly; expected 1 space but found 2 spaces

$clock->modify('+1 day');

self::assertNotEquals($oldNow, $clock->now());
self::assertEquals($newNow, $clock->now());

$clock->modify('-1 day');

self::assertEquals($oldNow, $clock->now());
self::assertNotEquals($newNow, $clock->now());
}

#[Test]
public function fromUTCCreatesClockFrozenAtCurrentSystemTimeInUTC(): void
{
Expand Down

0 comments on commit 7f6238c

Please sign in to comment.