sanmai/duoclock
Composer 安装命令:
composer require sanmai/duoclock
包简介
PHP time mocking for tests - PSR-20 clock with mockable sleep(), time(), and TimeSpy for PHPUnit testing
README 文档
README
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
composer require sanmai/duoclock
Interfaces
namespace DuoClock; interface DuoClockInterface { public function time(): int; public function microtime(): float; } interface SleeperInterface { public function sleep(int $seconds): int; public function usleep(int $microseconds): void; } interface NanoSleeperInterface { public function time_nanosleep(int $seconds, int $nanoseconds): array|bool; public function nanosleep(int $nanoseconds): array|bool; } interface TickerInterface { public function getStartTick(): float; public function getEndTick(): float; }
Usage
Real Clock:
$clock = new DuoClock\DuoClock(); $clock->now(); // DateTimeImmutable $clock->time(); // int $clock->microtime(); // float $clock->sleep(1); // real sleep $clock->usleep(1000); // real micro-sleep $clock->nanosleep(1_500_000_000); // sleep 1.5 seconds $clock->time_nanosleep(1, 500_000_000); // same as above
Measuring Elapsed Time
$clock = new DuoClock\DuoClock(); $timer = $clock->getStartTick(); // ...work... $timer += $clock->getEndTick(); // $timer now contains elapsed seconds as float
TimeSpy, as a testing-time dependency:
$clock = new DuoClock\TimeSpy(1752321600); // Corresponds to '2025-07-12T12:00:00Z' $clock->time(); // 1752321600 $clock->sleep(10); // advances virtual clock by 10 seconds $clock->usleep(5000); // advances virtual clock by 0.005 seconds $clock->time(); // 1752321610 $clock->microtime(); // 1752321610.005
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.
$mock = $this->createMock(DuoClock\TimeSpy::class); $mock->expects($this->exactly(1)) ->method('time') ->willReturn(self::TIME_BEFORE_LAUNCH); $example = new ExampleUsingTime($mock); $this->assertFalse($example->launch());
$mock = $this->createMock(DuoClock\TimeSpy::class); $mock->expects($this->exactly(1)) ->method('usleep') ->with(self::POLL_TIME); $example = new ExampleUsingSleep($mock); $example->waitDuringPolling();
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
# Run all checks (tests, static analysis, mutation testing)
make -j -k
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
sanmai/duoclock 适用场景与选型建议
sanmai/duoclock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.68M 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sanmai/duoclock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sanmai/duoclock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.68M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 37
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2025-07-12