From 083f05177af9c5a5f8e21d612cb9e615d494df9b Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 7 Mar 2024 10:11:50 +1100 Subject: [PATCH 1/3] Use support carbon --- src/Illuminate/Support/Sleep.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index cd32d7f479c5..6a7ca23b4864 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -2,9 +2,9 @@ namespace Illuminate\Support; -use Carbon\Carbon; use Carbon\CarbonInterval; use DateInterval; +use Illuminate\Support\Carbon; use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; From b4c6921947a9ae486d272abc7809d813424f3f73 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 7 Mar 2024 10:58:59 +1100 Subject: [PATCH 2/3] Allow syncing of carbon with sleep --- src/Illuminate/Support/Sleep.php | 22 ++++++++++++++++++++++ tests/Support/SleepTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index 6a7ca23b4864..949998ad6cbd 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -20,6 +20,13 @@ class Sleep */ public static $fakeSleepCallbacks = []; + /** + * Keep carbon's "now" in sync when sleeping. + * + * @var bool + */ + protected static $syncWithCarbon = false; + /** * The total duration to sleep. * @@ -259,6 +266,10 @@ public function __destruct() if (static::$fake) { static::$sequence[] = $this->duration; + if (static::$syncWithCarbon) { + Carbon::setTestNow(Carbon::now()->add($this->duration)); + } + foreach (static::$fakeSleepCallbacks as $callback) { $callback($this->duration); } @@ -317,6 +328,7 @@ public static function fake($value = true) static::$sequence = []; static::$fakeSleepCallbacks = []; + static::$syncWithCarbon = false; } /** @@ -458,4 +470,14 @@ public static function whenFakingSleep($callback) { static::$fakeSleepCallbacks[] = $callback; } + + /** + * Indicate that Carbon's "now" should be kept in sync when sleeping. + * + * @return void + */ + public static function syncWithCarbon($value = true) + { + static::$syncWithCarbon = $value; + } } diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php index d43b96a0d7f7..6afbd9114ec9 100644 --- a/tests/Support/SleepTest.php +++ b/tests/Support/SleepTest.php @@ -5,6 +5,7 @@ use Carbon\CarbonInterval; use Exception; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Date; use Illuminate\Support\Sleep; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\TestCase; @@ -563,4 +564,33 @@ public function testItDoesntRunCallbacksWhenNotFaking() $this->assertTrue(true); } + + public function testItDoesNotSyncCarbon() + { + Carbon::setTestNow('2000-01-01 00:00:00'); + Sleep::fake(); + + Sleep::for(5)->minutes() + ->and(3)->seconds(); + + Sleep::assertSequence([ + Sleep::for(303)->seconds(), + ]); + $this->assertSame('2000-01-01 00:00:00', Date::now()->toDateTimeString()); + } + + public function testItCanSyncCarbon() + { + Carbon::setTestNow('2000-01-01 00:00:00'); + Sleep::fake(); + Sleep::syncWithCarbon(); + + Sleep::for(5)->minutes() + ->and(3)->seconds(); + + Sleep::assertSequence([ + Sleep::for(303)->seconds(), + ]); + $this->assertSame('2000-01-01 00:05:03', Date::now()->toDateTimeString()); + } } From db4c34e345fe1521469f9cf8ee760d8a7cdfeb60 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 7 Mar 2024 11:03:30 +1100 Subject: [PATCH 3/3] Fix capitalization --- src/Illuminate/Support/Sleep.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index 949998ad6cbd..f54ec2a45cf8 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -21,7 +21,7 @@ class Sleep public static $fakeSleepCallbacks = []; /** - * Keep carbon's "now" in sync when sleeping. + * Keep Carbon's "now" in sync when sleeping. * * @var bool */