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

eugenefvdm's avatar

Best practice testing JSON API response codes when using Queueing

I'm using APIs to do most of the heavy lifting in my application. I'm trying to stick with best practices, e.g. I have Unit and Feature tests, lean controllers, using dependency injection to abstract my APIs etc. I'm also using Marcel Pociot's Laravel API Documentation Generator which I absolutely love and find essential in my toolkit.

However where I'm stuck now is with queuing. The APIs I'm calling can take up to 6 seconds to complete so I've migrated from directly calling the API in the controller to farming it off to an queueable Action.

The problem I have is my controllers don't have any more standard returns anymore. My tests are failing as well, because whereas before I was expecting return codes such as 200 and 201, there is nothing.

Any tips on how the pros handle these situation, and perhaps a small example? :-)

0 likes
1 reply
tykus's avatar

Your controller still can return an empty response body with appropriate status code, i.e. 202 Accepted which means The request has been accepted for processing, but the processing has not been completed. You can fake the queue (using Queue::fake())and make assertions that the correct job was dispatched.

Queue::fake();

$this->getJson(/* sends request to api */)
    ->assertStatus(202);

Queue::assertPushed(NameOfJob::class, /* closure to return something truthy */);

The Job itself would be covered under a Unit test, rather than the Feature test.

https://laravel.com/docs/5.8/mocking#queue-fake

Please or to participate in this conversation.