class Sleep

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
static array $fakeSleepCallbacks

The fake sleep callbacks.

static protected bool $syncWithCarbon

Keep Carbon's "now" in sync when sleeping.

CarbonInterval $duration

The total duration to sleep.

Closure|null $while

The callback that determines if sleeping should continue.

protected float|int|null $pending

The pending duration to sleep.

static protected bool $fake

Indicates that all sleeping should be faked.

static protected CarbonInterval> $sequence

The sequence of sleep durations encountered while faking.

protected bool $shouldSleep

Indicates if the instance should sleep.

protected bool $alreadySlept

Indicates if the instance already slept via then().

Methods

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

__construct(DateInterval|float|int $duration)

Create a new class instance.

static Sleep
for(DateInterval|float|int $duration)

Sleep for the given duration.

static Sleep
until(DateTimeInterface|float|int|string $timestamp)

Sleep until the given timestamp.

static Sleep
usleep(int $duration)

Sleep for the given number of microseconds.

static Sleep
sleep(float|int $duration)

Sleep for the given number of seconds.

duration(DateInterval|float|int $duration)

Sleep for the given duration. Replaces any previously defined duration.

minutes()

Sleep for the given number of minutes.

minute()

Sleep for one minute.

seconds()

Sleep for the given number of seconds.

second()

Sleep for one second.

milliseconds()

Sleep for the given number of milliseconds.

millisecond()

Sleep for one millisecond.

microseconds()

Sleep for the given number of microseconds.

microsecond()

Sleep for on microsecond.

and(float|int $duration)

Add additional time to sleep for.

while(Closure $callback)

Sleep while a given callback returns "true".

mixed
then(callable $then)

Specify a callback that should be executed after sleeping.

__destruct()

Handle the object's destruction.

void
goodnight()

Handle the object's destruction.

float|int
pullPending()

Resolve the pending duration.

static void
fake(bool $value = true, bool $syncWithCarbon = false)

Stay awake and capture any attempts to sleep.

static void
assertSlept(Closure $expected, int $times = 1)

Assert a given amount of sleeping occurred a specific number of times.

static void
assertSleptTimes(int $expected)

Assert sleeping occurred a given number of times.

static void
assertSequence(array $sequence)

Assert the given sleep sequence was encountered.

static void
assertNeverSlept()

Assert that no sleeping occurred.

static void
assertInsomniac()

Assert that no sleeping occurred.

shouldNotSleep()

Indicate that the instance should not sleep.

when(bool|Closure $condition)

Only sleep when the given condition is true.

unless(bool|Closure $condition)

Don't sleep when the given condition is true.

static void
whenFakingSleep(callable $callback)

Specify a callback that should be invoked when faking sleep within a test.

static void
syncWithCarbon(bool $value = true)

Indicate that Carbon's "now" should be kept in sync when sleeping.

static void
flushState()

Flush all static state.

Details

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 72
__construct(DateInterval|float|int $duration)

Create a new class instance.

Parameters

DateInterval|float|int $duration

at line 80
static Sleep for(DateInterval|float|int $duration)

Sleep for the given duration.

Parameters

DateInterval|float|int $duration

Return Value

Sleep

at line 88
static Sleep until(DateTimeInterface|float|int|string $timestamp)

Sleep until the given timestamp.

Parameters

DateTimeInterface|float|int|string $timestamp

Return Value

Sleep

at line 100
static Sleep usleep(int $duration)

Sleep for the given number of microseconds.

Parameters

int $duration

Return Value

Sleep

at line 108
static Sleep sleep(float|int $duration)

Sleep for the given number of seconds.

Parameters

float|int $duration

Return Value

Sleep

at line 116
protected Sleep duration(DateInterval|float|int $duration)

Sleep for the given duration. Replaces any previously defined duration.

Parameters

DateInterval|float|int $duration

Return Value

Sleep

at line 139
Sleep minutes()

Sleep for the given number of minutes.

Return Value

Sleep

at line 149
Sleep minute()

Sleep for one minute.

Return Value

Sleep

at line 157
Sleep seconds()

Sleep for the given number of seconds.

Return Value

Sleep

at line 167
Sleep second()

Sleep for one second.

Return Value

Sleep

at line 175
Sleep milliseconds()

Sleep for the given number of milliseconds.

Return Value

Sleep

at line 185
Sleep millisecond()

Sleep for one millisecond.

Return Value

Sleep

at line 193
Sleep microseconds()

Sleep for the given number of microseconds.

Return Value

Sleep

at line 203
Sleep microsecond()

Sleep for on microsecond.

Return Value

Sleep

at line 211
Sleep and(float|int $duration)

Add additional time to sleep for.

Parameters

float|int $duration

Return Value

Sleep

at line 221
Sleep while(Closure $callback)

Sleep while a given callback returns "true".

Parameters

Closure $callback

Return Value

Sleep

at line 231
mixed then(callable $then)

Specify a callback that should be executed after sleeping.

Parameters

callable $then

Return Value

mixed

at line 243
__destruct()

Handle the object's destruction.

at line 251
protected void goodnight()

Handle the object's destruction.

Return Value

void

at line 303
protected float|int pullPending()

Resolve the pending duration.

Return Value

float|int

at line 326
static void fake(bool $value = true, bool $syncWithCarbon = false)

Stay awake and capture any attempts to sleep.

Tests only. Sleep fakes persist in static state for the worker lifetime and affect every subsequent sleep call.

Parameters

bool $value
bool $syncWithCarbon

Return Value

void

at line 338
static void assertSlept(Closure $expected, int $times = 1)

Assert a given amount of sleeping occurred a specific number of times.

Parameters

Closure $expected
int $times

Return Value

void

at line 352
static void assertSleptTimes(int $expected)

Assert sleeping occurred a given number of times.

Parameters

int $expected

Return Value

void

at line 360
static void assertSequence(array $sequence)

Assert the given sleep sequence was encountered.

Parameters

array $sequence

Return Value

void

at line 399
static void assertNeverSlept()

Assert that no sleeping occurred.

Return Value

void

at line 407
static void assertInsomniac()

Assert that no sleeping occurred.

Return Value

void

at line 426
protected Sleep shouldNotSleep()

Indicate that the instance should not sleep.

Return Value

Sleep

at line 436
Sleep when(bool|Closure $condition)

Only sleep when the given condition is true.

Parameters

bool|Closure $condition

Return Value

Sleep

at line 446
Sleep unless(bool|Closure $condition)

Don't sleep when the given condition is true.

Parameters

bool|Closure $condition

Return Value

Sleep

at line 457
static void whenFakingSleep(callable $callback)

Specify a callback that should be invoked when faking sleep within a test.

Tests only. The callback persists in static state for the worker lifetime and runs for every subsequent faked sleep.

Parameters

callable $callback

Return Value

void

at line 468
static void syncWithCarbon(bool $value = true)

Indicate that Carbon's "now" should be kept in sync when sleeping.

Tests only. The flag persists in static state for the worker lifetime and affects every subsequent faked sleep.

Parameters

bool $value

Return Value

void

at line 476
static void flushState()

Flush all static state.

Return Value

void