QueueFake size returns wrong value
php artisan make:job TestJobphp artisan make:test QueueSizeTest public function testQueueSize()
{
\Queue::fake();
$job = new TestJob();
dispatch($job);
dispatch($job);
dispatch($job)->onQueue('Q2');
$this->assertEquals(2, \Queue::size());
$this->assertEquals(1, \Queue::size('Q2'));
}
The flaw is in QueueFake' size method:
/**
* Get the size of the queue.
*
* @param string|null $queue
* @return int
*/
public function size($queue = null)
{
return count($this->jobs);
}
push method:public function push($job, $data = '', $queue = null)
{
$this->jobs[is_object($job) ? get_class($job) : $job][] = [
'job' => $job,
'queue' => $queue,
];
}
This indeed appears to be a bug. Thanks for reporting. Appreciating a PR if you could whip one up.
I can't believe how lazy I am, I forgot that I was using a custom QueueFake for years because of this.
This code is just for inspiration, it works in my project but if you base a fix on it, please vet it it works in all cases:
public function size($queue = null)
{
return collect($this->jobs)
->map(function (array $job) use ($queue) {
return $job[0]['queue'] === $queue;
})
->count();
}
@mfn thanks for the inspiration, but that piece doesn't traverse through properly, I'll implement something soon
PR was merged