Search code examples
laravellaravel-5.8laravel-queuelaravel-jobs

How to manually run items from the Jobs table in Laravel (for testing)


I am trying to test that some data gets populated on a page that is done by a job. In my testing environment, the queue isn't running.

Is there any way to manually run the jobs from a function in a controller? I have retrieved all Jobs from my database by doing the following:

$allJobs = Jobs::all();

foreach ($allJobs as $job) {
     // $job->handle(); ????
}

What I would like is to iterate over each job and process them myself. My test suite can wait for these jobs to be processed. I can't seem to find any documentation about this. Thanks!


Solution

  • If the goal is to be able to write tests for your jobs, it is fairly simple:

    public function testJobsEvents()
    {
           $job = new \App\Jobs\YourJob;
           $job->handle();
    
           // Assert the side effect of your job...
    }