From 9bf7f0a814410063c59b741c7ff8764ff946ec2b Mon Sep 17 00:00:00 2001 From: Bradie Tilley <44430471+bradietilley@users.noreply.github.com> Date: Mon, 15 May 2023 20:52:06 +0800 Subject: [PATCH 1/4] feat: add `Conditionable` and `Macroable` traits to Sleep --- src/Illuminate/Support/Sleep.php | 5 ++++ tests/Support/SleepTest.php | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index 041535b9b42f..2cd6420f441c 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -5,11 +5,16 @@ use Carbon\Carbon; use Carbon\CarbonInterval; use DateInterval; +use Illuminate\Support\Traits\Conditionable; +use Illuminate\Support\Traits\Macroable; use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; class Sleep { + use Conditionable; + use Macroable; + /** * The total duration to sleep. * diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php index 13c7ed4d9d3b..e343314e56ca 100644 --- a/tests/Support/SleepTest.php +++ b/tests/Support/SleepTest.php @@ -414,4 +414,54 @@ public function testAssertSlept() $this->assertSame("The expected sleep was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage()); } } + + public function testItCanCreateMacrosViaMacroable() + { + Sleep::fake(); + + Sleep::macro('orSomeConfiguredAmountOfTime', function (): Sleep { + /** @var Sleep $this */ + return $this->for(1.234)->seconds(); + }); + + Sleep::macro('andSomeConfiguredAmountOfTime', function (): Sleep { + /** @var Sleep $this */ + return $this->and(567)->microseconds(); + }); + + // Sanity check (1 second default) + $sleep = Sleep::for(1)->second(); + $this->assertSame($sleep->duration->totalMicroseconds, 1000000); + + // A macro can specify a new duration + $sleep = $sleep->orSomeConfiguredAmountOfTime(); + $this->assertSame($sleep->duration->totalMicroseconds, 1234000); + + // A macro can supplement an existing duration + $sleep = $sleep->andSomeConfiguredAmountOfTime(); + $this->assertSame($sleep->duration->totalMicroseconds, 1234567); + } + + public function testItCanCreateConditionallyDefineSleepsViaConditionable() + { + Sleep::fake(); + + $sleep = Sleep::for(1) + ->second() + ->when( + true, + fn (Sleep $sleep) => $sleep->and(2)->milliseconds(), + fn (Sleep $sleep) => $sleep->and(3)->milliseconds(), + ); + $this->assertSame($sleep->duration->totalMicroseconds, 1002000); + + $sleep = Sleep::for(1) + ->second() + ->when( + false, + fn (Sleep $sleep) => $sleep->and(2)->milliseconds(), + fn (Sleep $sleep) => $sleep->and(3)->milliseconds(), + ); + $this->assertSame($sleep->duration->totalMicroseconds, 1003000); + } } From a5ee85262a9b72caccb0cf6241d7052dc2088479 Mon Sep 17 00:00:00 2001 From: Bradie Tilley <44430471+bradietilley@users.noreply.github.com> Date: Mon, 15 May 2023 21:12:20 +0800 Subject: [PATCH 2/4] refactor: allow replacing sleep duration via a new `->duration()` method --- src/Illuminate/Support/Sleep.php | 40 +++++++++++++++++++++----------- tests/Support/SleepTest.php | 20 +++++++++------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/Illuminate/Support/Sleep.php b/src/Illuminate/Support/Sleep.php index 2cd6420f441c..d4a904368ad9 100644 --- a/src/Illuminate/Support/Sleep.php +++ b/src/Illuminate/Support/Sleep.php @@ -58,19 +58,7 @@ class Sleep */ public function __construct($duration) { - if (! $duration instanceof DateInterval) { - $this->duration = CarbonInterval::microsecond(0); - - $this->pending = $duration; - } else { - $duration = CarbonInterval::instance($duration); - - if ($duration->totalMicroseconds < 0) { - $duration = CarbonInterval::seconds(0); - } - - $this->duration = $duration; - } + $this->duration($duration); } /** @@ -121,6 +109,32 @@ public static function sleep($duration) return (new static($duration))->seconds(); } + /** + * Sleep for the given duration. Replaces any previously defined durations. + * + * @param \DateInterval|int|float $duration + * @return $this + */ + public function duration($duration) + { + if (! $duration instanceof DateInterval) { + $this->duration = CarbonInterval::microsecond(0); + + $this->pending = $duration; + } else { + $duration = CarbonInterval::instance($duration); + + if ($duration->totalMicroseconds < 0) { + $duration = CarbonInterval::seconds(0); + } + + $this->duration = $duration; + $this->pending = null; + } + + return $this; + } + /** * Sleep for the given number of minutes. * diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php index e343314e56ca..0bf5d7f7e9c8 100644 --- a/tests/Support/SleepTest.php +++ b/tests/Support/SleepTest.php @@ -419,26 +419,30 @@ public function testItCanCreateMacrosViaMacroable() { Sleep::fake(); - Sleep::macro('orSomeConfiguredAmountOfTime', function (): Sleep { + Sleep::macro('forSomeConfiguredAmountOfTime', static function () { + return Sleep::for(3)->seconds(); + }); + + Sleep::macro('useSomeOtherAmountOfTime', function () { /** @var Sleep $this */ - return $this->for(1.234)->seconds(); + return $this->duration(1.234)->seconds(); }); - Sleep::macro('andSomeConfiguredAmountOfTime', function (): Sleep { + Sleep::macro('andSomeMoreGranularControl', function () { /** @var Sleep $this */ return $this->and(567)->microseconds(); }); - // Sanity check (1 second default) - $sleep = Sleep::for(1)->second(); - $this->assertSame($sleep->duration->totalMicroseconds, 1000000); + // A static macro can be referenced + $sleep = Sleep::forSomeConfiguredAmountOfTime(); + $this->assertSame($sleep->duration->totalMicroseconds, 3000000); // A macro can specify a new duration - $sleep = $sleep->orSomeConfiguredAmountOfTime(); + $sleep = $sleep->useSomeOtherAmountOfTime(); $this->assertSame($sleep->duration->totalMicroseconds, 1234000); // A macro can supplement an existing duration - $sleep = $sleep->andSomeConfiguredAmountOfTime(); + $sleep = $sleep->andSomeMoreGranularControl(); $this->assertSame($sleep->duration->totalMicroseconds, 1234567); } From e11f3b0f09379d07cd3e18c1eecf291584a4f4a6 Mon Sep 17 00:00:00 2001 From: Bradie Tilley <44430471+bradietilley@users.noreply.github.com> Date: Mon, 15 May 2023 21:19:34 +0800 Subject: [PATCH 3/4] test: the `duration()` method will replace previously defined durations --- tests/Support/SleepTest.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php index 0bf5d7f7e9c8..658a3bf44cd3 100644 --- a/tests/Support/SleepTest.php +++ b/tests/Support/SleepTest.php @@ -446,7 +446,7 @@ public function testItCanCreateMacrosViaMacroable() $this->assertSame($sleep->duration->totalMicroseconds, 1234567); } - public function testItCanCreateConditionallyDefineSleepsViaConditionable() + public function testItCanCreateConditionallyDefinedDurationsViaConditionable() { Sleep::fake(); @@ -468,4 +468,18 @@ public function testItCanCreateConditionallyDefineSleepsViaConditionable() ); $this->assertSame($sleep->duration->totalMicroseconds, 1003000); } + + public function testItCanReplacePreviouslyDefinedDurations() + { + Sleep::fake(); + + $sleep = Sleep::for(1)->second(); + $this->assertSame($sleep->duration->totalMicroseconds, 1000000); + + $sleep->duration(2)->second(); + $this->assertSame($sleep->duration->totalMicroseconds, 2000000); + + $sleep->duration(500)->milliseconds(); + $this->assertSame($sleep->duration->totalMicroseconds, 500000); + } } From d24cd9f5f783b0c4884eeac7171786cdd734002a Mon Sep 17 00:00:00 2001 From: Bradie Tilley <44430471+bradietilley@users.noreply.github.com> Date: Mon, 15 May 2023 21:27:32 +0800 Subject: [PATCH 4/4] style: indentation --- tests/Support/SleepTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php index 658a3bf44cd3..aef5012abea0 100644 --- a/tests/Support/SleepTest.php +++ b/tests/Support/SleepTest.php @@ -456,7 +456,7 @@ public function testItCanCreateConditionallyDefinedDurationsViaConditionable() true, fn (Sleep $sleep) => $sleep->and(2)->milliseconds(), fn (Sleep $sleep) => $sleep->and(3)->milliseconds(), - ); + ); $this->assertSame($sleep->duration->totalMicroseconds, 1002000); $sleep = Sleep::for(1) @@ -465,7 +465,7 @@ public function testItCanCreateConditionallyDefinedDurationsViaConditionable() false, fn (Sleep $sleep) => $sleep->and(2)->milliseconds(), fn (Sleep $sleep) => $sleep->and(3)->milliseconds(), - ); + ); $this->assertSame($sleep->duration->totalMicroseconds, 1003000); }