Skip to content

Commit b5e7500

Browse files
committed
[Clock] Align MockClock::sleep() behavior with NativeClock for negative values
1 parent 105ca41 commit b5e7500

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,16 @@ 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+
$clock = new MockClock($initialTime);
125+
126+
// Attempt to sleep for a negative duration
127+
$clock->sleep(-10.5);
128+
129+
// Assert that the time has NOT changed, matching NativeClock behavior
130+
$this->assertEquals($initialTime, $clock->now());
131+
}
120132
}

0 commit comments

Comments
 (0)