From 39fe10a437c0e23a0a2683c1c7d532ae83712716 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sat, 3 Feb 2024 18:27:15 -0500 Subject: [PATCH] Change to `travelBy` --- src/DefaultClock.php | 16 +++------------- tests/DefaultClockTest.php | 26 +++++--------------------- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/src/DefaultClock.php b/src/DefaultClock.php index 8999ed1..8238829 100644 --- a/src/DefaultClock.php +++ b/src/DefaultClock.php @@ -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)); } /** diff --git a/tests/DefaultClockTest.php b/tests/DefaultClockTest.php index 0f78c66..f4b5de5 100644 --- a/tests/DefaultClockTest.php +++ b/tests/DefaultClockTest.php @@ -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