Commit 9e0088f
committed
bug #62346 [Clock] Align MockClock::sleep() behavior with NativeClock for negative values (yoeunes)
This PR was merged into the 6.4 branch.
Discussion
----------
[Clock] Align MockClock::sleep() behavior with NativeClock for negative values
| Q | A
| ------------- | ---
| Branch? | 6.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | -
| License | MIT
This PR fixes a behavioral inconsistency between `NativeClock` and `MockClock` when handling negative sleep durations.
**The Problem:**
* `NativeClock::sleep(-10)`: Silently does nothing. The time does not change.
* `MockClock::sleep(-10)`: **Travels backward in time** by 10 seconds.
This inconsistency can lead to unreliable tests, as the mock does not accurately represent the real implementation's behavior for this edge case.
**The Fix:**
This fix adds a guard clause to `MockClock::sleep()` to ignore any duration less than or equal to zero, perfectly matching the behavior of `NativeClock`.
**Before (The Bug):**
```php
$clock = new MockClock('2000-01-01 12:00:00');
$clock->sleep(-10);
// $clock->now() is '2000-01-01 11:59:50'
```
**After (The Fix):**
```php
$clock = new MockClock('2000-01-01 12:00:00');
$clock->sleep(-10);
// $clock->now() is '2000-01-01 12:00:00'
```
Commits
-------
87ec08d [Clock] Align MockClock::sleep() behavior with NativeClock for negative valuesFile tree
2 files changed
+14
-0
lines changed- src/Symfony/Component/Clock
- Tests
2 files changed
+14
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
57 | 61 | | |
58 | 62 | | |
59 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
120 | 130 | | |
0 commit comments