From c35d76eb3bb4e6f6c64a82b68ab8666a7a292a48 Mon Sep 17 00:00:00 2001 From: Marvin Quezon Date: Wed, 18 Nov 2020 21:51:32 +0800 Subject: [PATCH] [8.x] Adds an ability for the Wormhole class to take us back to the present. (#35261) * Adds an ability for the Wormhole to take us back to the present. * Fixes styling. --- .../Testing/Concerns/InteractsWithTime.php | 4 +-- .../Foundation/Testing/Wormhole.php | 12 +++++++++ tests/Foundation/Testing/WormholeTest.php | 25 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 tests/Foundation/Testing/WormholeTest.php diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php index d6413a528062..184a2441ce86 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php @@ -44,8 +44,6 @@ public function travelTo(DateTimeInterface $date, $callback = null) */ public function travelBack() { - Carbon::setTestNow(); - - return Carbon::now(); + return Wormhole::back(); } } diff --git a/src/Illuminate/Foundation/Testing/Wormhole.php b/src/Illuminate/Foundation/Testing/Wormhole.php index 59512981e6ad..d660fe026a75 100644 --- a/src/Illuminate/Foundation/Testing/Wormhole.php +++ b/src/Illuminate/Foundation/Testing/Wormhole.php @@ -115,6 +115,18 @@ public function years($callback = null) return $this->handleCallback($callback); } + /** + * Travel back to the current time. + * + * @return \DateTimeInterface + */ + public static function back() + { + Carbon::setTestNow(); + + return Carbon::now(); + } + /** * Handle the given optional execution callback. * diff --git a/tests/Foundation/Testing/WormholeTest.php b/tests/Foundation/Testing/WormholeTest.php new file mode 100644 index 000000000000..baf41f059132 --- /dev/null +++ b/tests/Foundation/Testing/WormholeTest.php @@ -0,0 +1,25 @@ +addDays(10); + + // Travel in time.. + (new Wormhole(10))->days(); + + // Assert we are now in the future.. + $this->assertEquals($future->format('Y-m-d'), now()->format('Y-m-d')); + + // Assert we can go back to the present.. + $this->assertEquals($present->format('Y-m-d'), Wormhole::back()->format('Y-m-d')); + } +}