https://github.com/raigu/psr20-clock-testdoubles
Test Doubles for PSR-20 clock
https://github.com/raigu/psr20-clock-testdoubles
clock double php psr-20 stub test time
Last synced: 5 months ago
JSON representation
Test Doubles for PSR-20 clock
- Host: GitHub
- URL: https://github.com/raigu/psr20-clock-testdoubles
- Owner: raigu
- License: mit
- Created: 2021-08-29T17:54:40.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-02T18:26:10.000Z (almost 5 years ago)
- Last Synced: 2024-04-22T07:21:12.277Z (about 2 years ago)
- Topics: clock, double, php, psr-20, stub, test, time
- Language: PHP
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/raigu/psr20-clock-testdoubles)
[](https://packagist.org/packages/raigu/psr20-clock-testdoubles)
[](https://semver.org/spec/v2.0.0.html)
[](https://github.com/raigu/psr20-clock-testdoubles/actions)
[](https://codecov.io/gh/raigu/psr20-clock-testdoubles)
[](https://dashboard.stryker-mutator.io/reports/github.com/raigu/psr20-clock-testdoubles/main)
[](LICENSE)
Test Doubles for PSR-20 clock
Unstable because the PSR-20 is not officially released yet.
# Compatibility
* PHP 7.4, ^8.0
* psr/clock==@dev
# Changes
[./CHANGELOG.md](./CHANGELOG.md)
# Install
```shell
$ composer require --dev raigu/psr20-clock-testdoubles
```
# Test Doubles
## FrozenClock
Clock frozen in time. It always returns the same time now matter how many times the `now` is called.
```php
// creating with current system date and time
$clock = new \Raigu\TestDouble\Psr20\FrozenClock;
// creating with predefined date and time at
$clock = new \Raigu\TestDouble\Psr20\FrozenClock(
new DateTimeImmutable('2020-01-02')
);
```
## TimeTravelClock
Clock that acts like normal clock but has a shift in time if given.
```php
// By default, acts like normal clock.
$clock = new \Raigu\TestDouble\Psr20\TimeTravelClock();
$moment = $clock->now();
sleep(2);
// after two seconds the TimeTravelClock is also moved forward.
assert($moment->add(new DateInterval('PT2S'))->getTimestamp() === $clock->now()->getTimestamp());
// Moving to the specific date and time in the future or past:
$clock->travelInTime(new DateTimeImmutable('2020-01-02'));
assert($clock->now()->format('Y-m-d') === '2020-01-02');
// Move by the specific interval to the future
$clock->travelInTimeByInterval(new DateInterval('P10D'));
assert($clock->now()->format('Y-m-d') === '2020-01-12');
// Move by the specific interval to the past
$clock->travelInTimeByInterval(DateInterval::createFromDateString('-1 day'));
assert($clock->now()->format('Y-m-d') === '2020-01-11');
```
The `TimeTravelClock` implements the Decorator pattern by wrapping
a clock implementation. By default, it uses a system clock implementation,
but it can be replaced in constructor.
For example, it is possible to create TimeTravelClock that is frozen by using `FrozenClock`:
```php
$clock = new \Raigu\TestDouble\Psr20\TimeTravelClock(
new Raigu\TestDouble\Psr20\FrozenClock()
);
$moment = $clock->now();
sleep(2);
assert($moment->getTimestamp() === $clock->getTimestamp(), 'Does not tick because base clock is frozen.');
```
# Testing
```shell
$ composer test
$ composer specification
$ composer coverage
```