From e129efdb56bd86899d1b9dc08a14c342e72e1213 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 27 Apr 2023 10:09:41 +1000 Subject: [PATCH 01/25] wip --- index.php | 35 ++++++ src/Illuminate/Support/Pause.php | 208 +++++++++++++++++++++++++++++++ tests/Support/PauseTest.php | 114 +++++++++++++++++ 3 files changed, 357 insertions(+) create mode 100644 index.php create mode 100644 src/Illuminate/Support/Pause.php create mode 100644 tests/Support/PauseTest.php diff --git a/index.php b/index.php new file mode 100644 index 000000000000..07e25ed57432 --- /dev/null +++ b/index.php @@ -0,0 +1,35 @@ +bar()->baz(); +echo 'done'; diff --git a/src/Illuminate/Support/Pause.php b/src/Illuminate/Support/Pause.php new file mode 100644 index 000000000000..766e0c17f1e0 --- /dev/null +++ b/src/Illuminate/Support/Pause.php @@ -0,0 +1,208 @@ +duration = CarbonInterval::instance($duration); + + $this->pending = 0; + } else { + $this->duration = CarbonInterval::seconds(0); + + $this->pending = $duration; + } + } + + /** + * Pause for the given duration. + * + * @param int|float|DateInterval $duration + * @return static + */ + public static function for($duration) + { + return new static($duration); + } + + public static function until($timestamp) + { + // + } + + /** + * Pause execution for the given number of minutes. + * + * @return $this + */ + public function minutes() + { + $this->duration->addMinutes($this->pending); + + $this->pending = 0; + + return $this; + } + + /** + * Pause execution for the given number of minutes. + * + * @return $this + */ + public function minute() + { + return $this->minutes(); + } + + /** + * Pause execution for the given number of seconds. + */ + public function seconds() + { + $this->duration->addSeconds($this->pending); + + $this->pending = 0; + + return $this; + } + + /** + * Pause execution for the given number of seconds. + * + * @return $this + */ + public function second() + { + return $this->seconds(); + } + + /** + * Pause execution for the given number of milliseconds. + * + * @return $this + */ + public function milliseconds() + { + $this->duration->addMilliseconds($this->pending); + + $this->pending = 0; + + return $this; + } + + /** + * Pause execution for the given number of milliseconds. + * + * @return $this + */ + public function millisecond() + { + return $this->milliseconds(); + } + + /** + * Pause execution for the given number of microseconds. + * + * @return $this + */ + public function microseconds() + { + $this->duration->addMicroseconds($this->pending); + + $this->pending = 0; + + return $this; + } + + /** + * Pause execution for the given number of microseconds. + * + * @return $this + */ + public function microsecond() + { + return $this->microseconds(); + } + + /** + * Add additional time to the execution pause. + * + * @param int|float $duration + * @return $this + */ + public function and($duration) + { + $this->pending = $duration; + + return $this; + } + + /** + * Handle the object's destruction. + * + * @return void + */ + public function __destruct() + { + $remaining = $this->duration->copy(); + + if ((int) $remaining->totalSeconds > 0) { + static::sleep((int) $remaining->totalSeconds); + + $remaining = $remaining->subSeconds((int) $remaining->totalSeconds); + } + + + if ($remaining->totalMicroseconds > 0) { + static::usleep($remaining->totalMicroseconds); + } + } + + /** + * Pause execution for the given duration in microseconds. + * + * @param int $duration + * @return void + */ + public static function usleep($duration) + { + usleep($duration); + } + + /** + * Pause execution for the given duration in seconds. + * + * @param int $duration + * @return void + */ + public static function sleep($duration) + { + sleep($duration); + } +} diff --git a/tests/Support/PauseTest.php b/tests/Support/PauseTest.php new file mode 100644 index 000000000000..b6d7c62b357b --- /dev/null +++ b/tests/Support/PauseTest.php @@ -0,0 +1,114 @@ +seconds(); + $end = Carbon::now(); + + $this->assertTrue($start->toImmutable()->addSecond()->isBefore($end)); + $this->assertTrue($start->toImmutable()->addSecond()->addMilliseconds(20)->isAfter($end)); + } + + public function testItSleepsForSecondsWithMilliseconds() + { + $start = Carbon::now(); + Pause::for(1.5)->seconds(); + $end = Carbon::now(); + + $this->assertTrue($start->toImmutable()->addSecond()->addMilliseconds(500)->isBefore($end)); + $this->assertTrue($start->toImmutable()->addSecond()->addMilliseconds(520)->isAfter($end)); + } + + public function testItCanSpecifyMinutes() + { + $pause = Pause::for(1.5)->minutes(); + + $this->assertSame($pause->duration->totalSeconds, 90); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanSpecifySeconds() + { + $pause = Pause::for(5)->seconds(); + + $this->assertSame($pause->duration->totalSeconds, 5); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanSpecifySecond() + { + $pause = Pause::for(1)->second(); + + $this->assertSame($pause->duration->totalSeconds, 1); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanSpecifyMilliseconds() + { + $pause = Pause::for(5)->milliseconds(); + + $this->assertSame($pause->duration->totalMilliseconds, 5); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanSpecifyMillisecond() + { + $pause = Pause::for(1)->milliseconds(); + + $this->assertSame($pause->duration->totalMilliseconds, 1); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanSpecifyMicroseconds() + { + $pause = Pause::for(5)->microseconds(); + + $this->assertSame($pause->duration->totalMicroseconds, 5); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanSpecifyMicrosecond() + { + $pause = Pause::for(1)->millisecond(); + + $this->assertSame($pause->duration->totalMilliseconds, 1); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanChainDurations() + { + $pause = Pause::for(1)->second()->and(500)->microseconds(); + + $this->assertSame($pause->duration->totalMicroseconds, 1000500); + + $pause->duration = CarbonInterval::seconds(0); + } + + public function testItCanPassDateInterval() + { + $pause = Pause::for(CarbonInterval::seconds(3)); + + $this->assertSame($pause->duration->totalSeconds, 3); + + $pause->duration = CarbonInterval::seconds(0); + } +} From 478242889f7f3ed29e5d64f53cf7c6e071c8ef79 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 27 Apr 2023 10:27:16 +1000 Subject: [PATCH 02/25] wip --- src/Illuminate/Support/Pause.php | 41 ++++++++++++++++++++++++++++++++ tests/Support/PauseTest.php | 26 ++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/Illuminate/Support/Pause.php b/src/Illuminate/Support/Pause.php index 766e0c17f1e0..90d047bf003a 100644 --- a/src/Illuminate/Support/Pause.php +++ b/src/Illuminate/Support/Pause.php @@ -4,6 +4,7 @@ use Carbon\CarbonInterval; use DateInterval; +use RuntimeException; class Pause { @@ -21,6 +22,20 @@ class Pause */ protected $pending; + /** + * Indicate that all sleeping should be faked. + * + * @var bool + */ + protected static $fake = false; + + /** + * The sequence of pauses while faking. + * + * @var bool + */ + protected static $pauseSequence = []; + /** * Create a new Pause instance. * @@ -170,6 +185,16 @@ public function and($duration) */ public function __destruct() { + if ($this->pending !== 0) { + throw new RuntimeException('Unknown pause time unit.'); + } + + if (static::$fake) { + static::$pauseSequence = $this->duration; + + return; + } + $remaining = $this->duration->copy(); if ((int) $remaining->totalSeconds > 0) { @@ -205,4 +230,20 @@ public static function sleep($duration) { sleep($duration); } + + /** + * Capture all pauses for testing. + * + * @param bool $value + * @return void + */ + public static function fake($value = true) + { + static::$fake = $value; + } + + public static function assertSequence($sequence) + { + // + } } diff --git a/tests/Support/PauseTest.php b/tests/Support/PauseTest.php index b6d7c62b357b..5b265cd330e1 100644 --- a/tests/Support/PauseTest.php +++ b/tests/Support/PauseTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Pause; use PHPUnit\Framework\TestCase; +use RuntimeException; class PauseTest extends TestCase { @@ -111,4 +112,29 @@ public function testItCanPassDateInterval() $pause->duration = CarbonInterval::seconds(0); } + + public function testItThrowsOnUnknownTimeUnit() + { + try { + Pause::for(5); + $this->fail(); + } catch (RuntimeException $e) { + $this->assertSame('Unknown pause time unit.', $e->getMessage()); + } + } + + public function testItCanFakeSleep() + { + Pause::fake(); + + $start = Carbon::now(); + Pause::for(5)->seconds(); + $end = Carbon::now(); + + $this->assertTrue($start->toImmutable()->addMilliseconds(20)->isAfter($end)); + + Pause::assertSequence([ + Pause::for(5)->seconds() + ]); + } } From 089b262f2e71c14f6474f868a469647370dc2aa9 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 27 Apr 2023 10:28:50 +1000 Subject: [PATCH 03/25] wip --- src/Illuminate/Support/Pause.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Pause.php b/src/Illuminate/Support/Pause.php index 90d047bf003a..a00a4a6785f6 100644 --- a/src/Illuminate/Support/Pause.php +++ b/src/Illuminate/Support/Pause.php @@ -190,7 +190,9 @@ public function __destruct() } if (static::$fake) { - static::$pauseSequence = $this->duration; + if ($this->capture) { + static::$pauseSequence[] = $this->duration; + } return; } @@ -244,6 +246,7 @@ public static function fake($value = true) public static function assertSequence($sequence) { + // set all to no capture. // } } From b61d704202f8f84d05f1728d5ff56c6ca5b29326 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 27 Apr 2023 11:16:45 +1000 Subject: [PATCH 04/25] wip --- src/Illuminate/Support/Pause.php | 25 +++++++++++++++++---- tests/Support/PauseTest.php | 38 ++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/Pause.php b/src/Illuminate/Support/Pause.php index a00a4a6785f6..32bba1427838 100644 --- a/src/Illuminate/Support/Pause.php +++ b/src/Illuminate/Support/Pause.php @@ -4,6 +4,7 @@ use Carbon\CarbonInterval; use DateInterval; +use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; class Pause @@ -190,9 +191,9 @@ public function __destruct() } if (static::$fake) { - if ($this->capture) { + // if ($this->capture) { static::$pauseSequence[] = $this->duration; - } + // } return; } @@ -242,11 +243,27 @@ public static function sleep($duration) public static function fake($value = true) { static::$fake = $value; + + static::$pauseSequence = []; } public static function assertSequence($sequence) { - // set all to no capture. - // + collect($sequence) + ->zip(static::$pauseSequence) + ->eachSpread(function (?Pause $expected, ?CarbonInterval $actual) { + if ($expected === null) { + return false; + } + + if ($actual === null) { + // PHPUnit::fail('Expected '); + } + + PHPUnit::assertTrue( + $expected->duration->equalTo($actual), + "Expected pause of [{$expected->duration->forHumans()}] but instead found pause of [{$actual->forHumans()}]." + ); + }); } } diff --git a/tests/Support/PauseTest.php b/tests/Support/PauseTest.php index 5b265cd330e1..0d9ce1f8a1a8 100644 --- a/tests/Support/PauseTest.php +++ b/tests/Support/PauseTest.php @@ -5,12 +5,18 @@ use Carbon\CarbonInterval; use Illuminate\Support\Carbon; use Illuminate\Support\Pause; +use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\TestCase; use RuntimeException; class PauseTest extends TestCase { - // all tests have a 20 milliseconds leeway. + protected function setUp(): void + { + parent::setUp(); + + Pause::fake(false); + } public function testItSleepForSeconds() { @@ -132,9 +138,37 @@ public function testItCanFakeSleep() $end = Carbon::now(); $this->assertTrue($start->toImmutable()->addMilliseconds(20)->isAfter($end)); + } + + public function testItCanAssertPauseSequences() + { + Pause::fake(); + + Pause::for(5)->seconds(); + Pause::for(1)->seconds()->and(5)->microsecond(); Pause::assertSequence([ - Pause::for(5)->seconds() + Pause::for(5)->seconds(), + Pause::for(1)->seconds()->and(5)->microsecond(), ]); } + + public function testItCanFailAssertions() + { + Pause::fake(); + + Pause::for(5)->seconds(); + Pause::for(1)->seconds()->and(5)->microsecond(); + + try { + Pause::assertSequence([ + Pause::for(5)->seconds(), + Pause::for(5)->seconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected pause of [5 seconds] but instead found pause of [1 second].\nFailed asserting that false is true.", $e->getMessage()); + } + } } + From 8fbda40c4e73d0816a56ece146aea785d87b99ed Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 27 Apr 2023 12:11:24 +1000 Subject: [PATCH 05/25] wip --- src/Illuminate/Support/Pause.php | 55 +++++++++++++------ tests/Support/PauseTest.php | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Support/Pause.php b/src/Illuminate/Support/Pause.php index 32bba1427838..7093a8173f9f 100644 --- a/src/Illuminate/Support/Pause.php +++ b/src/Illuminate/Support/Pause.php @@ -2,8 +2,10 @@ namespace Illuminate\Support; +use Illuminate\Support\Carbon; use Carbon\CarbonInterval; use DateInterval; +use DateTimeInterface; use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; @@ -33,14 +35,14 @@ class Pause /** * The sequence of pauses while faking. * - * @var bool + * @var array */ protected static $pauseSequence = []; /** * Create a new Pause instance. * - * @param int|float $duration + * @param int|float|\DateInterval $duration * @return void */ public function __construct($duration) @@ -67,9 +69,21 @@ public static function for($duration) return new static($duration); } + /** + * Sleep until the given timestamp. + * + * @param int|\DateTimeInterface $timestamp + * @return void + */ public static function until($timestamp) { - // + if (is_int($timestamp)) { + $timestamp = Carbon::createFromTimestamp($timestamp); + } + + $duration = Carbon::now()->diff($timestamp); + + static::for($duration); } /** @@ -190,6 +204,10 @@ public function __destruct() throw new RuntimeException('Unknown pause time unit.'); } + if ($this->duration->invert) { + $this->duration = CarbonInterval::seconds(0); + } + if (static::$fake) { // if ($this->capture) { static::$pauseSequence[] = $this->duration; @@ -201,14 +219,14 @@ public function __destruct() $remaining = $this->duration->copy(); if ((int) $remaining->totalSeconds > 0) { - static::sleep((int) $remaining->totalSeconds); + sleep((int) $remaining->totalSeconds); $remaining = $remaining->subSeconds((int) $remaining->totalSeconds); } if ($remaining->totalMicroseconds > 0) { - static::usleep($remaining->totalMicroseconds); + usleep($remaining->totalMicroseconds); } } @@ -216,22 +234,22 @@ public function __destruct() * Pause execution for the given duration in microseconds. * * @param int $duration - * @return void + * @return $this */ public static function usleep($duration) { - usleep($duration); + return static::for($duration)->microseconds(); } /** * Pause execution for the given duration in seconds. * * @param int $duration - * @return void + * @return $this */ public static function sleep($duration) { - sleep($duration); + return static::for($duration)->seconds(); } /** @@ -247,22 +265,29 @@ public static function fake($value = true) static::$pauseSequence = []; } + /** + * Assert the given pause sequence was encountered. + * + * @param array $sequence + * @return void + */ public static function assertSequence($sequence) { + PHPUnit::assertTrue(($expectedCount = count($sequence)) <= ($actualCount = count(static::$pauseSequence)), + "Expected [{$expectedCount}] pauses but only found [{$actualCount}]." + ); + collect($sequence) + ->take($actualCount) ->zip(static::$pauseSequence) - ->eachSpread(function (?Pause $expected, ?CarbonInterval $actual) { + ->eachSpread(function (?Pause $expected, CarbonInterval $actual) { if ($expected === null) { return false; } - if ($actual === null) { - // PHPUnit::fail('Expected '); - } - PHPUnit::assertTrue( $expected->duration->equalTo($actual), - "Expected pause of [{$expected->duration->forHumans()}] but instead found pause of [{$actual->forHumans()}]." + "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->forHumans(['options' => 0])}]." ); }); } diff --git a/tests/Support/PauseTest.php b/tests/Support/PauseTest.php index 0d9ce1f8a1a8..962790a98531 100644 --- a/tests/Support/PauseTest.php +++ b/tests/Support/PauseTest.php @@ -170,5 +170,96 @@ public function testItCanFailAssertions() $this->assertSame("Expected pause of [5 seconds] but instead found pause of [1 second].\nFailed asserting that false is true.", $e->getMessage()); } } + + public function testItCanCallSleepDirectly() + { + Pause::fake(); + + Pause::sleep(3); + + Pause::assertSequence([ + Pause::for(3)->seconds(), + ]); + } + + public function testItCanCallUSleepDirectly() + { + Pause::fake(); + + Pause::usleep(3); + + Pause::assertSequence([ + Pause::for(3)->microseconds(), + ]); + } + + public function testItCanSleepTillGivenTime() + { + Carbon::setTestNow(now()->startOfDay()); + Pause::fake(); + + Pause::until(now()->addMinute()); + + Pause::assertSequence([ + Pause::for(60)->seconds() + ]); + } + + public function testItSilentlyDoesntSleepIfTimeHasAlreadyPast() + { + Pause::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Pause::until(now()->subMinute()); + + Pause::assertSequence([ + Pause::for(0)->seconds() + ]); + } + + public function testEmptyDiff() + { + Pause::fake(); + + Pause::for(0)->seconds(); + + try { + Pause::assertSequence([ + Pause::for(1)->seconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected pause of [1 second] but instead found pause of [0 seconds].\nFailed asserting that false is true.", $e->getMessage()); + } + } + + public function testMoreAssertionsThanPauses() + { + Pause::fake(); + + Pause::for(1)->seconds(); + + try { + Pause::assertSequence([ + Pause::for(1)->seconds(), + Pause::for(1)->seconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] pauses but only found [1].\nFailed asserting that false is true.", $e->getMessage()); + } + } + + public function testMorePausesThanAssertions() + { + Pause::fake(); + + Pause::for(1)->seconds(); + Pause::for(2)->seconds(); + + Pause::assertSequence([ + Pause::for(1)->seconds(), + ]); + } } From 1adda208fe7f197aca9b6d9e37bbeb51284ca230 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 27 Apr 2023 12:37:01 +1000 Subject: [PATCH 06/25] wip --- src/Illuminate/Support/Pause.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Support/Pause.php b/src/Illuminate/Support/Pause.php index 7093a8173f9f..ff97e0c0b2f6 100644 --- a/src/Illuminate/Support/Pause.php +++ b/src/Illuminate/Support/Pause.php @@ -278,7 +278,6 @@ public static function assertSequence($sequence) ); collect($sequence) - ->take($actualCount) ->zip(static::$pauseSequence) ->eachSpread(function (?Pause $expected, CarbonInterval $actual) { if ($expected === null) { From 2b9dac428a0570afe7233fd88cf4ddff45eae815 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 28 Apr 2023 09:54:05 +1000 Subject: [PATCH 07/25] wip --- .../Support/{Pause.php => Siesta.php} | 74 ++++---- .../Support/{PauseTest.php => SiestaTest.php} | 164 +++++++++++------- 2 files changed, 144 insertions(+), 94 deletions(-) rename src/Illuminate/Support/{Pause.php => Siesta.php} (91%) rename tests/Support/{PauseTest.php => SiestaTest.php} (57%) diff --git a/src/Illuminate/Support/Pause.php b/src/Illuminate/Support/Siesta.php similarity index 91% rename from src/Illuminate/Support/Pause.php rename to src/Illuminate/Support/Siesta.php index ff97e0c0b2f6..48e9320b9613 100644 --- a/src/Illuminate/Support/Pause.php +++ b/src/Illuminate/Support/Siesta.php @@ -9,7 +9,7 @@ use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; -class Pause +class Siesta { /** * The total duration to pause execution. @@ -26,19 +26,26 @@ class Pause protected $pending; /** - * Indicate that all sleeping should be faked. + * Indicates that all sleeping should be faked. * * @var bool */ protected static $fake = false; /** - * The sequence of pauses while faking. + * The sequence of sleep durations encountered while faking. * * @var array */ protected static $pauseSequence = []; + /** + * The instance should be "captured" when faking. + * + * @var bool + */ + protected $capture = true; + /** * Create a new Pause instance. * @@ -58,6 +65,28 @@ public function __construct($duration) } } + /** + * Pause execution for the given duration in microseconds. + * + * @param int $duration + * @return $this + */ + public static function usleep($duration) + { + return static::for($duration)->microseconds(); + } + + /** + * Pause execution for the given duration in seconds. + * + * @param int $duration + * @return $this + */ + public static function sleep($duration) + { + return static::for($duration)->seconds(); + } + /** * Pause for the given duration. * @@ -81,11 +110,10 @@ public static function until($timestamp) $timestamp = Carbon::createFromTimestamp($timestamp); } - $duration = Carbon::now()->diff($timestamp); - - static::for($duration); + static::for(Carbon::now()->diff($timestamp)); } + /** * Pause execution for the given number of minutes. * @@ -204,14 +232,14 @@ public function __destruct() throw new RuntimeException('Unknown pause time unit.'); } - if ($this->duration->invert) { + if ($this->duration->totalMicroseconds <= 0) { $this->duration = CarbonInterval::seconds(0); } if (static::$fake) { - // if ($this->capture) { + if ($this->capture) { static::$pauseSequence[] = $this->duration; - // } + } return; } @@ -230,28 +258,6 @@ public function __destruct() } } - /** - * Pause execution for the given duration in microseconds. - * - * @param int $duration - * @return $this - */ - public static function usleep($duration) - { - return static::for($duration)->microseconds(); - } - - /** - * Pause execution for the given duration in seconds. - * - * @param int $duration - * @return $this - */ - public static function sleep($duration) - { - return static::for($duration)->seconds(); - } - /** * Capture all pauses for testing. * @@ -279,11 +285,13 @@ public static function assertSequence($sequence) collect($sequence) ->zip(static::$pauseSequence) - ->eachSpread(function (?Pause $expected, CarbonInterval $actual) { + ->eachSpread(function (?Siesta $expected, CarbonInterval $actual) { if ($expected === null) { - return false; + return; } + $expected->capture = false; + PHPUnit::assertTrue( $expected->duration->equalTo($actual), "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->forHumans(['options' => 0])}]." diff --git a/tests/Support/PauseTest.php b/tests/Support/SiestaTest.php similarity index 57% rename from tests/Support/PauseTest.php rename to tests/Support/SiestaTest.php index 962790a98531..dcd4c6d28f91 100644 --- a/tests/Support/PauseTest.php +++ b/tests/Support/SiestaTest.php @@ -4,24 +4,24 @@ use Carbon\CarbonInterval; use Illuminate\Support\Carbon; -use Illuminate\Support\Pause; +use Illuminate\Support\Siesta; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\TestCase; use RuntimeException; -class PauseTest extends TestCase +class SiestaTest extends TestCase { protected function setUp(): void { parent::setUp(); - Pause::fake(false); + Siesta::fake(false); } public function testItSleepForSeconds() { $start = Carbon::now(); - Pause::for(1)->seconds(); + Siesta::for(1)->seconds(); $end = Carbon::now(); $this->assertTrue($start->toImmutable()->addSecond()->isBefore($end)); @@ -31,7 +31,7 @@ public function testItSleepForSeconds() public function testItSleepsForSecondsWithMilliseconds() { $start = Carbon::now(); - Pause::for(1.5)->seconds(); + Siesta::for(1.5)->seconds(); $end = Carbon::now(); $this->assertTrue($start->toImmutable()->addSecond()->addMilliseconds(500)->isBefore($end)); @@ -40,7 +40,7 @@ public function testItSleepsForSecondsWithMilliseconds() public function testItCanSpecifyMinutes() { - $pause = Pause::for(1.5)->minutes(); + $pause = Siesta::for(1.5)->minutes(); $this->assertSame($pause->duration->totalSeconds, 90); @@ -49,7 +49,7 @@ public function testItCanSpecifyMinutes() public function testItCanSpecifySeconds() { - $pause = Pause::for(5)->seconds(); + $pause = Siesta::for(5)->seconds(); $this->assertSame($pause->duration->totalSeconds, 5); @@ -58,7 +58,7 @@ public function testItCanSpecifySeconds() public function testItCanSpecifySecond() { - $pause = Pause::for(1)->second(); + $pause = Siesta::for(1)->second(); $this->assertSame($pause->duration->totalSeconds, 1); @@ -67,7 +67,7 @@ public function testItCanSpecifySecond() public function testItCanSpecifyMilliseconds() { - $pause = Pause::for(5)->milliseconds(); + $pause = Siesta::for(5)->milliseconds(); $this->assertSame($pause->duration->totalMilliseconds, 5); @@ -76,7 +76,7 @@ public function testItCanSpecifyMilliseconds() public function testItCanSpecifyMillisecond() { - $pause = Pause::for(1)->milliseconds(); + $pause = Siesta::for(1)->milliseconds(); $this->assertSame($pause->duration->totalMilliseconds, 1); @@ -85,7 +85,7 @@ public function testItCanSpecifyMillisecond() public function testItCanSpecifyMicroseconds() { - $pause = Pause::for(5)->microseconds(); + $pause = Siesta::for(5)->microseconds(); $this->assertSame($pause->duration->totalMicroseconds, 5); @@ -94,7 +94,7 @@ public function testItCanSpecifyMicroseconds() public function testItCanSpecifyMicrosecond() { - $pause = Pause::for(1)->millisecond(); + $pause = Siesta::for(1)->millisecond(); $this->assertSame($pause->duration->totalMilliseconds, 1); @@ -103,7 +103,7 @@ public function testItCanSpecifyMicrosecond() public function testItCanChainDurations() { - $pause = Pause::for(1)->second()->and(500)->microseconds(); + $pause = Siesta::for(1)->second()->and(500)->microseconds(); $this->assertSame($pause->duration->totalMicroseconds, 1000500); @@ -112,7 +112,7 @@ public function testItCanChainDurations() public function testItCanPassDateInterval() { - $pause = Pause::for(CarbonInterval::seconds(3)); + $pause = Siesta::for(CarbonInterval::seconds(3)); $this->assertSame($pause->duration->totalSeconds, 3); @@ -122,7 +122,7 @@ public function testItCanPassDateInterval() public function testItThrowsOnUnknownTimeUnit() { try { - Pause::for(5); + Siesta::for(5); $this->fail(); } catch (RuntimeException $e) { $this->assertSame('Unknown pause time unit.', $e->getMessage()); @@ -131,10 +131,10 @@ public function testItThrowsOnUnknownTimeUnit() public function testItCanFakeSleep() { - Pause::fake(); + Siesta::fake(); $start = Carbon::now(); - Pause::for(5)->seconds(); + Siesta::for(5)->seconds(); $end = Carbon::now(); $this->assertTrue($start->toImmutable()->addMilliseconds(20)->isAfter($end)); @@ -142,28 +142,28 @@ public function testItCanFakeSleep() public function testItCanAssertPauseSequences() { - Pause::fake(); + Siesta::fake(); - Pause::for(5)->seconds(); - Pause::for(1)->seconds()->and(5)->microsecond(); + Siesta::for(5)->seconds(); + Siesta::for(1)->seconds()->and(5)->microsecond(); - Pause::assertSequence([ - Pause::for(5)->seconds(), - Pause::for(1)->seconds()->and(5)->microsecond(), + Siesta::assertSequence([ + Siesta::for(5)->seconds(), + Siesta::for(1)->seconds()->and(5)->microsecond(), ]); } public function testItCanFailAssertions() { - Pause::fake(); + Siesta::fake(); - Pause::for(5)->seconds(); - Pause::for(1)->seconds()->and(5)->microsecond(); + Siesta::for(5)->seconds(); + Siesta::for(1)->seconds()->and(5)->microsecond(); try { - Pause::assertSequence([ - Pause::for(5)->seconds(), - Pause::for(5)->seconds(), + Siesta::assertSequence([ + Siesta::for(5)->seconds(), + Siesta::for(5)->seconds(), ]); $this->fail(); } catch (AssertionFailedError $e) { @@ -173,59 +173,59 @@ public function testItCanFailAssertions() public function testItCanCallSleepDirectly() { - Pause::fake(); + Siesta::fake(); - Pause::sleep(3); + Siesta::sleep(3); - Pause::assertSequence([ - Pause::for(3)->seconds(), + Siesta::assertSequence([ + Siesta::for(3)->seconds(), ]); } public function testItCanCallUSleepDirectly() { - Pause::fake(); + Siesta::fake(); - Pause::usleep(3); + Siesta::usleep(3); - Pause::assertSequence([ - Pause::for(3)->microseconds(), + Siesta::assertSequence([ + Siesta::for(3)->microseconds(), ]); } public function testItCanSleepTillGivenTime() { Carbon::setTestNow(now()->startOfDay()); - Pause::fake(); + Siesta::fake(); - Pause::until(now()->addMinute()); + Siesta::until(now()->addMinute()); - Pause::assertSequence([ - Pause::for(60)->seconds() + Siesta::assertSequence([ + Siesta::for(60)->seconds() ]); } - public function testItSilentlyDoesntSleepIfTimeHasAlreadyPast() + public function testItSleepsForZeroTimeIfTimeHasAlreadyPast() { - Pause::fake(); + Siesta::fake(); Carbon::setTestNow(now()->startOfDay()); - Pause::until(now()->subMinute()); + Siesta::until(now()->subMinute()); - Pause::assertSequence([ - Pause::for(0)->seconds() + Siesta::assertSequence([ + Siesta::for(0)->seconds() ]); } public function testEmptyDiff() { - Pause::fake(); + Siesta::fake(); - Pause::for(0)->seconds(); + Siesta::for(0)->seconds(); try { - Pause::assertSequence([ - Pause::for(1)->seconds(), + Siesta::assertSequence([ + Siesta::for(1)->seconds(), ]); $this->fail(); } catch (AssertionFailedError $e) { @@ -235,14 +235,14 @@ public function testEmptyDiff() public function testMoreAssertionsThanPauses() { - Pause::fake(); + Siesta::fake(); - Pause::for(1)->seconds(); + Siesta::for(1)->seconds(); try { - Pause::assertSequence([ - Pause::for(1)->seconds(), - Pause::for(1)->seconds(), + Siesta::assertSequence([ + Siesta::for(1)->seconds(), + Siesta::for(1)->seconds(), ]); $this->fail(); } catch (AssertionFailedError $e) { @@ -252,14 +252,56 @@ public function testMoreAssertionsThanPauses() public function testMorePausesThanAssertions() { - Pause::fake(); + Siesta::fake(); - Pause::for(1)->seconds(); - Pause::for(2)->seconds(); + Siesta::for(1)->seconds(); + Siesta::for(2)->seconds(); - Pause::assertSequence([ - Pause::for(1)->seconds(), + Siesta::assertSequence([ + Siesta::for(1)->seconds(), + ]); + } + + public function testItDoesntSleepForNegativeDurations() + { + Siesta::fake(); + + Siesta::for(-1)->seconds(); + + Siesta::assertSequence([ + Siesta::for(0)->seconds(), + ]); + } + + public function testItDoesSleepWhenNegativeValuesAreChainedToPositive() + { + Siesta::fake(); + + Siesta::for(-1)->seconds()->and(5)->seconds(); + + Siesta::assertSequence([ + Siesta::for(4)->seconds(), ]); } -} + public function testItDoesntCaptureAssertionInstances() + { + Siesta::fake(); + + Siesta::for(1)->second(); + + Siesta::assertSequence([ + Siesta::for(1)->second(), + ]); + + try { + Siesta::assertSequence([ + Siesta::for(1)->second(), + Siesta::for(1)->second(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] pauses but only found [1].\nFailed asserting that false is true.", $e->getMessage()); + } + } +} From c14fcf598613205f5b028a0ef5396592994dbb84 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 28 Apr 2023 09:54:32 +1000 Subject: [PATCH 08/25] wip --- index.php | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 index.php diff --git a/index.php b/index.php deleted file mode 100644 index 07e25ed57432..000000000000 --- a/index.php +++ /dev/null @@ -1,35 +0,0 @@ -bar()->baz(); -echo 'done'; From f21f929ae6f33d1631b288b9c4d972fa875d7918 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 28 Apr 2023 10:48:37 +1000 Subject: [PATCH 09/25] wip --- src/Illuminate/Cache/Lock.php | 3 +- .../Redis/Limiters/ConcurrencyLimiter.php | 3 +- .../Redis/Limiters/DurationLimiter.php | 3 +- src/Illuminate/Support/Siesta.php | 49 +++++++++---------- src/Illuminate/Support/Timebox.php | 2 +- src/Illuminate/Support/helpers.php | 3 +- 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/Illuminate/Cache/Lock.php b/src/Illuminate/Cache/Lock.php index bed170507a9a..e92cdb9bfe49 100644 --- a/src/Illuminate/Cache/Lock.php +++ b/src/Illuminate/Cache/Lock.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Cache\Lock as LockContract; use Illuminate\Contracts\Cache\LockTimeoutException; use Illuminate\Support\InteractsWithTime; +use Illuminate\Support\Siesta; use Illuminate\Support\Str; abstract class Lock implements LockContract @@ -114,7 +115,7 @@ public function block($seconds, $callback = null) $starting = $this->currentTime(); while (! $this->acquire()) { - usleep($this->sleepMilliseconds * 1000); + Siesta::usleep($this->sleepMilliseconds * 1000); if ($this->currentTime() - $seconds >= $starting) { throw new LockTimeoutException; diff --git a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php index e249053e2cc9..e8b8bc37c21f 100644 --- a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php +++ b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php @@ -3,6 +3,7 @@ namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; +use Illuminate\Support\Siesta; use Illuminate\Support\Str; use Throwable; @@ -75,7 +76,7 @@ public function block($timeout, $callback = null, $sleep = 250) throw new LimiterTimeoutException; } - usleep($sleep * 1000); + Siesta::usleep($sleep * 1000); } if (is_callable($callback)) { diff --git a/src/Illuminate/Redis/Limiters/DurationLimiter.php b/src/Illuminate/Redis/Limiters/DurationLimiter.php index 65297ff9f65f..feac57b868a5 100644 --- a/src/Illuminate/Redis/Limiters/DurationLimiter.php +++ b/src/Illuminate/Redis/Limiters/DurationLimiter.php @@ -3,6 +3,7 @@ namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; +use Illuminate\Support\Siesta; class DurationLimiter { @@ -84,7 +85,7 @@ public function block($timeout, $callback = null, $sleep = 750) throw new LimiterTimeoutException; } - usleep($sleep * 1000); + Siesta::usleep($sleep * 1000); } if (is_callable($callback)) { diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index 48e9320b9613..9efce1ad838a 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -65,28 +65,6 @@ public function __construct($duration) } } - /** - * Pause execution for the given duration in microseconds. - * - * @param int $duration - * @return $this - */ - public static function usleep($duration) - { - return static::for($duration)->microseconds(); - } - - /** - * Pause execution for the given duration in seconds. - * - * @param int $duration - * @return $this - */ - public static function sleep($duration) - { - return static::for($duration)->seconds(); - } - /** * Pause for the given duration. * @@ -113,6 +91,27 @@ public static function until($timestamp) static::for(Carbon::now()->diff($timestamp)); } + /** + * Pause execution for the given duration in microseconds. + * + * @param int $duration + * @return $this + */ + public static function usleep($duration) + { + return static::for($duration)->microseconds(); + } + + /** + * Pause execution for the given duration in seconds. + * + * @param int|float $duration + * @return $this + */ + public static function sleep($duration) + { + return static::for($duration)->seconds(); + } /** * Pause execution for the given number of minutes. @@ -238,7 +237,7 @@ public function __destruct() if (static::$fake) { if ($this->capture) { - static::$pauseSequence[] = $this->duration; + static::$pauseSequence[] = $this; } return; @@ -285,7 +284,7 @@ public static function assertSequence($sequence) collect($sequence) ->zip(static::$pauseSequence) - ->eachSpread(function (?Siesta $expected, CarbonInterval $actual) { + ->eachSpread(function (?Siesta $expected, Siesta $actual) { if ($expected === null) { return; } @@ -293,7 +292,7 @@ public static function assertSequence($sequence) $expected->capture = false; PHPUnit::assertTrue( - $expected->duration->equalTo($actual), + $expected->duration->equalTo($actual->duration), "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->forHumans(['options' => 0])}]." ); }); diff --git a/src/Illuminate/Support/Timebox.php b/src/Illuminate/Support/Timebox.php index 0d41e0ac2d05..4758dfb345dc 100644 --- a/src/Illuminate/Support/Timebox.php +++ b/src/Illuminate/Support/Timebox.php @@ -79,6 +79,6 @@ public function dontReturnEarly() */ protected function usleep(int $microseconds) { - usleep($microseconds); + Siesta::usleep($microseconds); } } diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 5b3847f4a54f..325d3983c993 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -6,6 +6,7 @@ use Illuminate\Support\Env; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Optional; +use Illuminate\Support\Siesta; use Illuminate\Support\Str; if (! function_exists('append_config')) { @@ -253,7 +254,7 @@ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds; if ($sleepMilliseconds) { - usleep(value($sleepMilliseconds, $attempts, $e) * 1000); + Siesta::usleep(value($sleepMilliseconds, $attempts, $e) * 1000); } goto beginning; From 1abd689fba52e3b6fd73796ae9bd7d6de4dd1c37 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 28 Apr 2023 11:10:59 +1000 Subject: [PATCH 10/25] wip --- src/Illuminate/Support/Siesta.php | 69 ++++++++++++++++++++----------- tests/Support/SiestaTest.php | 35 +++++++++++++++- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index 9efce1ad838a..b88ce0a66d22 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -12,14 +12,14 @@ class Siesta { /** - * The total duration to pause execution. + * The total duration to sleep. * * @var \Carbon\CarbonInterval */ public $duration; /** - * The pending duration to pause execution. + * The pending duration to sleep. * * @var int|float */ @@ -37,7 +37,7 @@ class Siesta * * @var array */ - protected static $pauseSequence = []; + protected static $sequence = []; /** * The instance should be "captured" when faking. @@ -47,7 +47,7 @@ class Siesta protected $capture = true; /** - * Create a new Pause instance. + * Create a new Siesta instance. * * @param int|float|\DateInterval $duration * @return void @@ -66,7 +66,7 @@ public function __construct($duration) } /** - * Pause for the given duration. + * Sleep for the given duration. * * @param int|float|DateInterval $duration * @return static @@ -92,7 +92,7 @@ public static function until($timestamp) } /** - * Pause execution for the given duration in microseconds. + * Sleep for the duration in microseconds. * * @param int $duration * @return $this @@ -103,7 +103,7 @@ public static function usleep($duration) } /** - * Pause execution for the given duration in seconds. + * Sleep for the duration in seconds. * * @param int|float $duration * @return $this @@ -114,7 +114,7 @@ public static function sleep($duration) } /** - * Pause execution for the given number of minutes. + * Sleep for the duration in minutes. * * @return $this */ @@ -128,7 +128,7 @@ public function minutes() } /** - * Pause execution for the given number of minutes. + * Sleep for the duration in minutes. * * @return $this */ @@ -138,7 +138,7 @@ public function minute() } /** - * Pause execution for the given number of seconds. + * Sleep for the duration in seconds. */ public function seconds() { @@ -150,7 +150,7 @@ public function seconds() } /** - * Pause execution for the given number of seconds. + * Sleep for the duration in seconds. * * @return $this */ @@ -160,7 +160,7 @@ public function second() } /** - * Pause execution for the given number of milliseconds. + * Sleep for the duration in milliseconds. * * @return $this */ @@ -174,7 +174,7 @@ public function milliseconds() } /** - * Pause execution for the given number of milliseconds. + * Sleep for the duration in milliseconds. * * @return $this */ @@ -184,7 +184,7 @@ public function millisecond() } /** - * Pause execution for the given number of microseconds. + * Sleep for the duration in microseconds. * * @return $this */ @@ -198,7 +198,7 @@ public function microseconds() } /** - * Pause execution for the given number of microseconds. + * Sleep for the duration in microseconds. * * @return $this */ @@ -208,7 +208,7 @@ public function microsecond() } /** - * Add additional time to the execution pause. + * Add additional time to sleep for. * * @param int|float $duration * @return $this @@ -228,7 +228,7 @@ public function and($duration) public function __destruct() { if ($this->pending !== 0) { - throw new RuntimeException('Unknown pause time unit.'); + throw new RuntimeException('Unknown Siesta duration unit.'); } if ($this->duration->totalMicroseconds <= 0) { @@ -237,7 +237,7 @@ public function __destruct() if (static::$fake) { if ($this->capture) { - static::$pauseSequence[] = $this; + static::$sequence[] = $this; } return; @@ -258,7 +258,7 @@ public function __destruct() } /** - * Capture all pauses for testing. + * Stay awake and captured any attempts to sleep. * * @param bool $value * @return void @@ -267,23 +267,23 @@ public static function fake($value = true) { static::$fake = $value; - static::$pauseSequence = []; + static::$sequence = []; } /** - * Assert the given pause sequence was encountered. + * Assert the given sleep sequence was encountered. * * @param array $sequence * @return void */ public static function assertSequence($sequence) { - PHPUnit::assertTrue(($expectedCount = count($sequence)) <= ($actualCount = count(static::$pauseSequence)), + PHPUnit::assertTrue(($expectedCount = count($sequence)) <= ($actualCount = count(static::$sequence)), "Expected [{$expectedCount}] pauses but only found [{$actualCount}]." ); collect($sequence) - ->zip(static::$pauseSequence) + ->zip(static::$sequence) ->eachSpread(function (?Siesta $expected, Siesta $actual) { if ($expected === null) { return; @@ -293,8 +293,29 @@ public static function assertSequence($sequence) PHPUnit::assertTrue( $expected->duration->equalTo($actual->duration), - "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->forHumans(['options' => 0])}]." + "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->duration->forHumans(['options' => 0])}]." ); }); } + + /** + * Assert that no sleeping occurred. + * + * @return void + */ + public static function assertInsomniac() + { + PHPUnit::assertSame(0, $count = count(static::$sequence), "Expected [0] pauses but found [{$count}]."); + } + + /** + * Assert sleeping occurred the given times. + * + * @param int $times + * @return void + */ + public static function assertSleptTimes($expected) + { + PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] pauses but found [{$count}]."); + } } diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index dcd4c6d28f91..bf84f466d0b7 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -125,7 +125,7 @@ public function testItThrowsOnUnknownTimeUnit() Siesta::for(5); $this->fail(); } catch (RuntimeException $e) { - $this->assertSame('Unknown pause time unit.', $e->getMessage()); + $this->assertSame('Unknown Siesta duration unit.', $e->getMessage()); } } @@ -304,4 +304,37 @@ public function testItDoesntCaptureAssertionInstances() $this->assertSame("Expected [2] pauses but only found [1].\nFailed asserting that false is true.", $e->getMessage()); } } + + public function testItCanAssertNoSleepingOccurred() + { + Siesta::fake(); + + Siesta::assertInsomniac(); + + Siesta::for(1)->second(); + + try { + Siesta::assertInsomniac(); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [0] pauses but found [1].\nFailed asserting that 1 is identical to 0.", $e->getMessage()); + } + } + + public function testItCanAssertSleepCount() + { + Siesta::fake(); + + Siesta::assertSleptTimes(0); + + Siesta::for(1)->second(); + + Siesta::assertSleptTimes(1); + try { + Siesta::assertSleptTimes(2); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] pauses but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + } } From 69d2ecec2acbdf60622bbfb9bcbb50f21b84ad5a Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 28 Apr 2023 11:27:16 +1000 Subject: [PATCH 11/25] wip --- tests/Support/SiestaTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index bf84f466d0b7..ffc6667d83ee 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -18,7 +18,7 @@ protected function setUp(): void Siesta::fake(false); } - public function testItSleepForSeconds() + public function testItSleepsForSeconds() { $start = Carbon::now(); Siesta::for(1)->seconds(); From e8e8092ffc5404a5bea0ca1124f96b1179970b38 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 28 Apr 2023 11:27:54 +1000 Subject: [PATCH 12/25] wip --- src/Illuminate/Support/Siesta.php | 3 --- tests/Support/SiestaTest.php | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index b88ce0a66d22..9c955a39583a 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -2,10 +2,8 @@ namespace Illuminate\Support; -use Illuminate\Support\Carbon; use Carbon\CarbonInterval; use DateInterval; -use DateTimeInterface; use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; @@ -251,7 +249,6 @@ public function __destruct() $remaining = $remaining->subSeconds((int) $remaining->totalSeconds); } - if ($remaining->totalMicroseconds > 0) { usleep($remaining->totalMicroseconds); } diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index ffc6667d83ee..edf916c24eef 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -201,7 +201,7 @@ public function testItCanSleepTillGivenTime() Siesta::until(now()->addMinute()); Siesta::assertSequence([ - Siesta::for(60)->seconds() + Siesta::for(60)->seconds(), ]); } @@ -213,7 +213,7 @@ public function testItSleepsForZeroTimeIfTimeHasAlreadyPast() Siesta::until(now()->subMinute()); Siesta::assertSequence([ - Siesta::for(0)->seconds() + Siesta::for(0)->seconds(), ]); } From 3cb3506b85f18e22bf232171387ea089f3c0f371 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 28 Apr 2023 11:31:17 +1000 Subject: [PATCH 13/25] wip --- src/Illuminate/Foundation/Testing/TestCase.php | 2 ++ tests/Support/SiestaTest.php | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 9fae49f6bc2e..74305e8fa260 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -10,6 +10,7 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\ParallelTesting; +use Illuminate\Support\Siesta; use Illuminate\Support\Str; use Illuminate\View\Component; use Mockery; @@ -245,6 +246,7 @@ protected function tearDown(): void Component::forgetFactory(); Queue::createPayloadUsing(null); HandleExceptions::forgetApp(); + Siesta::fake(false); if ($this->callbackException) { throw $this->callbackException; diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index edf916c24eef..53239d63a22f 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -11,9 +11,9 @@ class SiestaTest extends TestCase { - protected function setUp(): void + protected function tearDown(): void { - parent::setUp(); + parent::tearDown(); Siesta::fake(false); } From 48f75eafec4a32c6ecb5bc8e9c1103c9874c58e1 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Sat, 29 Apr 2023 06:41:08 +1000 Subject: [PATCH 14/25] Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro --- src/Illuminate/Support/Siesta.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index 9c955a39583a..b50ad53a28af 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -33,7 +33,7 @@ class Siesta /** * The sequence of sleep durations encountered while faking. * - * @var array + * @var array */ protected static $sequence = []; From c8644e1b40a1d06528445892deafa60d98057d73 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Sat, 29 Apr 2023 06:42:13 +1000 Subject: [PATCH 15/25] Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro --- src/Illuminate/Support/Siesta.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index b50ad53a28af..0050d7005317 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -137,6 +137,8 @@ public function minute() /** * Sleep for the duration in seconds. + * + * @return $this */ public function seconds() { From 9f3ec09a04a726f9f264c5fdc3754d88a5a39fd3 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Sat, 29 Apr 2023 06:42:37 +1000 Subject: [PATCH 16/25] Update src/Illuminate/Support/Siesta.php Co-authored-by: Nuno Maduro --- src/Illuminate/Support/Siesta.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index 0050d7005317..2822881567ae 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -310,7 +310,7 @@ public static function assertInsomniac() /** * Assert sleeping occurred the given times. * - * @param int $times + * @param int $expected * @return void */ public static function assertSleptTimes($expected) From 25c114d284b1a9090689031f75f65970d8fa975d Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Sat, 29 Apr 2023 07:18:11 +1000 Subject: [PATCH 17/25] wip --- src/Illuminate/Support/Siesta.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index 2822881567ae..d08e8302c8cb 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -33,7 +33,7 @@ class Siesta /** * The sequence of sleep durations encountered while faking. * - * @var array + * @var array */ protected static $sequence = []; @@ -237,7 +237,7 @@ public function __destruct() if (static::$fake) { if ($this->capture) { - static::$sequence[] = $this; + static::$sequence[] = $this->duration; } return; @@ -283,7 +283,7 @@ public static function assertSequence($sequence) collect($sequence) ->zip(static::$sequence) - ->eachSpread(function (?Siesta $expected, Siesta $actual) { + ->eachSpread(function (?Siesta $expected, CarbonInterval $actual) { if ($expected === null) { return; } @@ -291,8 +291,8 @@ public static function assertSequence($sequence) $expected->capture = false; PHPUnit::assertTrue( - $expected->duration->equalTo($actual->duration), - "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->duration->forHumans(['options' => 0])}]." + $expected->duration->equalTo($actual), + "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->forHumans(['options' => 0])}]." ); }); } From 97fc72b126d7b0ed4e6a5f1e8fcecb96e920091e Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Sat, 29 Apr 2023 07:28:36 +1000 Subject: [PATCH 18/25] wip --- src/Illuminate/Support/Siesta.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index d08e8302c8cb..d7c7f13a0694 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -251,8 +251,8 @@ public function __destruct() $remaining = $remaining->subSeconds((int) $remaining->totalSeconds); } - if ($remaining->totalMicroseconds > 0) { - usleep($remaining->totalMicroseconds); + if ((int) $remaining->totalMicroseconds > 0) { + usleep((int) $remaining->totalMicroseconds); } } From 5ab9e4f7bb001b080dfd433f6f2c744305e63db9 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Sat, 29 Apr 2023 07:35:28 +1000 Subject: [PATCH 19/25] wip --- src/Illuminate/Support/Siesta.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index d7c7f13a0694..e3f8c9b25703 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -245,14 +245,18 @@ public function __destruct() $remaining = $this->duration->copy(); - if ((int) $remaining->totalSeconds > 0) { - sleep((int) $remaining->totalSeconds); + $seconds = (int) $remaining->totalSeconds; - $remaining = $remaining->subSeconds((int) $remaining->totalSeconds); + if ($seconds > 0) { + sleep($seconds); + + $remaining = $remaining->subSeconds($seconds); } - if ((int) $remaining->totalMicroseconds > 0) { - usleep((int) $remaining->totalMicroseconds); + $microseconds = (int) $remaining->totalMicroseconds; + + if ($microseconds > 0) { + usleep($microseconds); } } From d69880e9bc46078a8a31d0e69e6c65423f7b2e9f Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 1 May 2023 12:39:28 +1000 Subject: [PATCH 20/25] wip --- :noh | 352 ++++++++++++++++++++++++++++++ src/Illuminate/Support/Siesta.php | 149 +++++++++---- tests/Support/SiestaTest.php | 192 ++++++++-------- 3 files changed, 561 insertions(+), 132 deletions(-) create mode 100644 :noh diff --git a/:noh b/:noh new file mode 100644 index 000000000000..9376ead93de0 --- /dev/null +++ b/:noh @@ -0,0 +1,352 @@ +seconds(); + $end = microtime(true); + + $this->assertEqualsWithDelta(1, $end - $start, 0.03); + } + + public function testItSleepsForSecondsWithMilliseconds() + { + $start = microtime(true); + Siesta::for(1.5)->seconds(); + $end = microtime(true); + + $this->assertEqualsWithDelta(1.5, $end - $start, 0.03); + } + + public function testItCanFakeSleeping() + { + Siesta::fake(); + + $start = microtime(true); + Siesta::for(1.5)->seconds(); + $end = microtime(true); + + $this->assertEqualsWithDelta(0, $end - $start, 0.03); + } + + public function testItCanSpecifyMinutes() + { + Siesta::fake(); + + $siesta = Siesta::for(1.5)->minutes(); + + $this->assertSame($siesta->duration->totalMicroseconds, 90_000_000); + } + + public function testItCanSpecifyMinute() + { + Siesta::fake(); + + $siesta = Siesta::for(1)->minute(); + + $this->assertSame($siesta->duration->totalMicroseconds, 60_000_000); + } + + public function testItCanSpecifySeconds() + { + Siesta::fake(); + + $siesta = Siesta::for(1.5)->seconds(); + + $this->assertSame($siesta->duration->totalMicroseconds, 1_500_000); + } + + public function testItCanSpecifySecond() + { + Siesta::fake(); + + $siesta = Siesta::for(1)->second(); + + $this->assertSame($siesta->duration->totalMicroseconds, 1_000_000); + } + + public function testItCanSpecifyMilliseconds() + { + Siesta::fake(); + + $siesta = Siesta::for(1.5)->milliseconds(); + + $this->assertSame($siesta->duration->totalMicroseconds, 1_500); + } + + public function testItCanSpecifyMillisecond() + { + Siesta::fake(); + + $siesta = Siesta::for(1)->millisecond(); + + $this->assertSame($siesta->duration->totalMicroseconds, 1_000); + } + + public function testItCanSpecifyMicroseconds() + { + Siesta::fake(); + + $siesta = Siesta::for(1.5)->microseconds(); + + // rounded as microseconds is the smallest unit supported... + $this->assertSame($siesta->duration->totalMicroseconds, 1); + } + + public function testItCanSpecifyMicrosecond() + { + Siesta::fake(); + + $siesta = Siesta::for(1)->microsecond(); + + $this->assertSame($siesta->duration->totalMicroseconds, 1); + } + + public function testItCanChainDurations() + { + Siesta::fake(); + + $siesta = Siesta::for(1)->second() + ->and(500)->microseconds(); + + $this->assertSame($siesta->duration->totalMicroseconds, 1000500); + } + + public function testItCanUseDateInterval() + { + Siesta::fake(); + + $siesta = Siesta::for(CarbonInterval::seconds(1)->addMilliseconds(5)); + + $this->assertSame($siesta->duration->totalMicroseconds, 1_005_000); + } + + public function testItThrowsForUnknownTimeUnit() + { + try { + Siesta::for(5); + $this->fail(); + } catch (RuntimeException $e) { + $this->assertSame('Unknown duration unit.', $e->getMessage()); + } + } + + + public function testItCanAssertSequence() + { + Siesta::fake(); + + Siesta::for(5)->seconds(); + Siesta::for(1)->seconds()->and(5)->microsecond(); + + Siesta::assertSequence([ + Siesta::for(5)->seconds(), + Siesta::for(1)->seconds()->and(5)->microsecond(), + ]); + } + + public function testItFailsSequenceAssertion() + { + Siesta::fake(); + + Siesta::for(5)->seconds(); + Siesta::for(1)->seconds()->and(5)->microseconds(); + + try { + Siesta::assertSequence([ + Siesta::for(5)->seconds(), + Siesta::for(9)->seconds()->and(8)->milliseconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected siesta duration of [9 seconds 8 milliseconds] but instead found duration of [1 second 5 microseconds].\nFailed asserting that false is true.", $e->getMessage()); + } + } + + public function testItCanUseSleep() + { + Siesta::fake(); + + Siesta::sleep(3); + + Siesta::assertSequence([ + Siesta::for(3)->seconds(), + ]); + } + + public function testItCanUseUSleep() + { + Siesta::fake(); + + Siesta::usleep(3); + + Siesta::assertSequence([ + Siesta::for(3)->microseconds(), + ]); + } + + public function testItCanSleepTillGivenTime() + { + Siesta::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Siesta::until(now()->addMinute()); + + Siesta::assertSequence([ + Siesta::for(60)->seconds(), + ]); + } + + public function testItCanSleepTillGivenTimestamp() + { + Siesta::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Siesta::until(now()->addMinute()->timestamp); + + Siesta::assertSequence([ + Siesta::for(60)->seconds(), + ]); + } + + public function testItSleepsForZeroTimeWithNegativeDateTime() + { + Siesta::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Siesta::until(now()->subMinutes(100)); + + Siesta::assertSequence([ + Siesta::for(0)->seconds(), + ]); + } + + public function testSleepingForZeroTime() + { + Siesta::fake(); + + Siesta::for(0)->seconds(); + + try { + Siesta::assertSequence([ + Siesta::for(1)->seconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected siesta duration of [1 second] but instead found duration of [0 seconds].\nFailed asserting that false is true.", $e->getMessage()); + } + } + + public function testItFailsWhenSequenceContainsTooManySiestas() + { + Siesta::fake(); + + Siesta::for(1)->seconds(); + + try { + Siesta::assertSequence([ + Siesta::for(1)->seconds(), + Siesta::for(1)->seconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + } + + public function testMorePausesThanAssertions() + { + Siesta::fake(); + + Siesta::for(1)->seconds(); + Siesta::for(2)->seconds(); + + Siesta::assertSequence([ + Siesta::for(1)->seconds(), + ]); + } + + public function testThrowsExceptionForNegativeValues() + { + try { + Siesta::for(-1)->seconds(); + $this->fail(); + } catch (RuntimeException $e) { + $this->assertSame('Duration must be greater than or equal to 0.', $e->getMessage()); + } + } + + public function testItDoesntCaptureAssertionInstances() + { + Siesta::fake(); + + Siesta::for(1)->second(); + + Siesta::assertSequence([ + Siesta::for(1)->second(), + ]); + + try { + Siesta::assertSequence([ + Siesta::for(1)->second(), + Siesta::for(1)->second(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] pauses but found [1].\nFailed asserting that false is true.", $e->getMessage()); + } + } + + public function testItCanAssertNoSleepingOccurred() + { + Siesta::fake(); + + Siesta::assertInsomniac(); + + Siesta::for(1)->second(); + + try { + Siesta::assertInsomniac(); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [0] pauses but found [1].\nFailed asserting that 1 is identical to 0.", $e->getMessage()); + } + } + + public function testItCanAssertSleepCount() + { + Siesta::fake(); + + Siesta::assertSleptTimes(0); + + Siesta::for(1)->second(); + + Siesta::assertSleptTimes(1); + try { + Siesta::assertSleptTimes(2); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] pauses but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + } +} diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index e3f8c9b25703..eb1682ee69a0 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -2,6 +2,7 @@ namespace Illuminate\Support; +use Carbon\Carbon; use Carbon\CarbonInterval; use DateInterval; use PHPUnit\Framework\Assert as PHPUnit; @@ -19,9 +20,9 @@ class Siesta /** * The pending duration to sleep. * - * @var int|float + * @var int|float|null */ - protected $pending; + protected $pending = null; /** * Indicates that all sleeping should be faked. @@ -38,11 +39,11 @@ class Siesta protected static $sequence = []; /** - * The instance should be "captured" when faking. + * Indicates if the instance should sleep. * * @var bool */ - protected $capture = true; + protected $shouldSleep = true; /** * Create a new Siesta instance. @@ -52,14 +53,18 @@ class Siesta */ public function __construct($duration) { - if ($duration instanceof DateInterval) { - $this->duration = CarbonInterval::instance($duration); + if (! $duration instanceof DateInterval) { + $this->duration = CarbonInterval::microsecond(0); - $this->pending = 0; + $this->pending = $duration; } else { - $this->duration = CarbonInterval::seconds(0); + $duration = CarbonInterval::instance($duration); - $this->pending = $duration; + if ($duration->totalMicroseconds < 0) { + $duration = CarbonInterval::seconds(0); + } + + $this->duration = $duration; } } @@ -78,7 +83,7 @@ public static function for($duration) * Sleep until the given timestamp. * * @param int|\DateTimeInterface $timestamp - * @return void + * @return static */ public static function until($timestamp) { @@ -86,29 +91,29 @@ public static function until($timestamp) $timestamp = Carbon::createFromTimestamp($timestamp); } - static::for(Carbon::now()->diff($timestamp)); + return new static(Carbon::now()->diff($timestamp)); } /** * Sleep for the duration in microseconds. * * @param int $duration - * @return $this + * @return static */ public static function usleep($duration) { - return static::for($duration)->microseconds(); + return (new static($duration))->microseconds(); } /** * Sleep for the duration in seconds. * * @param int|float $duration - * @return $this + * @return static */ public static function sleep($duration) { - return static::for($duration)->seconds(); + return (new static($duration))->seconds(); } /** @@ -118,9 +123,7 @@ public static function sleep($duration) */ public function minutes() { - $this->duration->addMinutes($this->pending); - - $this->pending = 0; + $this->duration->add('minutes', $this->pullPending()); return $this; } @@ -142,9 +145,7 @@ public function minute() */ public function seconds() { - $this->duration->addSeconds($this->pending); - - $this->pending = 0; + $this->duration->add('seconds', $this->pullPending()); return $this; } @@ -166,9 +167,7 @@ public function second() */ public function milliseconds() { - $this->duration->addMilliseconds($this->pending); - - $this->pending = 0; + $this->duration->add('milliseconds', $this->pullPending()); return $this; } @@ -190,9 +189,7 @@ public function millisecond() */ public function microseconds() { - $this->duration->addMicroseconds($this->pending); - - $this->pending = 0; + $this->duration->add('microseconds', $this->pullPending()); return $this; } @@ -227,18 +224,16 @@ public function and($duration) */ public function __destruct() { - if ($this->pending !== 0) { - throw new RuntimeException('Unknown Siesta duration unit.'); + if (! $this->shouldSleep) { + return; } - if ($this->duration->totalMicroseconds <= 0) { - $this->duration = CarbonInterval::seconds(0); + if ($this->pending !== null) { + throw new RuntimeException('Unknown duration unit.'); } if (static::$fake) { - if ($this->capture) { - static::$sequence[] = $this->duration; - } + static::$sequence[] = $this->duration; return; } @@ -260,6 +255,28 @@ public function __destruct() } } + /** + * Resolve the pending duration. + * + * @return int|float + */ + protected function pullPending() + { + if ($this->pending === null) { + $this->shouldNotSleep(); + + throw new RuntimeException('No duration specified.'); + } + + if ($this->pending < 0) { + $this->pending = 0; + } + + return tap($this->pending, function () { + $this->pending = null; + }); + } + /** * Stay awake and captured any attempts to sleep. * @@ -273,6 +290,28 @@ public static function fake($value = true) static::$sequence = []; } + /** + * Assert the given sleeping occurred the a specific number of times. + * + * @param \Closure|static $expected + * @param int $times + * @return void + */ + public static function assertSlept($expected, $times = 1) + { + $callback = $expected instanceof static + ? fn (Siesta $actual) => $actual->duration->equalTo($expected->shouldNotSleep()->duration) + : $expected; + + $count = collect(static::$sequence)->filter($expected)->count(); + + PHPUnit::assertSame( + $times, + $count, + "The expected siesta was found [{$count}] times instead of [{$times}]." + ); + } + /** * Assert the given sleep sequence was encountered. * @@ -281,9 +320,7 @@ public static function fake($value = true) */ public static function assertSequence($sequence) { - PHPUnit::assertTrue(($expectedCount = count($sequence)) <= ($actualCount = count(static::$sequence)), - "Expected [{$expectedCount}] pauses but only found [{$actualCount}]." - ); + static::assertSleptTimes(count($sequence)); collect($sequence) ->zip(static::$sequence) @@ -292,11 +329,18 @@ public static function assertSequence($sequence) return; } - $expected->capture = false; - PHPUnit::assertTrue( - $expected->duration->equalTo($actual), - "Expected pause of [{$expected->duration->forHumans(['options' => 0])}] but instead found pause of [{$actual->forHumans(['options' => 0])}]." + $expected->shouldNotSleep()->duration->equalTo($actual), + vsprintf("Expected siesta duration of [%s] but instead found duration of [%s].", [ + $expected->duration->cascade()->forHumans([ + 'options' => 0, + 'minimumUnit' => 'microsecond', + ]), + $actual->cascade()->forHumans([ + 'options' => 0, + 'minimumUnit' => 'microsecond', + ]), + ]) ); }); } @@ -308,7 +352,14 @@ public static function assertSequence($sequence) */ public static function assertInsomniac() { - PHPUnit::assertSame(0, $count = count(static::$sequence), "Expected [0] pauses but found [{$count}]."); + foreach (static::$sequence as $duration) { + PHPUnit::assertSame(0, $duration->totalMicroseconds, vsprintf('Unexpected siesta duration of [%s] found.', [ + $duration->cascade()->forHumans([ + 'options' => 0, + 'minimumUnit' => 'microsecond', + ]), + ])); + } } /** @@ -319,6 +370,18 @@ public static function assertInsomniac() */ public static function assertSleptTimes($expected) { - PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] pauses but found [{$count}]."); + PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] siestas but found [{$count}]."); + } + + /** + * Indicate that the instance should not sleep. + * + * @return $this + */ + protected function shouldNotSleep() + { + $this->shouldSleep = false; + + return $this; } } diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index 53239d63a22f..acba48f45c89 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -16,131 +16,143 @@ protected function tearDown(): void parent::tearDown(); Siesta::fake(false); + + Carbon::setTestNow(); } public function testItSleepsForSeconds() { - $start = Carbon::now(); + $start = microtime(true); Siesta::for(1)->seconds(); - $end = Carbon::now(); + $end = microtime(true); - $this->assertTrue($start->toImmutable()->addSecond()->isBefore($end)); - $this->assertTrue($start->toImmutable()->addSecond()->addMilliseconds(20)->isAfter($end)); + $this->assertEqualsWithDelta(1, $end - $start, 0.03); } public function testItSleepsForSecondsWithMilliseconds() { - $start = Carbon::now(); + $start = microtime(true); Siesta::for(1.5)->seconds(); - $end = Carbon::now(); + $end = microtime(true); - $this->assertTrue($start->toImmutable()->addSecond()->addMilliseconds(500)->isBefore($end)); - $this->assertTrue($start->toImmutable()->addSecond()->addMilliseconds(520)->isAfter($end)); + $this->assertEqualsWithDelta(1.5, $end - $start, 0.03); + } + + public function testItCanFakeSleeping() + { + Siesta::fake(); + + $start = microtime(true); + Siesta::for(1.5)->seconds(); + $end = microtime(true); + + $this->assertEqualsWithDelta(0, $end - $start, 0.03); } public function testItCanSpecifyMinutes() { - $pause = Siesta::for(1.5)->minutes(); + Siesta::fake(); + + $siesta = Siesta::for(1.5)->minutes(); + + $this->assertSame($siesta->duration->totalMicroseconds, 90_000_000); + } + + public function testItCanSpecifyMinute() + { + Siesta::fake(); - $this->assertSame($pause->duration->totalSeconds, 90); + $siesta = Siesta::for(1)->minute(); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 60_000_000); } public function testItCanSpecifySeconds() { - $pause = Siesta::for(5)->seconds(); + Siesta::fake(); - $this->assertSame($pause->duration->totalSeconds, 5); + $siesta = Siesta::for(1.5)->seconds(); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 1_500_000); } public function testItCanSpecifySecond() { - $pause = Siesta::for(1)->second(); + Siesta::fake(); - $this->assertSame($pause->duration->totalSeconds, 1); + $siesta = Siesta::for(1)->second(); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 1_000_000); } public function testItCanSpecifyMilliseconds() { - $pause = Siesta::for(5)->milliseconds(); + Siesta::fake(); - $this->assertSame($pause->duration->totalMilliseconds, 5); + $siesta = Siesta::for(1.5)->milliseconds(); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 1_500); } public function testItCanSpecifyMillisecond() { - $pause = Siesta::for(1)->milliseconds(); + Siesta::fake(); - $this->assertSame($pause->duration->totalMilliseconds, 1); + $siesta = Siesta::for(1)->millisecond(); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 1_000); } public function testItCanSpecifyMicroseconds() { - $pause = Siesta::for(5)->microseconds(); + Siesta::fake(); - $this->assertSame($pause->duration->totalMicroseconds, 5); + $siesta = Siesta::for(1.5)->microseconds(); - $pause->duration = CarbonInterval::seconds(0); + // rounded as microseconds is the smallest unit supported... + $this->assertSame($siesta->duration->totalMicroseconds, 1); } public function testItCanSpecifyMicrosecond() { - $pause = Siesta::for(1)->millisecond(); + Siesta::fake(); - $this->assertSame($pause->duration->totalMilliseconds, 1); + $siesta = Siesta::for(1)->microsecond(); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 1); } public function testItCanChainDurations() { - $pause = Siesta::for(1)->second()->and(500)->microseconds(); + Siesta::fake(); - $this->assertSame($pause->duration->totalMicroseconds, 1000500); + $siesta = Siesta::for(1)->second() + ->and(500)->microseconds(); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 1000500); } - public function testItCanPassDateInterval() + public function testItCanUseDateInterval() { - $pause = Siesta::for(CarbonInterval::seconds(3)); + Siesta::fake(); - $this->assertSame($pause->duration->totalSeconds, 3); + $siesta = Siesta::for(CarbonInterval::seconds(1)->addMilliseconds(5)); - $pause->duration = CarbonInterval::seconds(0); + $this->assertSame($siesta->duration->totalMicroseconds, 1_005_000); } - public function testItThrowsOnUnknownTimeUnit() + public function testItThrowsForUnknownTimeUnit() { try { Siesta::for(5); $this->fail(); } catch (RuntimeException $e) { - $this->assertSame('Unknown Siesta duration unit.', $e->getMessage()); + $this->assertSame('Unknown duration unit.', $e->getMessage()); } } - public function testItCanFakeSleep() - { - Siesta::fake(); - - $start = Carbon::now(); - Siesta::for(5)->seconds(); - $end = Carbon::now(); - $this->assertTrue($start->toImmutable()->addMilliseconds(20)->isAfter($end)); - } - - public function testItCanAssertPauseSequences() + public function testItCanAssertSequence() { Siesta::fake(); @@ -153,25 +165,25 @@ public function testItCanAssertPauseSequences() ]); } - public function testItCanFailAssertions() + public function testItFailsSequenceAssertion() { Siesta::fake(); Siesta::for(5)->seconds(); - Siesta::for(1)->seconds()->and(5)->microsecond(); + Siesta::for(1)->seconds()->and(5)->microseconds(); try { Siesta::assertSequence([ Siesta::for(5)->seconds(), - Siesta::for(5)->seconds(), + Siesta::for(9)->seconds()->and(8)->milliseconds(), ]); $this->fail(); } catch (AssertionFailedError $e) { - $this->assertSame("Expected pause of [5 seconds] but instead found pause of [1 second].\nFailed asserting that false is true.", $e->getMessage()); + $this->assertSame("Expected siesta duration of [9 seconds 8 milliseconds] but instead found duration of [1 second 5 microseconds].\nFailed asserting that false is true.", $e->getMessage()); } } - public function testItCanCallSleepDirectly() + public function testItCanUseSleep() { Siesta::fake(); @@ -182,7 +194,7 @@ public function testItCanCallSleepDirectly() ]); } - public function testItCanCallUSleepDirectly() + public function testItCanUseUSleep() { Siesta::fake(); @@ -195,8 +207,8 @@ public function testItCanCallUSleepDirectly() public function testItCanSleepTillGivenTime() { - Carbon::setTestNow(now()->startOfDay()); Siesta::fake(); + Carbon::setTestNow(now()->startOfDay()); Siesta::until(now()->addMinute()); @@ -205,19 +217,31 @@ public function testItCanSleepTillGivenTime() ]); } - public function testItSleepsForZeroTimeIfTimeHasAlreadyPast() + public function testItCanSleepTillGivenTimestamp() { Siesta::fake(); Carbon::setTestNow(now()->startOfDay()); - Siesta::until(now()->subMinute()); + Siesta::until(now()->addMinute()->timestamp); + + Siesta::assertSequence([ + Siesta::for(60)->seconds(), + ]); + } + + public function testItSleepsForZeroTimeWithNegativeDateTime() + { + Siesta::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Siesta::until(now()->subMinutes(100)); Siesta::assertSequence([ Siesta::for(0)->seconds(), ]); } - public function testEmptyDiff() + public function testSleepingForZeroTime() { Siesta::fake(); @@ -229,11 +253,11 @@ public function testEmptyDiff() ]); $this->fail(); } catch (AssertionFailedError $e) { - $this->assertSame("Expected pause of [1 second] but instead found pause of [0 seconds].\nFailed asserting that false is true.", $e->getMessage()); + $this->assertSame("Expected siesta duration of [1 second] but instead found duration of [0 microseconds].\nFailed asserting that false is true.", $e->getMessage()); } } - public function testMoreAssertionsThanPauses() + public function testItFailsWhenSequenceContainsTooManySiestas() { Siesta::fake(); @@ -246,23 +270,11 @@ public function testMoreAssertionsThanPauses() ]); $this->fail(); } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] pauses but only found [1].\nFailed asserting that false is true.", $e->getMessage()); + $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); } } - public function testMorePausesThanAssertions() - { - Siesta::fake(); - - Siesta::for(1)->seconds(); - Siesta::for(2)->seconds(); - - Siesta::assertSequence([ - Siesta::for(1)->seconds(), - ]); - } - - public function testItDoesntSleepForNegativeDurations() + public function testSilentlySetsDurationToZeroForNegativeValues() { Siesta::fake(); @@ -273,17 +285,6 @@ public function testItDoesntSleepForNegativeDurations() ]); } - public function testItDoesSleepWhenNegativeValuesAreChainedToPositive() - { - Siesta::fake(); - - Siesta::for(-1)->seconds()->and(5)->seconds(); - - Siesta::assertSequence([ - Siesta::for(4)->seconds(), - ]); - } - public function testItDoesntCaptureAssertionInstances() { Siesta::fake(); @@ -301,7 +302,7 @@ public function testItDoesntCaptureAssertionInstances() ]); $this->fail(); } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] pauses but only found [1].\nFailed asserting that false is true.", $e->getMessage()); + $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); } } @@ -311,13 +312,18 @@ public function testItCanAssertNoSleepingOccurred() Siesta::assertInsomniac(); + Siesta::for(0)->second(); + + // we still have not slept... + Siesta::assertInsomniac(); + Siesta::for(1)->second(); try { Siesta::assertInsomniac(); $this->fail(); } catch (AssertionFailedError $e) { - $this->assertSame("Expected [0] pauses but found [1].\nFailed asserting that 1 is identical to 0.", $e->getMessage()); + $this->assertSame("Unexpected siesta duration of [1 second] found.\nFailed asserting that 1000000 is identical to 0.", $e->getMessage()); } } @@ -330,11 +336,19 @@ public function testItCanAssertSleepCount() Siesta::for(1)->second(); Siesta::assertSleptTimes(1); + + try { + Siesta::assertSleptTimes(0); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [0] siestas but found [1].\nFailed asserting that 1 is identical to 0.", $e->getMessage()); + } + try { Siesta::assertSleptTimes(2); $this->fail(); } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] pauses but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); } } } From 627656c9b0d1624fa770d5743753d552adbe4840 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 1 May 2023 12:40:07 +1000 Subject: [PATCH 21/25] wip --- :noh | 352 ----------------------------------------------------------- 1 file changed, 352 deletions(-) delete mode 100644 :noh diff --git a/:noh b/:noh deleted file mode 100644 index 9376ead93de0..000000000000 --- a/:noh +++ /dev/null @@ -1,352 +0,0 @@ -seconds(); - $end = microtime(true); - - $this->assertEqualsWithDelta(1, $end - $start, 0.03); - } - - public function testItSleepsForSecondsWithMilliseconds() - { - $start = microtime(true); - Siesta::for(1.5)->seconds(); - $end = microtime(true); - - $this->assertEqualsWithDelta(1.5, $end - $start, 0.03); - } - - public function testItCanFakeSleeping() - { - Siesta::fake(); - - $start = microtime(true); - Siesta::for(1.5)->seconds(); - $end = microtime(true); - - $this->assertEqualsWithDelta(0, $end - $start, 0.03); - } - - public function testItCanSpecifyMinutes() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->minutes(); - - $this->assertSame($siesta->duration->totalMicroseconds, 90_000_000); - } - - public function testItCanSpecifyMinute() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->minute(); - - $this->assertSame($siesta->duration->totalMicroseconds, 60_000_000); - } - - public function testItCanSpecifySeconds() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->seconds(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_500_000); - } - - public function testItCanSpecifySecond() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->second(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_000_000); - } - - public function testItCanSpecifyMilliseconds() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->milliseconds(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_500); - } - - public function testItCanSpecifyMillisecond() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->millisecond(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_000); - } - - public function testItCanSpecifyMicroseconds() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->microseconds(); - - // rounded as microseconds is the smallest unit supported... - $this->assertSame($siesta->duration->totalMicroseconds, 1); - } - - public function testItCanSpecifyMicrosecond() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->microsecond(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1); - } - - public function testItCanChainDurations() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->second() - ->and(500)->microseconds(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1000500); - } - - public function testItCanUseDateInterval() - { - Siesta::fake(); - - $siesta = Siesta::for(CarbonInterval::seconds(1)->addMilliseconds(5)); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_005_000); - } - - public function testItThrowsForUnknownTimeUnit() - { - try { - Siesta::for(5); - $this->fail(); - } catch (RuntimeException $e) { - $this->assertSame('Unknown duration unit.', $e->getMessage()); - } - } - - - public function testItCanAssertSequence() - { - Siesta::fake(); - - Siesta::for(5)->seconds(); - Siesta::for(1)->seconds()->and(5)->microsecond(); - - Siesta::assertSequence([ - Siesta::for(5)->seconds(), - Siesta::for(1)->seconds()->and(5)->microsecond(), - ]); - } - - public function testItFailsSequenceAssertion() - { - Siesta::fake(); - - Siesta::for(5)->seconds(); - Siesta::for(1)->seconds()->and(5)->microseconds(); - - try { - Siesta::assertSequence([ - Siesta::for(5)->seconds(), - Siesta::for(9)->seconds()->and(8)->milliseconds(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected siesta duration of [9 seconds 8 milliseconds] but instead found duration of [1 second 5 microseconds].\nFailed asserting that false is true.", $e->getMessage()); - } - } - - public function testItCanUseSleep() - { - Siesta::fake(); - - Siesta::sleep(3); - - Siesta::assertSequence([ - Siesta::for(3)->seconds(), - ]); - } - - public function testItCanUseUSleep() - { - Siesta::fake(); - - Siesta::usleep(3); - - Siesta::assertSequence([ - Siesta::for(3)->microseconds(), - ]); - } - - public function testItCanSleepTillGivenTime() - { - Siesta::fake(); - Carbon::setTestNow(now()->startOfDay()); - - Siesta::until(now()->addMinute()); - - Siesta::assertSequence([ - Siesta::for(60)->seconds(), - ]); - } - - public function testItCanSleepTillGivenTimestamp() - { - Siesta::fake(); - Carbon::setTestNow(now()->startOfDay()); - - Siesta::until(now()->addMinute()->timestamp); - - Siesta::assertSequence([ - Siesta::for(60)->seconds(), - ]); - } - - public function testItSleepsForZeroTimeWithNegativeDateTime() - { - Siesta::fake(); - Carbon::setTestNow(now()->startOfDay()); - - Siesta::until(now()->subMinutes(100)); - - Siesta::assertSequence([ - Siesta::for(0)->seconds(), - ]); - } - - public function testSleepingForZeroTime() - { - Siesta::fake(); - - Siesta::for(0)->seconds(); - - try { - Siesta::assertSequence([ - Siesta::for(1)->seconds(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected siesta duration of [1 second] but instead found duration of [0 seconds].\nFailed asserting that false is true.", $e->getMessage()); - } - } - - public function testItFailsWhenSequenceContainsTooManySiestas() - { - Siesta::fake(); - - Siesta::for(1)->seconds(); - - try { - Siesta::assertSequence([ - Siesta::for(1)->seconds(), - Siesta::for(1)->seconds(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); - } - } - - public function testMorePausesThanAssertions() - { - Siesta::fake(); - - Siesta::for(1)->seconds(); - Siesta::for(2)->seconds(); - - Siesta::assertSequence([ - Siesta::for(1)->seconds(), - ]); - } - - public function testThrowsExceptionForNegativeValues() - { - try { - Siesta::for(-1)->seconds(); - $this->fail(); - } catch (RuntimeException $e) { - $this->assertSame('Duration must be greater than or equal to 0.', $e->getMessage()); - } - } - - public function testItDoesntCaptureAssertionInstances() - { - Siesta::fake(); - - Siesta::for(1)->second(); - - Siesta::assertSequence([ - Siesta::for(1)->second(), - ]); - - try { - Siesta::assertSequence([ - Siesta::for(1)->second(), - Siesta::for(1)->second(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] pauses but found [1].\nFailed asserting that false is true.", $e->getMessage()); - } - } - - public function testItCanAssertNoSleepingOccurred() - { - Siesta::fake(); - - Siesta::assertInsomniac(); - - Siesta::for(1)->second(); - - try { - Siesta::assertInsomniac(); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [0] pauses but found [1].\nFailed asserting that 1 is identical to 0.", $e->getMessage()); - } - } - - public function testItCanAssertSleepCount() - { - Siesta::fake(); - - Siesta::assertSleptTimes(0); - - Siesta::for(1)->second(); - - Siesta::assertSleptTimes(1); - try { - Siesta::assertSleptTimes(2); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] pauses but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); - } - } -} From 1e4e26cc18a0296ee428976a1299263be1aac61b Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 1 May 2023 12:51:48 +1000 Subject: [PATCH 22/25] wip --- src/Illuminate/Support/Siesta.php | 22 ++++++++++----------- tests/Support/SiestaTest.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index eb1682ee69a0..3ddde0cd27b9 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -312,6 +312,17 @@ public static function assertSlept($expected, $times = 1) ); } + /** + * Assert sleeping occurred the given times. + * + * @param int $expected + * @return void + */ + public static function assertSleptTimes($expected) + { + PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] siestas but found [{$count}]."); + } + /** * Assert the given sleep sequence was encountered. * @@ -362,17 +373,6 @@ public static function assertInsomniac() } } - /** - * Assert sleeping occurred the given times. - * - * @param int $expected - * @return void - */ - public static function assertSleptTimes($expected) - { - PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] siestas but found [{$count}]."); - } - /** * Indicate that the instance should not sleep. * diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index acba48f45c89..0160b76d6b97 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -5,6 +5,7 @@ use Carbon\CarbonInterval; use Illuminate\Support\Carbon; use Illuminate\Support\Siesta; +use PHPUnit\Event\Test\AssertionFailed; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -351,4 +352,36 @@ public function testItCanAssertSleepCount() $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); } } + + public function testAssertSlept() + { + Siesta::fake(); + + Siesta::assertSlept(fn () => true, 0); + + try { + Siesta::assertSlept(fn () => true); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("The expected siesta was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage()); + } + + Siesta::for(5)->seconds(); + + Siesta::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 5); + + try { + Siesta::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 5, 2); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("The expected siesta was found [1] times instead of [2].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + + try { + Siesta::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 6); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("The expected siesta was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage()); + } + } } From d027e5abd4df345088a3361396fa035bb1bf7773 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 1 May 2023 12:58:20 +1000 Subject: [PATCH 23/25] wip --- src/Illuminate/Support/Siesta.php | 6 +----- tests/Support/SiestaTest.php | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index 3ddde0cd27b9..d3db35a1814a 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -293,16 +293,12 @@ public static function fake($value = true) /** * Assert the given sleeping occurred the a specific number of times. * - * @param \Closure|static $expected + * @param \Closure $expected * @param int $times * @return void */ public static function assertSlept($expected, $times = 1) { - $callback = $expected instanceof static - ? fn (Siesta $actual) => $actual->duration->equalTo($expected->shouldNotSleep()->duration) - : $expected; - $count = collect(static::$sequence)->filter($expected)->count(); PHPUnit::assertSame( diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index 0160b76d6b97..dee3b982e92e 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -152,7 +152,6 @@ public function testItThrowsForUnknownTimeUnit() } } - public function testItCanAssertSequence() { Siesta::fake(); From a1d8a65bdb11295066c5f5481d53a2ad677cff2d Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 1 May 2023 12:59:31 +1000 Subject: [PATCH 24/25] wip --- src/Illuminate/Support/Siesta.php | 2 +- tests/Support/SiestaTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Siesta.php index d3db35a1814a..c2b2e8e39f23 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Siesta.php @@ -338,7 +338,7 @@ public static function assertSequence($sequence) PHPUnit::assertTrue( $expected->shouldNotSleep()->duration->equalTo($actual), - vsprintf("Expected siesta duration of [%s] but instead found duration of [%s].", [ + vsprintf('Expected siesta duration of [%s] but instead found duration of [%s].', [ $expected->duration->cascade()->forHumans([ 'options' => 0, 'minimumUnit' => 'microsecond', diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php index dee3b982e92e..201669795cca 100644 --- a/tests/Support/SiestaTest.php +++ b/tests/Support/SiestaTest.php @@ -5,7 +5,6 @@ use Carbon\CarbonInterval; use Illuminate\Support\Carbon; use Illuminate\Support\Siesta; -use PHPUnit\Event\Test\AssertionFailed; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\TestCase; use RuntimeException; From 62de7d513834e7cff01f6098b0dae3d005f2e334 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 3 May 2023 14:03:10 -0500 Subject: [PATCH 25/25] formatting and rename to sleep --- src/Illuminate/Cache/Lock.php | 4 +- .../Foundation/Testing/TestCase.php | 4 +- .../Redis/Limiters/ConcurrencyLimiter.php | 4 +- .../Redis/Limiters/DurationLimiter.php | 4 +- .../Support/{Siesta.php => Sleep.php} | 54 ++- src/Illuminate/Support/Timebox.php | 2 +- src/Illuminate/Support/helpers.php | 4 +- tests/Support/SiestaTest.php | 385 ------------------ tests/Support/SleepTest.php | 385 ++++++++++++++++++ 9 files changed, 428 insertions(+), 418 deletions(-) rename src/Illuminate/Support/{Siesta.php => Sleep.php} (84%) delete mode 100644 tests/Support/SiestaTest.php create mode 100644 tests/Support/SleepTest.php diff --git a/src/Illuminate/Cache/Lock.php b/src/Illuminate/Cache/Lock.php index e92cdb9bfe49..ccd1c6474a5f 100644 --- a/src/Illuminate/Cache/Lock.php +++ b/src/Illuminate/Cache/Lock.php @@ -5,7 +5,7 @@ use Illuminate\Contracts\Cache\Lock as LockContract; use Illuminate\Contracts\Cache\LockTimeoutException; use Illuminate\Support\InteractsWithTime; -use Illuminate\Support\Siesta; +use Illuminate\Support\Sleep; use Illuminate\Support\Str; abstract class Lock implements LockContract @@ -115,7 +115,7 @@ public function block($seconds, $callback = null) $starting = $this->currentTime(); while (! $this->acquire()) { - Siesta::usleep($this->sleepMilliseconds * 1000); + Sleep::usleep($this->sleepMilliseconds * 1000); if ($this->currentTime() - $seconds >= $starting) { throw new LockTimeoutException; diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 74305e8fa260..a7315a2c5124 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -10,7 +10,7 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\ParallelTesting; -use Illuminate\Support\Siesta; +use Illuminate\Support\Sleep; use Illuminate\Support\Str; use Illuminate\View\Component; use Mockery; @@ -246,7 +246,7 @@ protected function tearDown(): void Component::forgetFactory(); Queue::createPayloadUsing(null); HandleExceptions::forgetApp(); - Siesta::fake(false); + Sleep::fake(false); if ($this->callbackException) { throw $this->callbackException; diff --git a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php index e8b8bc37c21f..62e50b01aad1 100644 --- a/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php +++ b/src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php @@ -3,7 +3,7 @@ namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; -use Illuminate\Support\Siesta; +use Illuminate\Support\Sleep; use Illuminate\Support\Str; use Throwable; @@ -76,7 +76,7 @@ public function block($timeout, $callback = null, $sleep = 250) throw new LimiterTimeoutException; } - Siesta::usleep($sleep * 1000); + Sleep::usleep($sleep * 1000); } if (is_callable($callback)) { diff --git a/src/Illuminate/Redis/Limiters/DurationLimiter.php b/src/Illuminate/Redis/Limiters/DurationLimiter.php index feac57b868a5..b0ecdaf9f4b4 100644 --- a/src/Illuminate/Redis/Limiters/DurationLimiter.php +++ b/src/Illuminate/Redis/Limiters/DurationLimiter.php @@ -3,7 +3,7 @@ namespace Illuminate\Redis\Limiters; use Illuminate\Contracts\Redis\LimiterTimeoutException; -use Illuminate\Support\Siesta; +use Illuminate\Support\Sleep; class DurationLimiter { @@ -85,7 +85,7 @@ public function block($timeout, $callback = null, $sleep = 750) throw new LimiterTimeoutException; } - Siesta::usleep($sleep * 1000); + Sleep::usleep($sleep * 1000); } if (is_callable($callback)) { diff --git a/src/Illuminate/Support/Siesta.php b/src/Illuminate/Support/Sleep.php similarity index 84% rename from src/Illuminate/Support/Siesta.php rename to src/Illuminate/Support/Sleep.php index c2b2e8e39f23..d4ee90f261e1 100644 --- a/src/Illuminate/Support/Siesta.php +++ b/src/Illuminate/Support/Sleep.php @@ -8,7 +8,7 @@ use PHPUnit\Framework\Assert as PHPUnit; use RuntimeException; -class Siesta +class Sleep { /** * The total duration to sleep. @@ -46,7 +46,7 @@ class Siesta protected $shouldSleep = true; /** - * Create a new Siesta instance. + * Create a new class instance. * * @param int|float|\DateInterval $duration * @return void @@ -71,7 +71,7 @@ public function __construct($duration) /** * Sleep for the given duration. * - * @param int|float|DateInterval $duration + * @param \DateInterval|int|float $duration * @return static */ public static function for($duration) @@ -82,7 +82,7 @@ public static function for($duration) /** * Sleep until the given timestamp. * - * @param int|\DateTimeInterface $timestamp + * @param \DateTimeInterface|int $timestamp * @return static */ public static function until($timestamp) @@ -95,7 +95,7 @@ public static function until($timestamp) } /** - * Sleep for the duration in microseconds. + * Sleep for the given number of microseconds. * * @param int $duration * @return static @@ -106,7 +106,7 @@ public static function usleep($duration) } /** - * Sleep for the duration in seconds. + * Sleep for the given number of seconds. * * @param int|float $duration * @return static @@ -117,7 +117,7 @@ public static function sleep($duration) } /** - * Sleep for the duration in minutes. + * Sleep for the given number of minutes. * * @return $this */ @@ -129,7 +129,7 @@ public function minutes() } /** - * Sleep for the duration in minutes. + * Sleep for one minute. * * @return $this */ @@ -139,7 +139,7 @@ public function minute() } /** - * Sleep for the duration in seconds. + * Sleep for the given number of seconds. * * @return $this */ @@ -151,7 +151,7 @@ public function seconds() } /** - * Sleep for the duration in seconds. + * Sleep for one second. * * @return $this */ @@ -161,7 +161,7 @@ public function second() } /** - * Sleep for the duration in milliseconds. + * Sleep for the given number of milliseconds. * * @return $this */ @@ -173,7 +173,7 @@ public function milliseconds() } /** - * Sleep for the duration in milliseconds. + * Sleep for one millisecond. * * @return $this */ @@ -183,7 +183,7 @@ public function millisecond() } /** - * Sleep for the duration in microseconds. + * Sleep for the given number of microseconds. * * @return $this */ @@ -195,7 +195,7 @@ public function microseconds() } /** - * Sleep for the duration in microseconds. + * Sleep for on microsecond. * * @return $this */ @@ -278,7 +278,7 @@ protected function pullPending() } /** - * Stay awake and captured any attempts to sleep. + * Stay awake and capture any attempts to sleep. * * @param bool $value * @return void @@ -291,7 +291,7 @@ public static function fake($value = true) } /** - * Assert the given sleeping occurred the a specific number of times. + * Assert a given amount of sleeping occurred a specific number of times. * * @param \Closure $expected * @param int $times @@ -304,19 +304,19 @@ public static function assertSlept($expected, $times = 1) PHPUnit::assertSame( $times, $count, - "The expected siesta was found [{$count}] times instead of [{$times}]." + "The expected sleep was found [{$count}] times instead of [{$times}]." ); } /** - * Assert sleeping occurred the given times. + * Assert sleeping occurred a given number of times. * * @param int $expected * @return void */ public static function assertSleptTimes($expected) { - PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] siestas but found [{$count}]."); + PHPUnit::assertSame($expected, $count = count(static::$sequence), "Expected [{$expected}] sleeps but found [{$count}]."); } /** @@ -331,14 +331,14 @@ public static function assertSequence($sequence) collect($sequence) ->zip(static::$sequence) - ->eachSpread(function (?Siesta $expected, CarbonInterval $actual) { + ->eachSpread(function (?Sleep $expected, CarbonInterval $actual) { if ($expected === null) { return; } PHPUnit::assertTrue( $expected->shouldNotSleep()->duration->equalTo($actual), - vsprintf('Expected siesta duration of [%s] but instead found duration of [%s].', [ + vsprintf('Expected sleep duration of [%s] but actually slept for [%s].', [ $expected->duration->cascade()->forHumans([ 'options' => 0, 'minimumUnit' => 'microsecond', @@ -352,6 +352,16 @@ public static function assertSequence($sequence) }); } + /** + * Assert that no sleeping occurred. + * + * @return void + */ + public static function assertNeverSlept() + { + return static::assertInsomniac(); + } + /** * Assert that no sleeping occurred. * @@ -360,7 +370,7 @@ public static function assertSequence($sequence) public static function assertInsomniac() { foreach (static::$sequence as $duration) { - PHPUnit::assertSame(0, $duration->totalMicroseconds, vsprintf('Unexpected siesta duration of [%s] found.', [ + PHPUnit::assertSame(0, $duration->totalMicroseconds, vsprintf('Unexpected sleep duration of [%s] found.', [ $duration->cascade()->forHumans([ 'options' => 0, 'minimumUnit' => 'microsecond', diff --git a/src/Illuminate/Support/Timebox.php b/src/Illuminate/Support/Timebox.php index 4758dfb345dc..cb99a4a8492d 100644 --- a/src/Illuminate/Support/Timebox.php +++ b/src/Illuminate/Support/Timebox.php @@ -79,6 +79,6 @@ public function dontReturnEarly() */ protected function usleep(int $microseconds) { - Siesta::usleep($microseconds); + Sleep::usleep($microseconds); } } diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 325d3983c993..2907e1249069 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -6,7 +6,7 @@ use Illuminate\Support\Env; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Optional; -use Illuminate\Support\Siesta; +use Illuminate\Support\Sleep; use Illuminate\Support\Str; if (! function_exists('append_config')) { @@ -254,7 +254,7 @@ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds; if ($sleepMilliseconds) { - Siesta::usleep(value($sleepMilliseconds, $attempts, $e) * 1000); + Sleep::usleep(value($sleepMilliseconds, $attempts, $e) * 1000); } goto beginning; diff --git a/tests/Support/SiestaTest.php b/tests/Support/SiestaTest.php deleted file mode 100644 index 201669795cca..000000000000 --- a/tests/Support/SiestaTest.php +++ /dev/null @@ -1,385 +0,0 @@ -seconds(); - $end = microtime(true); - - $this->assertEqualsWithDelta(1, $end - $start, 0.03); - } - - public function testItSleepsForSecondsWithMilliseconds() - { - $start = microtime(true); - Siesta::for(1.5)->seconds(); - $end = microtime(true); - - $this->assertEqualsWithDelta(1.5, $end - $start, 0.03); - } - - public function testItCanFakeSleeping() - { - Siesta::fake(); - - $start = microtime(true); - Siesta::for(1.5)->seconds(); - $end = microtime(true); - - $this->assertEqualsWithDelta(0, $end - $start, 0.03); - } - - public function testItCanSpecifyMinutes() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->minutes(); - - $this->assertSame($siesta->duration->totalMicroseconds, 90_000_000); - } - - public function testItCanSpecifyMinute() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->minute(); - - $this->assertSame($siesta->duration->totalMicroseconds, 60_000_000); - } - - public function testItCanSpecifySeconds() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->seconds(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_500_000); - } - - public function testItCanSpecifySecond() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->second(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_000_000); - } - - public function testItCanSpecifyMilliseconds() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->milliseconds(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_500); - } - - public function testItCanSpecifyMillisecond() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->millisecond(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_000); - } - - public function testItCanSpecifyMicroseconds() - { - Siesta::fake(); - - $siesta = Siesta::for(1.5)->microseconds(); - - // rounded as microseconds is the smallest unit supported... - $this->assertSame($siesta->duration->totalMicroseconds, 1); - } - - public function testItCanSpecifyMicrosecond() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->microsecond(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1); - } - - public function testItCanChainDurations() - { - Siesta::fake(); - - $siesta = Siesta::for(1)->second() - ->and(500)->microseconds(); - - $this->assertSame($siesta->duration->totalMicroseconds, 1000500); - } - - public function testItCanUseDateInterval() - { - Siesta::fake(); - - $siesta = Siesta::for(CarbonInterval::seconds(1)->addMilliseconds(5)); - - $this->assertSame($siesta->duration->totalMicroseconds, 1_005_000); - } - - public function testItThrowsForUnknownTimeUnit() - { - try { - Siesta::for(5); - $this->fail(); - } catch (RuntimeException $e) { - $this->assertSame('Unknown duration unit.', $e->getMessage()); - } - } - - public function testItCanAssertSequence() - { - Siesta::fake(); - - Siesta::for(5)->seconds(); - Siesta::for(1)->seconds()->and(5)->microsecond(); - - Siesta::assertSequence([ - Siesta::for(5)->seconds(), - Siesta::for(1)->seconds()->and(5)->microsecond(), - ]); - } - - public function testItFailsSequenceAssertion() - { - Siesta::fake(); - - Siesta::for(5)->seconds(); - Siesta::for(1)->seconds()->and(5)->microseconds(); - - try { - Siesta::assertSequence([ - Siesta::for(5)->seconds(), - Siesta::for(9)->seconds()->and(8)->milliseconds(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected siesta duration of [9 seconds 8 milliseconds] but instead found duration of [1 second 5 microseconds].\nFailed asserting that false is true.", $e->getMessage()); - } - } - - public function testItCanUseSleep() - { - Siesta::fake(); - - Siesta::sleep(3); - - Siesta::assertSequence([ - Siesta::for(3)->seconds(), - ]); - } - - public function testItCanUseUSleep() - { - Siesta::fake(); - - Siesta::usleep(3); - - Siesta::assertSequence([ - Siesta::for(3)->microseconds(), - ]); - } - - public function testItCanSleepTillGivenTime() - { - Siesta::fake(); - Carbon::setTestNow(now()->startOfDay()); - - Siesta::until(now()->addMinute()); - - Siesta::assertSequence([ - Siesta::for(60)->seconds(), - ]); - } - - public function testItCanSleepTillGivenTimestamp() - { - Siesta::fake(); - Carbon::setTestNow(now()->startOfDay()); - - Siesta::until(now()->addMinute()->timestamp); - - Siesta::assertSequence([ - Siesta::for(60)->seconds(), - ]); - } - - public function testItSleepsForZeroTimeWithNegativeDateTime() - { - Siesta::fake(); - Carbon::setTestNow(now()->startOfDay()); - - Siesta::until(now()->subMinutes(100)); - - Siesta::assertSequence([ - Siesta::for(0)->seconds(), - ]); - } - - public function testSleepingForZeroTime() - { - Siesta::fake(); - - Siesta::for(0)->seconds(); - - try { - Siesta::assertSequence([ - Siesta::for(1)->seconds(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected siesta duration of [1 second] but instead found duration of [0 microseconds].\nFailed asserting that false is true.", $e->getMessage()); - } - } - - public function testItFailsWhenSequenceContainsTooManySiestas() - { - Siesta::fake(); - - Siesta::for(1)->seconds(); - - try { - Siesta::assertSequence([ - Siesta::for(1)->seconds(), - Siesta::for(1)->seconds(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); - } - } - - public function testSilentlySetsDurationToZeroForNegativeValues() - { - Siesta::fake(); - - Siesta::for(-1)->seconds(); - - Siesta::assertSequence([ - Siesta::for(0)->seconds(), - ]); - } - - public function testItDoesntCaptureAssertionInstances() - { - Siesta::fake(); - - Siesta::for(1)->second(); - - Siesta::assertSequence([ - Siesta::for(1)->second(), - ]); - - try { - Siesta::assertSequence([ - Siesta::for(1)->second(), - Siesta::for(1)->second(), - ]); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); - } - } - - public function testItCanAssertNoSleepingOccurred() - { - Siesta::fake(); - - Siesta::assertInsomniac(); - - Siesta::for(0)->second(); - - // we still have not slept... - Siesta::assertInsomniac(); - - Siesta::for(1)->second(); - - try { - Siesta::assertInsomniac(); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Unexpected siesta duration of [1 second] found.\nFailed asserting that 1000000 is identical to 0.", $e->getMessage()); - } - } - - public function testItCanAssertSleepCount() - { - Siesta::fake(); - - Siesta::assertSleptTimes(0); - - Siesta::for(1)->second(); - - Siesta::assertSleptTimes(1); - - try { - Siesta::assertSleptTimes(0); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [0] siestas but found [1].\nFailed asserting that 1 is identical to 0.", $e->getMessage()); - } - - try { - Siesta::assertSleptTimes(2); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("Expected [2] siestas but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); - } - } - - public function testAssertSlept() - { - Siesta::fake(); - - Siesta::assertSlept(fn () => true, 0); - - try { - Siesta::assertSlept(fn () => true); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("The expected siesta was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage()); - } - - Siesta::for(5)->seconds(); - - Siesta::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 5); - - try { - Siesta::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 5, 2); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("The expected siesta was found [1] times instead of [2].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); - } - - try { - Siesta::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 6); - $this->fail(); - } catch (AssertionFailedError $e) { - $this->assertSame("The expected siesta was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage()); - } - } -} diff --git a/tests/Support/SleepTest.php b/tests/Support/SleepTest.php new file mode 100644 index 000000000000..38b456893ec9 --- /dev/null +++ b/tests/Support/SleepTest.php @@ -0,0 +1,385 @@ +seconds(); + $end = microtime(true); + + $this->assertEqualsWithDelta(1, $end - $start, 0.03); + } + + public function testItSleepsForSecondsWithMilliseconds() + { + $start = microtime(true); + Sleep::for(1.5)->seconds(); + $end = microtime(true); + + $this->assertEqualsWithDelta(1.5, $end - $start, 0.03); + } + + public function testItCanFakeSleeping() + { + Sleep::fake(); + + $start = microtime(true); + Sleep::for(1.5)->seconds(); + $end = microtime(true); + + $this->assertEqualsWithDelta(0, $end - $start, 0.03); + } + + public function testItCanSpecifyMinutes() + { + Sleep::fake(); + + $sleep = Sleep::for(1.5)->minutes(); + + $this->assertSame($sleep->duration->totalMicroseconds, 90_000_000); + } + + public function testItCanSpecifyMinute() + { + Sleep::fake(); + + $sleep = Sleep::for(1)->minute(); + + $this->assertSame($sleep->duration->totalMicroseconds, 60_000_000); + } + + public function testItCanSpecifySeconds() + { + Sleep::fake(); + + $sleep = Sleep::for(1.5)->seconds(); + + $this->assertSame($sleep->duration->totalMicroseconds, 1_500_000); + } + + public function testItCanSpecifySecond() + { + Sleep::fake(); + + $sleep = Sleep::for(1)->second(); + + $this->assertSame($sleep->duration->totalMicroseconds, 1_000_000); + } + + public function testItCanSpecifyMilliseconds() + { + Sleep::fake(); + + $sleep = Sleep::for(1.5)->milliseconds(); + + $this->assertSame($sleep->duration->totalMicroseconds, 1_500); + } + + public function testItCanSpecifyMillisecond() + { + Sleep::fake(); + + $sleep = Sleep::for(1)->millisecond(); + + $this->assertSame($sleep->duration->totalMicroseconds, 1_000); + } + + public function testItCanSpecifyMicroseconds() + { + Sleep::fake(); + + $sleep = Sleep::for(1.5)->microseconds(); + + // rounded as microseconds is the smallest unit supported... + $this->assertSame($sleep->duration->totalMicroseconds, 1); + } + + public function testItCanSpecifyMicrosecond() + { + Sleep::fake(); + + $sleep = Sleep::for(1)->microsecond(); + + $this->assertSame($sleep->duration->totalMicroseconds, 1); + } + + public function testItCanChainDurations() + { + Sleep::fake(); + + $sleep = Sleep::for(1)->second() + ->and(500)->microseconds(); + + $this->assertSame($sleep->duration->totalMicroseconds, 1000500); + } + + public function testItCanUseDateInterval() + { + Sleep::fake(); + + $sleep = Sleep::for(CarbonInterval::seconds(1)->addMilliseconds(5)); + + $this->assertSame($sleep->duration->totalMicroseconds, 1_005_000); + } + + public function testItThrowsForUnknownTimeUnit() + { + try { + Sleep::for(5); + $this->fail(); + } catch (RuntimeException $e) { + $this->assertSame('Unknown duration unit.', $e->getMessage()); + } + } + + public function testItCanAssertSequence() + { + Sleep::fake(); + + Sleep::for(5)->seconds(); + Sleep::for(1)->seconds()->and(5)->microsecond(); + + Sleep::assertSequence([ + Sleep::for(5)->seconds(), + Sleep::for(1)->seconds()->and(5)->microsecond(), + ]); + } + + public function testItFailsSequenceAssertion() + { + Sleep::fake(); + + Sleep::for(5)->seconds(); + Sleep::for(1)->seconds()->and(5)->microseconds(); + + try { + Sleep::assertSequence([ + Sleep::for(5)->seconds(), + Sleep::for(9)->seconds()->and(8)->milliseconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected sleep duration of [9 seconds 8 milliseconds] but actually slept for [1 second 5 microseconds].\nFailed asserting that false is true.", $e->getMessage()); + } + } + + public function testItCanUseSleep() + { + Sleep::fake(); + + Sleep::sleep(3); + + Sleep::assertSequence([ + Sleep::for(3)->seconds(), + ]); + } + + public function testItCanUseUSleep() + { + Sleep::fake(); + + Sleep::usleep(3); + + Sleep::assertSequence([ + Sleep::for(3)->microseconds(), + ]); + } + + public function testItCanSleepTillGivenTime() + { + Sleep::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Sleep::until(now()->addMinute()); + + Sleep::assertSequence([ + Sleep::for(60)->seconds(), + ]); + } + + public function testItCanSleepTillGivenTimestamp() + { + Sleep::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Sleep::until(now()->addMinute()->timestamp); + + Sleep::assertSequence([ + Sleep::for(60)->seconds(), + ]); + } + + public function testItSleepsForZeroTimeWithNegativeDateTime() + { + Sleep::fake(); + Carbon::setTestNow(now()->startOfDay()); + + Sleep::until(now()->subMinutes(100)); + + Sleep::assertSequence([ + Sleep::for(0)->seconds(), + ]); + } + + public function testSleepingForZeroTime() + { + Sleep::fake(); + + Sleep::for(0)->seconds(); + + try { + Sleep::assertSequence([ + Sleep::for(1)->seconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected sleep duration of [1 second] but actually slept for [0 microseconds].\nFailed asserting that false is true.", $e->getMessage()); + } + } + + public function testItFailsWhenSequenceContainsTooManySleeps() + { + Sleep::fake(); + + Sleep::for(1)->seconds(); + + try { + Sleep::assertSequence([ + Sleep::for(1)->seconds(), + Sleep::for(1)->seconds(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] sleeps but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + } + + public function testSilentlySetsDurationToZeroForNegativeValues() + { + Sleep::fake(); + + Sleep::for(-1)->seconds(); + + Sleep::assertSequence([ + Sleep::for(0)->seconds(), + ]); + } + + public function testItDoesntCaptureAssertionInstances() + { + Sleep::fake(); + + Sleep::for(1)->second(); + + Sleep::assertSequence([ + Sleep::for(1)->second(), + ]); + + try { + Sleep::assertSequence([ + Sleep::for(1)->second(), + Sleep::for(1)->second(), + ]); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] sleeps but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + } + + public function testItCanAssertNoSleepingOccurred() + { + Sleep::fake(); + + Sleep::assertInsomniac(); + + Sleep::for(0)->second(); + + // we still have not slept... + Sleep::assertInsomniac(); + + Sleep::for(1)->second(); + + try { + Sleep::assertInsomniac(); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Unexpected sleep duration of [1 second] found.\nFailed asserting that 1000000 is identical to 0.", $e->getMessage()); + } + } + + public function testItCanAssertSleepCount() + { + Sleep::fake(); + + Sleep::assertSleptTimes(0); + + Sleep::for(1)->second(); + + Sleep::assertSleptTimes(1); + + try { + Sleep::assertSleptTimes(0); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [0] sleeps but found [1].\nFailed asserting that 1 is identical to 0.", $e->getMessage()); + } + + try { + Sleep::assertSleptTimes(2); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("Expected [2] sleeps but found [1].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + } + + public function testAssertSlept() + { + Sleep::fake(); + + Sleep::assertSlept(fn () => true, 0); + + try { + Sleep::assertSlept(fn () => true); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("The expected sleep was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage()); + } + + Sleep::for(5)->seconds(); + + Sleep::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 5); + + try { + Sleep::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 5, 2); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("The expected sleep was found [1] times instead of [2].\nFailed asserting that 1 is identical to 2.", $e->getMessage()); + } + + try { + Sleep::assertSlept(fn (CarbonInterval $duration) => $duration->totalSeconds === 6); + $this->fail(); + } catch (AssertionFailedError $e) { + $this->assertSame("The expected sleep was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage()); + } + } +}