From 21c61ee8734500784cc9ab4da31403b340218175 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Sun, 13 Mar 2022 02:02:12 -0300 Subject: [PATCH 1/4] [9.x] Adds "freezeTime" helper for tests. --- .../Foundation/Testing/Concerns/InteractsWithTime.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php index bacbce882168..75f3d801ad6f 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php @@ -7,6 +7,17 @@ trait InteractsWithTime { + /** + * Freezes the current time. + * + * @param callable|null $callback + * @return void + */ + public function freezeTime($callback = null) + { + $this->travelTo(Carbon::now()->startOfSecond(), $callback); + } + /** * Begin travelling to another time. * From 8ec569d8a0e5f29b40a9a6e9431be98a1b74c9f8 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Sun, 13 Mar 2022 02:06:28 -0300 Subject: [PATCH 2/4] Removes start of second. Returns callback result, if any. --- .../Foundation/Testing/Concerns/InteractsWithTime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php index 75f3d801ad6f..ba6fefb45e1a 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php @@ -11,11 +11,11 @@ trait InteractsWithTime * Freezes the current time. * * @param callable|null $callback - * @return void + * @return mixed */ public function freezeTime($callback = null) { - $this->travelTo(Carbon::now()->startOfSecond(), $callback); + return $this->travelTo(Carbon::now(), $callback); } /** From fbb0e2e3fa2aade4cec778be5965b4a2845da81c Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Sun, 13 Mar 2022 14:06:40 -0300 Subject: [PATCH 3/4] Adds freezeCurrentSecond for DB timestamps without subsecond precision. --- .../Foundation/Testing/Concerns/InteractsWithTime.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php index ba6fefb45e1a..f64bb8c5f7e4 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php @@ -18,6 +18,17 @@ public function freezeTime($callback = null) return $this->travelTo(Carbon::now(), $callback); } + /** + * Freezes the current second. + * + * @param callable|null $callback + * @return mixed + */ + public function freezeSecond($callback = null) + { + return $this->travelTo(Carbon::now()->startOfSecond(), $callback); + } + /** * Begin travelling to another time. * From 8cad0f2a5cac7604697aed6e3adcb79e1ffd309b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 14 Mar 2022 13:34:33 -0500 Subject: [PATCH 4/4] formatting --- .../Foundation/Testing/Concerns/InteractsWithTime.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php index f64bb8c5f7e4..0dd044664156 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTime.php @@ -8,7 +8,7 @@ trait InteractsWithTime { /** - * Freezes the current time. + * Freeze time. * * @param callable|null $callback * @return mixed @@ -19,7 +19,7 @@ public function freezeTime($callback = null) } /** - * Freezes the current second. + * Freeze time at the beginning of the current second. * * @param callable|null $callback * @return mixed @@ -52,7 +52,7 @@ public function travelTo($date, $callback = null) Carbon::setTestNow($date); if ($callback) { - return tap($callback(), function () { + return tap($callback(Carbon::now()), function () { Carbon::setTestNow(); }); }