Download the PHP package sanmai/duoclock without Composer
On this page you can find all versions of the php package sanmai/duoclock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sanmai/duoclock
More information about sanmai/duoclock
Files in sanmai/duoclock
Package duoclock
Short Description PHP time mocking for tests - PSR-20 clock with mockable sleep(), time(), and TimeSpy for PHPUnit testing
License Apache-2.0
Informations about the package duoclock
DuoClock
DuoClock is a PSR-20-compatible clock abstraction. It provides dual time access (DateTimeImmutable, int, float) and mockable sleep functions (sleep, usleep, and more) for testing time-sensitive code.
Features
- Implements
Psr\Clock\ClockInterface. - Provides:
now(): DateTimeImmutabletime(): intmicrotime(): float
- Offers mockable
sleep(),usleep(),nanosleep(), andtime_nanosleep()for test environments. - Provides
getStartTick()andgetEndTick()for measuring elapsed time. - Mockable time methods:
now(),time(), andmicrotime(). - Includes a deterministic
TimeSpyfor testing. - Is minimal, with a lightweight design (depends only on
psr/clock). - Has all classes non-final to allow easy mocking and testing.
Installation
Interfaces
Usage
Real Clock:
Measuring Elapsed Time
TimeSpy, as a testing-time dependency:
Mocking and Spies
The recommended approach is to always use TimeSpy for testing ($clock = new TimeSpy();) because calls to $clock->sleep() and $clock->usleep() do not delay execution even if you do not specifically mock them.
Why DuoClock Exists
PHP now has PSR-20, a standard interface for representing the current time using immutable objects. This interface works well for many applications, but assumes that all time-based code should consume DateTimeImmutable. In practice, testing time-based code often requires mocking and emulating sleep() and usleep(), especially for retry logic, timeout simulations, or rate limiters. You do not want to wait for literal seconds for your sleep() tests to pass! PSR-20 offers no solution for this, which is where DuoClock steps in.
Development
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.