Files
beste_clock/tests/Clock/FrozenClockTest.php
T
Andreas Heigl 731f4c9651 Implement forward compatible Clock-Interface
The stella-maris/clock package provides an interface based on the
currently proposed status of PSR20. Due to the inactivity of the PSR20 working
group this is a way to already provide interoperability while still
maintaining forward compatibility. When the current status of PSR20 will
be released at one point in time the stella-maris/clock package will
extend the PSR20 interface so that this package becomes immeadiately
PSR20 compatible without any further work necessary. In the long run the
stella-maris/clock package will then be marked deprecated so that people
can then use the PSR20 provided implementation.

Should the implementation of PSR20 change between now and a possible
release then this interface will still exist and a possible
implementation will need more code-changes anyhow so this interface will
still provide some way of interoperability.

The implementation of the PSR20 polyfill has been moved to the
stella-maris/clock interface which can be implemented independentyl from
PSR20 and might allow some interoperability before the working group of
the FIG manages to decide upon something.
2022-04-20 16:21:40 +02:00

110 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Beste\Clock\Tests;
use Beste\Clock\FrozenClock;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use StellaMaris\Clock\ClockInterface;
/**
* @internal
* @coversDefaultClass \Beste\Clock\FrozenClock
*/
final class FrozenClockTest extends TestCase
{
/**
* @test
* @covers ::__construct
* @covers ::at
* @covers ::now
*/
public function itFreezesTime(): void
{
$now = new DateTimeImmutable('2021-03-21 22:16:00');
$clock = FrozenClock::at($now);
self::assertEquals($now, $clock->now());
}
/**
* @test
*
* @uses \Beste\Clock\FrozenClock::__construct
* @uses \Beste\Clock\FrozenClock::fromUTC()
*
* @covers ::now()
*/
public function itReturnsAnEqualNowButNotTheSame(): void
{
$clock = FrozenClock::fromUTC();
$first = $clock->now();
$second = $clock->now();
self::assertEquals($first, $second);
self::assertNotSame($first, $second);
}
/**
* @test
*
* @uses \Beste\Clock\FrozenClock::__construct
* @uses \Beste\Clock\FrozenClock::now
*
* @covers ::withNowFrom
*/
public function itFreezesTimeFromAnotherClock(): void
{
$now = new DateTimeImmutable('now');
$clock = $this->createMock(ClockInterface::class);
$clock
->expects(self::once())
->method('now')
->willReturn($now);
$frozenClock = FrozenClock::withNowFrom($clock);
self::assertEquals($now, $frozenClock->now());
}
/**
* @test
*
* @uses \Beste\Clock\FrozenClock::__construct
* @uses \Beste\Clock\FrozenClock::now
*
* @covers ::fromUTC
*/
public function itFreezesTheCurrentUTCTime(): void
{
$clock = FrozenClock::fromUTC();
self::assertSame('UTC', $clock->now()->getTimezone()->getName());
self::assertEquals($clock->now(), $clock->now());
}
/**
* @test
*
* @uses \Beste\Clock\FrozenClock::__construct
* @uses \Beste\Clock\FrozenClock::at
* @uses \Beste\Clock\FrozenClock::now
*
* @covers ::setTo
*/
public function itCanBeSet(): void
{
$now = new DateTimeImmutable('2021-03-21 18:18:18');
$then = new DateTimeImmutable('2021-03-21 19:19:19');
$clock = FrozenClock::at($now);
$clock->setTo($then);
self::assertEquals($then, $clock->now());
}
}