lcobucci/clock
Small PHP clock abstraction to decouple your code from direct DateTimeImmutable instantiation. Depend on the Clock interface and use SystemClock for real time or FrozenClock for deterministic tests, with explicit timezone support.
new DateTimeImmutable() with dependency-injected clocks (e.g., FrozenClock). Eliminates flaky tests caused by real-time dependencies.SystemClock::fromUTC()).now() calls in cron-like logic with injectable clocks.Adopt if:
new DateTimeImmutable() or time() directly in business logic.Look elsewhere if:
Carbon and already leverage its freezeTime() or shouldReceive() methods (though lcobucci/clock offers a more generic solution).spatie/temporal.Timecop or Java’s Clock patterns—this is PHP-specific.For Executives:
*"This package lets us write unbreakable time-sensitive logic—like expiration checks or scheduled jobs—without flaky tests. By injecting a 'clock' instead of using new DateTime(), we can:
For Engineers:
*"Replace new DateTimeImmutable() with Clock interface:
SystemClock (e.g., SystemClock::fromUTC()).FrozenClock with fixed timestamps (e.g., FrozenClock::fromUTC('2023-01-01')).
Benefits:sleep(1) hacks in tests.// Before (brittle)
if (new DateTimeImmutable() > $expiry) { ... }
// After (testable)
if ($clock->now() > $expiry) { ... }
Migration path: Start with critical time-sensitive services (e.g., payments, subscriptions)."*
For QA/Testers: *"This lets you reproduce time-based bugs deterministically. No more:
$clock = new FrozenClock(new DateTimeImmutable('2023-12-31 23:59:59'));
// Now tests always run at 'Dec 31, 2023'—no surprises!"*
How can I help you explore Laravel packages today?