beste/clock
beste/clock is a lightweight PHP clock implementation for time-dependent code. It provides a simple, test-friendly way to obtain the current time, swap in fixed or mock clocks, and improve determinism across your application and automated tests.
Start by installing via Composer: composer require beste/clock. This package provides immutable ClockInterface implementations—primarily SystemClock, FrozenClock, and MockClock—to abstract time-related operations in your application. The core use case is injecting clock dependencies instead of calling date(), time(), or Carbon::now() directly, enabling better testability and control over time in business logic. First, bind a clock implementation in your service container (e.g., SystemClock for production, FrozenClock for tests), then type-hint ClockInterface in services, commands, or domain services.
ClockInterface into value objects, aggregates, or domain services to determine current time for timestamps, expiry calculations, or scheduling without side effects.FrozenClock with a fixed DateTimeImmutable in unit tests (e.g., $clock = new FrozenClock(new DateTimeImmutable('2022-01-01'))), or MockClock (if available) for advanced assertions on time progression.ClockInterface::class => fn() => new SystemClock() in AppServiceProvider. Override in phpunit.xml or feature test setups with FrozenClock for deterministic test runs.\DateTimeZone when constructing clocks to handle localized time logically (e.g., new SystemClock(new \DateTimeZone('UTC'))), especially for cron jobs or multi-region apps.FrozenClock stays static unless manually advanced via withTime(). For advancing time automatically (e.g., ++$clock), wrap it or create a test helper.@method \DateTimeImmutable now() via @mixin or stubs if autocompletion breaks; the interface is sparse by design.ClockInterface (e.g., NtpClock for sync with NTP servers or AppClock with app-specific defaults), then bind it conditionally (e.g., in local vs prod environments).How can I help you explore Laravel packages today?