Skip to content

Commit efe2c6e

Browse files
authored
Add check for job class existance (#24)
1 parent f09476d commit efe2c6e

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/Job/QlessJob.php

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Container\Container;
77
use Illuminate\Queue\Jobs\Job;
88
use Illuminate\Contracts\Queue\Job as JobContract;
9+
use Illuminate\Queue\Jobs\JobName;
910
use Illuminate\Queue\ManuallyFailedException;
1011
use LaravelQless\Queue\QlessQueue;
1112
use Qless\Jobs\BaseJob;
@@ -194,4 +195,21 @@ public function getRawBody()
194195
{
195196
return $this->payload;
196197
}
198+
199+
/**
200+
* Process an exception that caused the job to fail.
201+
*
202+
* @param \Throwable|null $e
203+
* @return void
204+
*/
205+
protected function failed($e)
206+
{
207+
$payload = $this->payload();
208+
209+
[$class, $method] = JobName::parse($payload['job']);
210+
211+
if (class_exists($class) && method_exists($this->instance = $this->resolve($class), 'failed')) {
212+
$this->instance->failed($payload['data'], $e);
213+
}
214+
}
197215
}

tests/Job/QlessJobTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace LaravelQless\Tests\Job;
44

55
use Illuminate\Container\Container;
6+
use Illuminate\Contracts\Container\BindingResolutionException;
67
use LaravelQless\Contracts\JobHandler;
78
use LaravelQless\Queue\QlessQueue;
89
use LaravelQless\Tests\Helpers\ModifierTrait;
10+
use LaravelQless\Tests\Stub\JobStub;
911
use Orchestra\Testbench\TestCase;
1012
use LaravelQless\Job\QlessJob;
1113
use Qless\Jobs\BaseJob;
@@ -53,6 +55,48 @@ public function testFireFailed(): void
5355
{
5456
self::markTestSkipped('Refactor class to set `failed` property');
5557
}
58+
public function testFireFailedWithNotExistentClass(): void
59+
{
60+
$jobMock = $this->getJob();
61+
$jobMock->method('getData')
62+
->willReturn(new JobData(['key' => 'value']));
63+
64+
$jobMock->method('__get')
65+
->with('failed')
66+
->willReturn(true);
67+
68+
$container = $this->getContainer();
69+
$container->method('make')
70+
->willThrowException(new BindingResolutionException());
71+
72+
$payload = json_encode(['job' => 'Foo@fire']);
73+
74+
$job = (new QlessJob($container, $this->getQueue(), $this->getJobHandler(), $jobMock, $payload));
75+
$job->fire();
76+
77+
self::assertNull($job->getResolvedJob());
78+
}
79+
80+
public function testFireFailedWithExistentClass(): void
81+
{
82+
$jobMock = $this->getJob();
83+
$jobMock->method('getData')
84+
->willReturn(new JobData(['key' => 'value']));
85+
86+
$jobMock->method('__get')
87+
->with('failed')
88+
->willReturn(true);
89+
90+
$container = $this->getContainer();
91+
$container->method('make')->with(JobStub::class)->willReturn(new JobStub());
92+
93+
$payload = json_encode(['job' => JobStub::class . '@failed', 'data' => []]);
94+
95+
$job = (new QlessJob($container, $this->getQueue(), $this->getJobHandler(), $jobMock, $payload));
96+
$job->fire();
97+
98+
self::assertNotNull($job->getResolvedJob());
99+
}
56100

57101
public function testRelease(): void
58102
{

tests/Stub/JobStub.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace LaravelQless\Tests\Stub;
3+
4+
/**
5+
* Class JobStub
6+
*
7+
* @package LaravelQless\Tests\Stub
8+
*/
9+
class JobStub
10+
{
11+
public function fire()
12+
{
13+
}
14+
15+
public function failed()
16+
{
17+
}
18+
}

0 commit comments

Comments
 (0)