Skip to content

Commit 87ec08d

Browse files
committed
[Clock] Align MockClock::sleep() behavior with NativeClock for negative values
1 parent 4433ffc commit 87ec08d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Symfony/Component/Clock/MockClock.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function now(): DatePoint
5454

5555
public function sleep(float|int $seconds): void
5656
{
57+
if (0 >= $seconds) {
58+
return;
59+
}
60+
5761
$now = (float) $this->now->format('Uu') + $seconds * 1e6;
5862
$now = substr_replace(\sprintf('@%07.0F', $now), '.', -6, 0);
5963
$timezone = $this->now->getTimezone();

src/Symfony/Component/Clock/Tests/MockClockTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,14 @@ public function testWithTimeZone()
117117
$this->assertNotSame($clock, $utcClock);
118118
$this->assertSame('UTC', $utcClock->now()->getTimezone()->getName());
119119
}
120+
121+
public function testSleepWithNegativeValueDoesNothing()
122+
{
123+
$initialTime = new \DateTimeImmutable('2000-01-01 12:00:00 UTC');
124+
125+
$clock = new MockClock($initialTime);
126+
$clock->sleep(-10.5);
127+
128+
$this->assertEquals($initialTime, $clock->now());
129+
}
120130
}

0 commit comments

Comments
 (0)