Skip to content

Commit 6db8d19

Browse files
committed
Make SettableClock support both int and interval
1 parent 91d0979 commit 6db8d19

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/Clock/SettableClock.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ public function __construct(\DateTimeInterface $now)
1515
$this->wrapped = new MockClock(\DateTimeImmutable::createFromInterface($now));
1616
}
1717

18-
public function advance(int $seconds): void
18+
public function advance(int|\DateInterval $secondsOrInterval): void
1919
{
20-
$now = $this->now()->add(new \DateInterval("PT{$seconds}S"));
20+
if (!$secondsOrInterval instanceof \DateInterval) {
21+
$secondsOrInterval = new \DateInterval("PT{$secondsOrInterval}S");
22+
}
23+
24+
$now = $this->now()->add($secondsOrInterval);
2125
$this->wrapped = new MockClock($now);
2226
}
2327

tests/Clock/SettableClockTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ public function testItAdvancesTimeBySeconds(): void
3737
);
3838
}
3939

40+
public function testItAdvancesTimeByDateInterval(): void
41+
{
42+
$initialTime = new \DateTime('2023-10-01 12:00:00');
43+
$clock = new SettableClock($initialTime);
44+
45+
$clock->advance(new \DateInterval('PT1H')); // 1 hour
46+
47+
$this->assertEquals(
48+
new \DateTimeImmutable('2023-10-01 13:00:00'),
49+
$clock->now(),
50+
'Clock should advance by the specified seconds',
51+
);
52+
}
53+
4054
public function testItCanAdvanceMultipleTimes(): void
4155
{
4256
$initialTime = new \DateTime('2023-10-01 12:00:00');

0 commit comments

Comments
 (0)