Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ranx99's avatar

How to test redis queue connection?

I am using "redis" to mange my queues

phpunit.xml:

<server name="QUEUE_CONNECTION" value="redis"/>

But in the following test the queue's connection name returns "null":

 /** @test */
    public function is_pushed_to_queue()
    {
        Queue::fake();
        $user = factory(User::class)->create();

        $user->notify(
            new EmailVerificationNotification($user)
        );
 
        $this->assertEquals(1, Queue::size());
        $this->assertEquals('redis', Queue::getConnectionName());// getConnectionName returns "null"
    }

phpunit error:

Failed asserting that null matches expected 'redis'.

How can I test the connection of the queues which is "redis"?

0 likes
5 replies
bobbybouwmann's avatar
Level 88

If you use Queue::fake() the test will be mocked. So instead of putting the jobs in Redis, it will keep an array with the fired jobs that are in the queue. This way you can assert against it and see if the correct number of events have been fired. Because of that, the connection name is not Redis anymore.

In general, you don't want to run your tests against a Redis queue. In this case, you have to trust the framework that they provide the tools to make this work. The only thing you should care about in your test is if the business logic is happening correctly. So in your case is the notification sent and so on.

Let me know if that makes sense to you ;)

PS: You're firing a notification. So instead you should fake the notifications and assert against that: https://laravel.com/docs/6.x/mocking#notification-fake

Ranx99's avatar

yes, it makes sense.

So, I just need to trust the framework that It will add these jobs when I go on prod.

Yes I already tested the notification and asserted it will go to the queue.

thanks for info,

Ranx99's avatar

A related question:

Don't I need to test the redis connection itself? to be sure if redis is installed in the server and ready to make operations. or I should not care about testing this?

1 like
bobbybouwmann's avatar

Yeah, you should double-check if the Redis connection is working on your server. In general, the code for the Redis part is part of the framework. It your jobs locally work with the sync driver you can assume they work with the redis driver ;)

2 likes
Daniel-Pablo's avatar

Hi @bobbybouwmann it's possible to change the phpunit to this?

<env name="QUEUE_CONNECTION" value="redis"/>

and make the test with Redis directly? or I should use SYNC? how can I assume that if my jobs locally work with the sync driver I can assume they work with the Redis driver? can you please give more information about this, is very interesting and important to understand, thanks in advance @bobbybouwmann

Please or to participate in this conversation.