Skip to content

Commit

Permalink
Change to travelBy
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Feb 3, 2024
1 parent edd58eb commit 39fe10a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 34 deletions.
16 changes: 3 additions & 13 deletions src/DefaultClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,13 @@ public static function travel(Instant $instant): void
}

/**
* Travels forward by a duration, but allows time to continue moving forward from there.
* Travels forward in time by a duration (or backward if the duration is negative).
*
* 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
public static function travelBy(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()));
self::set(new OffsetClock(self::get(), $duration));
}

/**
Expand Down
26 changes: 5 additions & 21 deletions tests/DefaultClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,16 @@ public function testTravelForward(): void
DefaultClock::set($fixedClock);
self::assertInstantIs(1000, 0, Instant::now());

DefaultClock::travelForward(Duration::ofSeconds(1000));
// Travel forward
DefaultClock::travelBy(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);
// Travel backward
DefaultClock::travelBy(Duration::ofSeconds(-1000));
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());
self::assertInstantIs(1002, 0, Instant::now());
}

public function testScale(): void
Expand Down

0 comments on commit 39fe10a

Please sign in to comment.