🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano
MockClock — mock clock with virtual sleep
Purpose
Like FrozenClock but additionally implements SleepableClockInterface: sleep() instantly advances virtual time without blocking the process.
What for and how to use
Test retry/backoff, polling and timeouts in milliseconds: the code “sleeps”, the clock advances, the test never waits.
Example
$clock = MockClock::from('2026-01-01 00:00:00', 'UTC');
$retry = new RetryPolicy($clock); // the code under test calls sleep()
$retry->run(); // instant: the sleep is virtual
$clock->sleepUntil(new DateTimeImmutable('2026-01-01 00:05:00'));
Anti-patterns
Do not mix with real delays (usleep) in one scenario — the timelines diverge.
Lifecycle
sequenceDiagram
participant T as Test
participant C as MockClock
participant S as Service
T->>C: from('2026-01-01')
T->>S: run()
S->>C: sleep(30)
C-->>S: instantly (+30s virtual)
S->>C: now()
C-->>S: 00:00:30
Feature comparison with competitors
| Aspect | 🏆 CloudCastle | symfony | carbon | lcobucci | beste | ergebnis |
|---|---|---|---|---|---|---|
Virtual sleep() without blocking | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
sleepUntil() until a moment | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
Backward shifts (rewind) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| Isolation without global state | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
| Total | 🏆 4 | 2 | 0 | 1 | 1 | 1 |
🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano