Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Clock] Align MockClock::sleep() behavior with NativeClock for negati…
…ve values
  • Loading branch information
yoeunes committed Nov 11, 2025
commit 87ec08d43aa0b26eaba1aa54283c94d81549b0d1
4 changes: 4 additions & 0 deletions src/Symfony/Component/Clock/MockClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function now(): DatePoint

public function sleep(float|int $seconds): void
{
if (0 >= $seconds) {
return;
}

$now = (float) $this->now->format('Uu') + $seconds * 1e6;
$now = substr_replace(\sprintf('@%07.0F', $now), '.', -6, 0);
$timezone = $this->now->getTimezone();
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Clock/Tests/MockClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,14 @@ public function testWithTimeZone()
$this->assertNotSame($clock, $utcClock);
$this->assertSame('UTC', $utcClock->now()->getTimezone()->getName());
}

public function testSleepWithNegativeValueDoesNothing()
{
$initialTime = new \DateTimeImmutable('2000-01-01 12:00:00 UTC');

$clock = new MockClock($initialTime);
$clock->sleep(-10.5);

$this->assertEquals($initialTime, $clock->now());
}
}
Loading