Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

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, and minutes 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.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

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.

Comments?