I'll-Be-Back I'll-Be-Back - 3 years ago 141
PHP Question

Test functionality in the Job Class

I like to test (Feature test via phpunit) some methods in the Job Class (Lumen Queue) to make sure it is working correctly.

Problem is Job Class have some jobs methods like


If I run phpunit from console, I get an error:

Error: Call to a member function getJobId() on null

The test code look like this:

* @test
public function it_has_successfully_uploaded()
$job = new SomeJob(['file' => ['file1.zip']]);


How do I solve this issue?

Answer Source

Your code is using $this->job->getJobId(), but nowhere is that property declared or set. Are you perhaps using the InteractsWithQueue trait but forgot to include that in your code paste?

If so, the job property is set in InteractsWithQueue::setJob. This method may be called from several places, but in your case it is probably from Illuminate\Queue\CallQueuedHandler::setJobInstanceIfNecessary. This is some internal initialization of your job that Laravel does for you, which you must imitate in your test setup.

I would implement a simplistic version of Illuminate\Contracts\Queue\Job and call $job->setJob(new SimplisticVersionShazaamJob(...)); before calling $job->handle().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download