From 0493cc1aef59665d2644e2addd73f6fa5a04a26d Mon Sep 17 00:00:00 2001 From: Bradie Tilley <44430471+bradietilley@users.noreply.github.com> Date: Wed, 17 May 2023 19:10:56 +0800 Subject: [PATCH 1/5] feat: add conditional methods to 'Sleep' class --- src/Illuminate/Support/Sleep.php | 29 ++++++++++++++++++++++++ tests/Support/SleepTest.php | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index 101ed3b36f50..9ff57150c09d 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Carbon\CarbonInterval; +use Closure; use DateInterval; use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; @@ -411,4 +412,32 @@ protected function shouldNotSleep() return $this; } + + /** + * Only sleep when the condition is true + * + * @param (\Closure($this): bool)|bool $condition + * @return $this + */ + public function when($condition) + { + $condition = $condition instanceof Closure ? $condition($this) : $condition; + + $this->shouldSleep = (bool) $condition; + + return $this; + } + + /** + * Always sleep unless the condition is true + * + * @param (\Closure($this): bool)|bool $condition + * @return $this + */ + public function unless($condition) + { + $condition = $condition instanceof Closure ? $condition($this) : $condition; + + return $this->when(! $condition); + } } diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php index af73291014f1..48efdb506ebc 100644 --- a/tests/Support/SleepTest.php +++ b/tests/Support/SleepTest.php @@ -463,4 +463,42 @@ public function testItCanReplacePreviouslyDefinedDurations() $sleep->setDuration(500)->milliseconds(); $this->assertSame($sleep->duration->totalMicroseconds, 500000); } + + public function testItCanSleepConditionallyWhen() + { + Sleep::fake(); + + // Control test + Sleep::assertSlept(fn () => true, 0); + Sleep::for(1)->second(); + Sleep::assertSlept(fn () => true, 1); + Sleep::fake(); + Sleep::assertSlept(fn () => true, 0); + + // Reset + Sleep::fake(); + + // Will not sleep if `when()` yields `false` + Sleep::for(1)->second()->when(false); + Sleep::for(1)->second()->when(fn () => false); + + // Will not sleep if `unless()` yields `true` + Sleep::for(1)->second()->unless(true); + Sleep::for(1)->second()->unless(fn () => true); + + // Finish 'do not sleep' tests - assert no sleeping occurred + Sleep::assertSlept(fn () => true, 0); + + // Will sleep if `when()` yields `true` + Sleep::for(1)->second()->when(true); + Sleep::assertSlept(fn () => true, 1); + Sleep::for(1)->second()->when(fn () => true); + Sleep::assertSlept(fn () => true, 2); + + // Will sleep if `unless()` yields `false` + Sleep::for(1)->second()->unless(false); + Sleep::assertSlept(fn () => true, 3); + Sleep::for(1)->second()->unless(fn () => false); + Sleep::assertSlept(fn () => true, 4); + } } From 9ba7da5ca2050ee85c24271fa4fb2577d00b2c65 Mon Sep 17 00:00:00 2001 From: Bradie Tilley <44430471+bradietilley@users.noreply.github.com> Date: Wed, 17 May 2023 19:52:09 +0800 Subject: [PATCH 2/5] style: add dot at end of phpdoc lines --- src/Illuminate/Support/Sleep.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index 9ff57150c09d..ad903502b18e 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -414,7 +414,7 @@ protected function shouldNotSleep() } /** - * Only sleep when the condition is true + * Only sleep when the condition is true. * * @param (\Closure($this): bool)|bool $condition * @return $this @@ -429,7 +429,7 @@ public function when($condition) } /** - * Always sleep unless the condition is true + * Always sleep unless the condition is true. * * @param (\Closure($this): bool)|bool $condition * @return $this From 8a169768674e154167a2a0e871fb9c9472bcda41 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 17 May 2023 09:05:11 -0500 Subject: [PATCH 3/5] formatting --- src/Illuminate/Support/Sleep.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index ad903502b18e..d11feb9e85cd 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -4,7 +4,6 @@ use Carbon\Carbon; use Carbon\CarbonInterval; -use Closure; use DateInterval; use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; @@ -416,14 +415,12 @@ protected function shouldNotSleep() /** * Only sleep when the condition is true. * - * @param (\Closure($this): bool)|bool $condition + * @param (\Closure($this): bool)|bool $condition * @return $this */ public function when($condition) { - $condition = $condition instanceof Closure ? $condition($this) : $condition; - - $this->shouldSleep = (bool) $condition; + $this->shouldSleep = (bool) value($condition, $this); return $this; } @@ -431,13 +428,11 @@ public function when($condition) /** * Always sleep unless the condition is true. * - * @param (\Closure($this): bool)|bool $condition + * @param (\Closure($this): bool)|bool $condition * @return $this */ public function unless($condition) { - $condition = $condition instanceof Closure ? $condition($this) : $condition; - - return $this->when(! $condition); + return $this->when(! value($condition, $this)); } } From f1a16348d1f71f93da512ce2a16285b669fe046f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 17 May 2023 09:06:27 -0500 Subject: [PATCH 4/5] Update Sleep.php --- 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 d11feb9e85cd..577c0eade252 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -426,7 +426,7 @@ public function when($condition) } /** - * Always sleep unless the condition is true. + * Don't sleep when the condition is true. * * @param (\Closure($this): bool)|bool $condition * @return $this From 31f54b1008b000a08d055fa5dd5d380ccba69cf9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 17 May 2023 09:06:54 -0500 Subject: [PATCH 5/5] Update Sleep.php --- src/Illuminate/Support/Sleep.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index 577c0eade252..bb9022990870 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -413,7 +413,7 @@ protected function shouldNotSleep() } /** - * Only sleep when the condition is true. + * Only sleep when the given condition is true. * * @param (\Closure($this): bool)|bool $condition * @return $this @@ -426,7 +426,7 @@ public function when($condition) } /** - * Don't sleep when the condition is true. + * Don't sleep when the given condition is true. * * @param (\Closure($this): bool)|bool $condition * @return $this