Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DefaultClock::travelBy(Duration), add travelTo to replace travel #92

Merged
merged 9 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/tools/ecs/*
!/tools/ecs/composer.json
!/tools/ecs/ecs.php
/.idea
francislavoie marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ All objects read the current time from a `Clock` implementation. The following i
- `SystemClock` returns the system time; it's the default clock
- `FixedClock`: returns a pre-configured time
- `OffsetClock`: adds an offset to another clock
- `ScaleClock`: makes another clock fast forward by a scale factor
- `ScaleClock`: makes another clock fast-forward by a scale factor

These classes belong to the `Brick\DateTime\Clock` namespace.

Expand Down Expand Up @@ -127,7 +127,9 @@ DefaultClock::reset(); // do not forget to reset the clock to the system clock!
There are also useful shortcut methods to use clocks in your tests, inspired by [timecop](https://github.com/travisjeffery/timecop):

- `freeze()` freezes time to a specific point in time
- `travel()` travels to a specific point in time, but allows time to continue moving forward from there
- `travel()` travels to an `Instant` in time, but allows time to continue moving forward from there
- `travelForward()` travels forward by a `Duration`
- `travelBackward()` travels backward by a `Duration`
- `scale()` makes time move at a given pace

#### Freeze the time to a specific point
Expand Down
20 changes: 20 additions & 0 deletions src/DefaultClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ public static function travel(Instant $instant): void
self::set(new OffsetClock($clock, $offset));
}

/**
* Travels forward by a duration, but allows time to continue moving forward from there.
*
* If the current default clock is frozen, you must `reset()` it first, or the time will stay frozen.
*/
public static function travelForward(Duration $duration): void
{
self::set(new OffsetClock(self::get(), $duration->abs()));
}

/**
* Travels backward by a duration, but allows time to continue moving forward from there.
*
* If the current default clock is frozen, you must `reset()` it first, or the time will stay frozen.
*/
public static function travelBackward(Duration $duration): void
{
self::set(new OffsetClock(self::get(), $duration->abs()->negated()));
}

/**
* Makes time move at a given pace.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/DefaultClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Brick\DateTime\Clock\FixedClock;
use Brick\DateTime\DefaultClock;
use Brick\DateTime\Duration;
use Brick\DateTime\Instant;

/**
Expand Down Expand Up @@ -37,6 +38,40 @@ public function testTravel(): void
self::assertInstantIs(-998, 0, Instant::now());
}

public function testTravelForward(): void
francislavoie marked this conversation as resolved.
Show resolved Hide resolved
{
$fixedClock = new FixedClock(Instant::of(1000, 0));
DefaultClock::set($fixedClock);
self::assertInstantIs(1000, 0, Instant::now());

DefaultClock::travelForward(Duration::ofSeconds(1000));
self::assertInstantIs(2000, 0, Instant::now());

// Absolute value of the duration
DefaultClock::travelForward(Duration::ofSeconds(-1000));
self::assertInstantIs(3000, 0, Instant::now());

$fixedClock->move(2);
self::assertInstantIs(3002, 0, Instant::now());
}

public function testTravelBackward(): void
{
$fixedClock = new FixedClock(Instant::of(1000, 0));
DefaultClock::set($fixedClock);
self::assertInstantIs(1000, 0, Instant::now());

DefaultClock::travelBackward(Duration::ofSeconds(1000));
self::assertInstantIs(0, 0, Instant::now());

// Absolute value of duration
DefaultClock::travelBackward(Duration::ofSeconds(-1000));
self::assertInstantIs(-1000, 0, Instant::now());

$fixedClock->move(2);
self::assertInstantIs(-998, 0, Instant::now());
}

public function testScale(): void
{
$fixedClock = new FixedClock(Instant::of(1000, 0));
Expand Down
Loading