The new Sleep helper in Laravel 10.x
If you’ve been using PHP for a while, you might have used its sleep function at some point to pause the execution of the script for a given number of seconds.
For instance, if you want to pause the execution of the script for 5 seconds, you can do it like so.
Route::get('/', function () {
sleep(5);
});
This will pause the execution of the script for 5 seconds. But the problem with this function is that it’s a blocking function. And that means, if you’ve written tests around it, it will pause the execution of the test for 5 seconds as well. And that’s not something you want.
So, to solve mitigate this, Laravel 10.x has introduced a new Illuminate\Support\Sleep
helper that allows you to pause the execution of the script without blocking the execution of the test.
The Illuminate\Support\Sleep
helper
To start using the Illuminate\Support\Sleep
helper, you can import it and replace the sleep
function with it like so.
use Illuminate\Support\Sleep;
Route::get('/', function () {
Sleep::for(5)->seconds();
});
As you can tell, the Illuminate\Support\Sleep
helper has a seconds
method that accepts the number of seconds to pause the execution of the script.
You can specify the sleep time in milliseconds, microseconds, or minutes as well using the
milliseconds
,microseconds
, andminutes
methods respectively.
This method works exactly like PHP’s sleep
function but the magic happens when you write tests around it.
Faking sleep in tests
So, if you write a test around the above route, you can use the Sleep::fake
method to fake that the Illuminate\Support\Sleep
helper is being used.
use Illuminate\Support\Sleep;
it('returns the response immediately', function () {
Sleep::fake();
$response = $this->get('/');
$response->assertStatus(200);
});
This will make the Illuminate\Support\Sleep
helper to return immediately without pausing the execution of the test. So, this way, you can test your code without having to wait for the Illuminate\Support\Sleep
helper to finish its execution.
Asserting sleep times
We can also assert a few of the things around the Illuminate\Support\Sleep
helper. For instance, we can assert that the Illuminate\Support\Sleep
helper was called with the correct number of times using the assertSleepTimes
method.
use Illuminate\Support\Sleep;
it('returns the response immediately', function () {
Sleep::fake();
$response = $this->get('/');
$response->assertStatus(200);
Sleep::assertSleepTimes(1);
});
Or if you want to check if there wasn’t any call to the Illuminate\Support\Sleep
helper, you can use the assertNeverSlept
/assertInsomniac
methods like so.
use Illuminate\Support\Sleep;
it('returns the response immediately', function () {
Sleep::fake();
$response = $this->get('/');
$response->assertStatus(200);
Sleep::assertNeverSlept();
// or
Sleep::assertInsomniac();
});
Pretty neat, huh?
Prefer video format instead? Fret not! I have covered everything in this video as well.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.