Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add Macroable trait to Sleep class #47099

Merged
merged 4 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/Illuminate/Support/Sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
use Carbon\Carbon;
use Carbon\CarbonInterval;
use DateInterval;
use Illuminate\Support\Traits\Macroable;
use PHPUnit\Framework\Assert as PHPUnit;
use RuntimeException;

class Sleep
{
use Macroable;

/**
* The total duration to sleep.
*
Expand Down Expand Up @@ -53,19 +56,7 @@ class Sleep
*/
public function __construct($duration)
{
if (! $duration instanceof DateInterval) {
$this->duration = CarbonInterval::microsecond(0);

$this->pending = $duration;
} else {
$duration = CarbonInterval::instance($duration);

if ($duration->totalMicroseconds < 0) {
$duration = CarbonInterval::seconds(0);
}

$this->duration = $duration;
}
$this->duration($duration);
}

/**
Expand Down Expand Up @@ -116,6 +107,32 @@ public static function sleep($duration)
return (new static($duration))->seconds();
}

/**
* Sleep for the given duration. Replaces any previously defined duration.
*
* @param \DateInterval|int|float $duration
* @return $this
*/
protected function duration($duration)
{
if (! $duration instanceof DateInterval) {
$this->duration = CarbonInterval::microsecond(0);

$this->pending = $duration;
} else {
$duration = CarbonInterval::instance($duration);

if ($duration->totalMicroseconds < 0) {
$duration = CarbonInterval::seconds(0);
}

$this->duration = $duration;
$this->pending = null;
}

return $this;
}

/**
* Sleep for the given number of minutes.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Support/SleepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,49 @@ public function testAssertSlept()
$this->assertSame("The expected sleep was found [0] times instead of [1].\nFailed asserting that 0 is identical to 1.", $e->getMessage());
}
}

public function testItCanCreateMacrosViaMacroable()
{
Sleep::fake();

Sleep::macro('forSomeConfiguredAmountOfTime', static function () {
return Sleep::for(3)->seconds();
});

Sleep::macro('useSomeOtherAmountOfTime', function () {
/** @var Sleep $this */
return $this->duration(1.234)->seconds();
});

Sleep::macro('andSomeMoreGranularControl', function () {
/** @var Sleep $this */
return $this->and(567)->microseconds();
});

// A static macro can be referenced
$sleep = Sleep::forSomeConfiguredAmountOfTime();
$this->assertSame($sleep->duration->totalMicroseconds, 3000000);

// A macro can specify a new duration
$sleep = $sleep->useSomeOtherAmountOfTime();
$this->assertSame($sleep->duration->totalMicroseconds, 1234000);

// A macro can supplement an existing duration
$sleep = $sleep->andSomeMoreGranularControl();
$this->assertSame($sleep->duration->totalMicroseconds, 1234567);
}

public function testItCanReplacePreviouslyDefinedDurations()
{
Sleep::fake();

$sleep = Sleep::for(1)->second();
$this->assertSame($sleep->duration->totalMicroseconds, 1000000);

$sleep->duration(2)->second();
$this->assertSame($sleep->duration->totalMicroseconds, 2000000);

$sleep->duration(500)->milliseconds();
$this->assertSame($sleep->duration->totalMicroseconds, 500000);
}
}