Skip to content

Commit

Permalink
Merge pull request #143 from ergebnis/feature/freeze
Browse files Browse the repository at this point in the history
Enhancement: Allow creation of FrozenClock by freezing SystemClock
  • Loading branch information
ergebnis-bot authored May 4, 2020
2 parents 6b3795c + ec7b030 commit 6feefb7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -72,18 +80,21 @@ 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
[#41]: https://github.com/ergebnis/clock/pull/41
[#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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ sleep(5);
$stillNow = $clock->now();
```

Alternatively, create a new frozen clock by freezing a system clock:

```php
<?php

use Ergebnis\Clock;

$timeZone = new \DateTimeZone('Europe/Berlin');

$clock = new Clock\SystemClock($timeZone);

$frozenClock = $clock->freeze();

$now = $clock->now();

sleep(5);

$stillNow = $clock->now();
```

## Changelog

Please have a look at [`CHANGELOG.md`](CHANGELOG.md).
Expand Down
8 changes: 7 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.10.1@eeed5ecccc10131397f0eb7ee6da810c0be3a7fc"/>
<files psalm-version="3.11.2@d470903722cfcbc1cd04744c5491d3e6d13ec3d9">
<file src="test/Unit/SystemClockTest.php">
<RedundantCondition occurrences="1">
<code>assertInstanceOf</code>
</RedundantCondition>
</file>
</files>
5 changes: 5 additions & 0 deletions src/SystemClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public function now(): \DateTimeImmutable
$this->timezone
);
}

public function freeze(): FrozenClock
{
return new FrozenClock($this->now());
}
}
31 changes: 31 additions & 0 deletions test/Unit/SystemClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,8 @@
* @internal
*
* @covers \Ergebnis\Clock\SystemClock
*
* @uses \Ergebnis\Clock\FrozenClock
*/
final class SystemClockTest extends Framework\TestCase
{
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 6feefb7

Please sign in to comment.