I want to use PHPUnit to exercise the queued job lifecycle when performing a job triggered by an API request.
EDIT: phpunit.xml has
<env name="QUEUE_DRIVER" value="database"/>
There is a call from the frontend to an API that dispatches a job and immediately returns a response with the status (queued). This works fine when called from the frontend Javascript.
Trying to test this same endpoint with PHPUnit, with a test that calls the same API endpoint, I see that the job does not appear in the 'jobs' table. Here is the test code (EDIT2):
public function testJobStatusBasic()
{
$response = $this->post('/api/jobstatustest');
$response->assertStatus(200);
$response->assertJson([
"data" => []
]);
}
This returns as a pass, the API endpoint shown there inserts a test job which takes a few seconds to complete. At least, it does when the front-end calls it. Just not from PHPUnit. The idea is that I can add some subsequent GET calls to test the job status monitoring system.
With some logging, I can see that the Job constructor is called. No errors are seen, there is just no job in the database when the call returns.
Setting QUEUE_CONNECTION to "sync" works fine executing the job synchronously, delaying the return of the above POST, but this is not what I want to test here, where I specifically want to run the entire job lifecycle. Or is this impossible / not intended to work like this?