From 3c3d6a66f2d5dff20bd7ad4e8dc82e94f748d5ff Mon Sep 17 00:00:00 2001 From: Abenet Tamiru Date: Mon, 11 Mar 2024 13:55:41 +0100 Subject: [PATCH] [10.x] Allow sync with carbon to be set from fake method (#50450) * feat: allow sync with carbon to be set from fake method * style: add trailing commas --- src/Illuminate/Support/Sleep.php | 5 +++-- tests/Support/SleepTest.php | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index f52fb3e4bfc8..54cefe6334b9 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -319,15 +319,16 @@ protected function pullPending() * Stay awake and capture any attempts to sleep. * * @param bool $value + * @param bool $syncWithCarbon * @return void */ - public static function fake($value = true) + public static function fake($value = true, $syncWithCarbon = false) { static::$fake = $value; static::$sequence = []; static::$fakeSleepCallbacks = []; - static::$syncWithCarbon = false; + static::$syncWithCarbon = $syncWithCarbon; } /** diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php index 6afbd9114ec9..957d3c20c99e 100644 --- a/tests/Support/SleepTest.php +++ b/tests/Support/SleepTest.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Date; use Illuminate\Support\Sleep; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -593,4 +594,40 @@ public function testItCanSyncCarbon() ]); $this->assertSame('2000-01-01 00:05:03', Date::now()->toDateTimeString()); } + + #[TestWith([ + 'syncWithCarbon' => true, + 'datetime' => '2000-01-01 00:05:03', + ])] + #[TestWith([ + 'syncWithCarbon' => false, + 'datetime' => '2000-01-01 00:00:00', + ])] + public function testFakeCanSetSyncWithCarbon(bool $syncWithCarbon, string $datetime) + { + Carbon::setTestNow('2000-01-01 00:00:00'); + Sleep::fake(syncWithCarbon: $syncWithCarbon); + + Sleep::for(5)->minutes() + ->and(3)->seconds(); + + Sleep::assertSequence([ + Sleep::for(303)->seconds(), + ]); + $this->assertSame($datetime, Date::now()->toDateTimeString()); + } + + public function testFakeDoesNotNeedToSyncWithCarbon() + { + 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()); + } }