diff --git a/CHANGELOG.md b/CHANGELOG.md index 53ad5f8..f86b296 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased -For a full diff see [`2.0.1...master`][2.0.1...master]. +For a full diff see [`2.1.0...master`][2.1.0...master]. + +## [`2.1.0`][2.1.0] + +For a full diff see [`2.0.1...2.1.0`][2.0.1...2.1.0]. + +### Added + +* Added possibility to create a `FrozenClock` by freezing a `SystemClock` ([#143]), by [@localheinz] ## [`2.0.1`][2.0.1] @@ -72,11 +80,13 @@ For a full diff see [`36912f6...1.0.0`][36912f6...1.0.0]. [1.0.0]: https://github.com/ergebnis/clock/releases/tag/1.0.0 [2.0.0]: https://github.com/ergebnis/clock/releases/tag/2.0.0 [2.0.1]: https://github.com/ergebnis/clock/releases/tag/2.0.1 +[2.1.0]: https://github.com/ergebnis/clock/releases/tag/2.1.0 [36912f6...1.0.0]: https://github.com/ergebnis/clock/compare/36912f6...1.0.0 [1.0.0...2.0.0]: https://github.com/ergebnis/clock/compare/1.0.0...2.0.0 [2.0.0...2.0.1]: https://github.com/ergebnis/clock/compare/2.0.0...2.0.1 -[2.0.1...master]: https://github.com/ergebnis/clock/compare/2.0.1...master +[2.0.1...2.1.0]: https://github.com/ergebnis/clock/compare/2.0.1...2.1.0 +[2.1.0...master]: https://github.com/ergebnis/clock/compare/2.1.0...master [#1]: https://github.com/ergebnis/clock/pull/1 [#2]: https://github.com/ergebnis/clock/pull/2 @@ -84,6 +94,7 @@ For a full diff see [`36912f6...1.0.0`][36912f6...1.0.0]. [#52]: https://github.com/ergebnis/clock/pull/52 [#53]: https://github.com/ergebnis/clock/pull/53 [#57]: https://github.com/ergebnis/clock/pull/57 +[#143]: https://github.com/ergebnis/clock/pull/143 [@ergebnis]: https://github.com/ergebnis [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index f84bcb9..7b4c57c 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,26 @@ sleep(5); $stillNow = $clock->now(); ``` +Alternatively, create a new frozen clock by freezing a system clock: + +```php +freeze(); + +$now = $clock->now(); + +sleep(5); + +$stillNow = $clock->now(); +``` + ## Changelog Please have a look at [`CHANGELOG.md`](CHANGELOG.md). diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 47118bf..09bcc06 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,2 +1,8 @@ - + + + + assertInstanceOf + + + diff --git a/src/SystemClock.php b/src/SystemClock.php index dce1316..64ec888 100644 --- a/src/SystemClock.php +++ b/src/SystemClock.php @@ -29,4 +29,9 @@ public function now(): \DateTimeImmutable $this->timezone ); } + + public function freeze(): FrozenClock + { + return new FrozenClock($this->now()); + } } diff --git a/test/Unit/SystemClockTest.php b/test/Unit/SystemClockTest.php index 9b82f5c..339785e 100644 --- a/test/Unit/SystemClockTest.php +++ b/test/Unit/SystemClockTest.php @@ -14,6 +14,7 @@ namespace Ergebnis\Clock\Test\Unit; use Ergebnis\Clock\Clock; +use Ergebnis\Clock\FrozenClock; use Ergebnis\Clock\SystemClock; use Ergebnis\Test\Util\Helper; use PHPUnit\Framework; @@ -22,6 +23,8 @@ * @internal * * @covers \Ergebnis\Clock\SystemClock + * + * @uses \Ergebnis\Clock\FrozenClock */ final class SystemClockTest extends Framework\TestCase { @@ -53,4 +56,32 @@ public function testNowReturnsCurrentDateTime(): void self::assertGreaterThanOrEqual($before, $now); self::assertLessThanOrEqual($after, $now); } + + public function testFreezeReturnsFrozenClock(): void + { + $timeZone = new \DateTimeZone('Europe/Berlin'); + + $clock = new SystemClock($timeZone); + + $before = new \DateTimeImmutable( + 'now', + $timeZone + ); + + $frozenClock = $clock->freeze(); + + $after = new \DateTimeImmutable( + 'now', + $timeZone + ); + + self::assertInstanceOf(FrozenClock::class, $frozenClock); + + $now = $frozenClock->now(); + + self::assertGreaterThanOrEqual($before, $now); + self::assertLessThanOrEqual($after, $now); + + self::assertSame($now, $frozenClock->now()); + } }