crutch/clock-dev

psr/clock 测试实现

v1.0.2 2023-02-18 15:31 UTC

This package is not auto-updated.

Last update: 2024-09-14 23:07:01 UTC


README

psr/clock 测试实现。建议用于测试。

安装

# Only for tests:
composer require --dev crutch/clock-dev
# Or for all project:
composer require crutch/clock-dev

用法

等待时钟

<?php

$format = 'Y-m-d\TH:i:sp';

$clock = new \Crutch\DevClock\ClockWaited(
    new DateTimeZone('UTC'),
    DateTimeImmutable::createFromFormat($format, '2022-01-01T00:00:00Z')
);

var_dump($clock->now()->format($format)); // "2022-01-01T00:00:00Z"
$clock->wait(5);
var_dump($clock->now()->format($format)); // "2022-01-01T00:00:05Z"
$clock->wait(300);
var_dump($clock->now()->format($format)); // "2022-01-01T00:05:05Z"
<?php

$format = 'Y-m-d\TH:i:sp';

$timer = new \Crutch\DevClock\ClockSequence(
    DateTimeImmutable::createFromFormat($format, '2022-01-01T00:00:00Z'),
    new \Crutch\DevClock\Sequence\HardIntervalSequence(5),
    new DateTimeZone('UTC')
);

var_dump($timer->now()->format($format)); // "2022-01-01T00:00:00Z"
var_dump($timer->now()->format($format)); // "2022-01-01T00:00:05Z"
var_dump($timer->now()->format($format)); // "2022-01-01T00:00:10Z"
var_dump($timer->now()->format($format)); // "2022-01-01T00:00:15Z"
var_dump($timer->now()->format($format)); // "2022-01-01T00:00:20Z"