Files
beste_clock/examples/wrapping_clock.php
T
Jérôme Gamez f3edef9cbc Add a WrappingClock
The `WrappingClock` allows using an object with a `now()` method
returning a `DateTimeImmutable` object as a "real" clock.
2022-04-24 15:38:25 +02:00

29 lines
712 B
PHP

<?php
require __DIR__.'/../vendor/autoload.php';
use Beste\Clock\WrappingClock;
// Create a frozen $now so that we can test the wrapping clock.
$now = new DateTimeImmutable('2012-04-24 12:00:00');
// Create an object that is NOT a clock, but has a now() method returning the frozen $now.
$clock = new class($now) {
private \DateTimeImmutable $now;
public function __construct(\DateTimeImmutable $now)
{
$this->now = $now;
}
public function now(): \DateTimeImmutable
{
return $this->now;
}
};
// We can now wrap the object in a clock.
$wrappedClock = WrappingClock::wrapping($clock);
assert($now->format(DATE_ATOM) === $wrappedClock->now()->format(DATE_ATOM));