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
I created DuoClock as a PSR-20-compatible clock abstraction. It provides dual time access (DateTimeImmutable
, int
, float
) and mockable sleep functions (sleep
, usleep
) for testing time-sensitive code.
Features
I designed DuoClock to:
- Implement
Psr\Clock\ClockInterface
. - Provide:
now(): DateTimeImmutable
time(): int
microtime(): float
- Offer mockable
sleep()
andusleep()
for test environments. - Mockable time methods:
now()
,time()
, andmicrotime()
. - Include a deterministic
TimeSpy
for testing. - Be minimal, with a lightweight design (depends only on
psr/clock
). - Have all classes non-final to allow easy mocking and testing.
Installation
Interfaces
Usage
Real Clock:
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.