Skip to content

Commit 05f13e3

Browse files
committed
Require every job to extend the base Job class
1 parent bb5c8d2 commit 05f13e3

File tree

8 files changed

+83
-58
lines changed

8 files changed

+83
-58
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Resque\Resque::enqueue('default', 'My_Job', $args);
110110
Each job should be in its own class, and include a `perform` method.
111111

112112
```php
113-
class My_Job
113+
class My_Job extends \Resque\Job\Job
114114
{
115115
public function perform()
116116
{
@@ -132,9 +132,9 @@ defined, it will be called before the `perform` method is run. The `tearDown`
132132
method, if defined, will be called after the job finishes.
133133

134134
```php
135-
class My_Job
135+
class My_Job extends \Resque\Job\Job
136136
{
137-
public function setUp()
137+
public function setUp(): void
138138
{
139139
// ... Set up environment for this job
140140
}
@@ -144,7 +144,7 @@ class My_Job
144144
// .. Run job
145145
}
146146

147-
public function tearDown()
147+
public function tearDown(): void
148148
{
149149
// ... Remove environment for this job
150150
}

lib/Job/Factory.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,17 @@ class Factory implements FactoryInterface
1010
* @param $className
1111
* @param array $args
1212
* @param $queue
13-
* @return \Resque\Job\JobInterface
13+
* @return \Resque\Job\Job
1414
* @throws \Resque\Exceptions\ResqueException
1515
*/
16-
public function create($className, $args, $queue)
16+
public function create($className, $args, $queue): Job
1717
{
1818
if (!class_exists($className)) {
1919
throw new ResqueException(
2020
'Could not find job class ' . $className . '.'
2121
);
2222
}
2323

24-
if (!method_exists($className, 'perform')) {
25-
throw new ResqueException(
26-
'Job class ' . $className . ' does not contain a perform method.'
27-
);
28-
}
29-
3024
$instance = new $className();
3125
$instance->args = $args;
3226
$instance->queue = $queue;

lib/Job/FactoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface FactoryInterface
88
* @param $className
99
* @param array $args
1010
* @param $queue
11-
* @return \Resque\Job\JobInterface
11+
* @return \Resque\Job\Job
1212
*/
13-
public function create($className, $args, $queue);
13+
public function create($className, $args, $queue): Job;
1414
}

lib/Job/JobInterface.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/JobHandler.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Resque\Exceptions\DoNotPerformException;
99
use Resque\Job\FactoryInterface;
1010
use Resque\Job\Factory;
11+
use Resque\Job\Job;
1112
use Error;
1213

1314
/**
@@ -50,7 +51,7 @@ class JobHandler
5051
public $endTime;
5152

5253
/**
53-
* @var object|\Resque\Job\JobInterface Instance of the class performing work for this job.
54+
* @var Job Instance of the class performing work for this job.
5455
*/
5556
private $instance;
5657

@@ -194,10 +195,10 @@ public function getArguments()
194195

195196
/**
196197
* Get the instantiated object for this job that will be performing work.
197-
* @return \Resque\Job\JobInterface Instance of the object that this job belongs to.
198+
* @return \Resque\Job\Job Instance of the object that this job belongs to.
198199
* @throws \Resque\Exceptions\ResqueException
199200
*/
200-
public function getInstance()
201+
public function getInstance(): Job
201202
{
202203
if (!is_null($this->instance)) {
203204
return $this->instance;
@@ -212,9 +213,8 @@ public function getInstance()
212213
* Actually execute a job by calling the perform method on the class
213214
* associated with the job with the supplied arguments.
214215
*
215-
* @return bool
216-
* @throws Resque\Exceptions\ResqueException When the job's class could not be found
217-
* or it does not contain a perform method.
216+
* @return mixed
217+
* @throws Resque\Exceptions\ResqueException When the job's class could not be found.
218218
*/
219219
public function perform()
220220
{
@@ -225,15 +225,12 @@ public function perform()
225225
$this->startTime = microtime(true);
226226

227227
$instance = $this->getInstance();
228-
if (is_callable([$instance, 'setUp'])) {
229-
$instance->setUp();
230-
}
228+
229+
$instance->setUp();
231230

232231
$result = $instance->perform();
233232

234-
if (is_callable([$instance, 'tearDown'])) {
235-
$instance->tearDown();
236-
}
233+
$instance->tearDown();
237234

238235
$this->endTime = microtime(true);
239236

@@ -343,7 +340,7 @@ public function setJobFactory(FactoryInterface $jobFactory)
343340
/**
344341
* @return Resque\Job\FactoryInterface
345342
*/
346-
public function getJobFactory()
343+
public function getJobFactory(): FactoryInterface
347344
{
348345
if ($this->jobFactory === null) {
349346
$this->jobFactory = new Factory();

test/Resque/Tests/JobHandlerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use \Resque\Redis;
88
use \Resque\JobHandler;
99
use \Resque\Stat;
10-
use \Resque\Job\JobInterface;
10+
use \Resque\Job\Job;
1111
use \Resque\Job\FactoryInterface;
1212
use \Test_Job_With_SetUp;
1313
use \Test_Job_With_TearDown;
@@ -410,7 +410,7 @@ public function testUseFactoryToGetJobInstance()
410410
$factory = new Some_Stub_Factory();
411411
$job->setJobFactory($factory);
412412
$instance = $job->getInstance();
413-
$this->assertInstanceOf('Resque\Job\JobInterface', $instance);
413+
$this->assertInstanceOf('Resque\Job\Job', $instance);
414414
}
415415

416416
public function testDoNotUseFactoryToGetInstance()
@@ -422,11 +422,11 @@ public function testDoNotUseFactoryToGetInstance()
422422
$job = new JobHandler('jobs', $payload);
423423
$factory = $this->getMockBuilder('Resque\Job\FactoryInterface')
424424
->getMock();
425-
$testJob = $this->getMockBuilder('Resque\Job\JobInterface')
425+
$testJob = $this->getMockBuilder('Resque\Job\Job')
426426
->getMock();
427427
$factory->expects(self::never())->method('create')->will(self::returnValue($testJob));
428428
$instance = $job->getInstance();
429-
$this->assertInstanceOf('Resque\Job\JobInterface', $instance);
429+
$this->assertInstanceOf('Resque\Job\Job', $instance);
430430
}
431431

432432
public function testJobStatusIsNullIfIdMissingFromPayload()
@@ -505,7 +505,7 @@ public function testJobHandlerSetsStartAndEndTimeForFailedJob()
505505
}
506506
}
507507

508-
class Some_Job_Class implements JobInterface
508+
class Some_Job_Class extends Job
509509
{
510510

511511
/**
@@ -524,9 +524,9 @@ class Some_Stub_Factory implements FactoryInterface
524524
* @param $className
525525
* @param array $args
526526
* @param $queue
527-
* @return Resque\Job\JobInterface
527+
* @return Resque\Job\Job
528528
*/
529-
public function create($className, $args, $queue)
529+
public function create($className, $args, $queue): Job
530530
{
531531
return new Some_Job_Class();
532532
}

test/Resque/Tests/JobStatusTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use \Resque\Job\Status;
77
use \Resque\JobHandler;
88
use \Resque\Resque;
9+
use \stdClass;
910

1011
/**
1112
* Status tests.
@@ -30,6 +31,25 @@ public function setUp(): void
3031
$this->worker->setLogger($this->logger);
3132
}
3233

34+
/**
35+
* Unit test data provider for potential return values from perform().
36+
*
37+
* @return array
38+
*/
39+
public static function performResultProvider(): array
40+
{
41+
$data = [];
42+
43+
$data['boolean'] = [ true ];
44+
$data['float'] = [ 1.0 ];
45+
$data['integer'] = [ 100 ];
46+
$data['string'] = [ 'string' ];
47+
$data['null'] = [ null ];
48+
$data['array'] = [[ 'key' => 'value' ]];
49+
50+
return $data;
51+
}
52+
3353
public function testJobStatusCanBeTracked()
3454
{
3555
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
@@ -76,6 +96,28 @@ public function testCompletedJobReturnsCompletedStatus()
7696
$this->assertEquals(Status::STATUS_COMPLETE, $status->get());
7797
}
7898

99+
/**
100+
* @param mixed $value Potential return value from perform()
101+
*
102+
* @dataProvider performResultProvider
103+
*/
104+
public function testCompletedJobReturnsResult($value)
105+
{
106+
$token = Resque::enqueue('jobs', 'Returning_Job', [ 'return' => $value ], true);
107+
$this->worker->work(0);
108+
$status = new Status($token);
109+
$this->assertEquals($value, $status->result());
110+
}
111+
112+
public function testCompletedJobReturnsObjectResultAsArray()
113+
{
114+
$value = new stdClass();
115+
$token = Resque::enqueue('jobs', 'Returning_Job', [ 'return' => $value ], true);
116+
$this->worker->work(0);
117+
$status = new Status($token);
118+
$this->assertEquals([], $status->result());
119+
}
120+
79121
public function testStatusIsNotTrackedWhenToldNotTo()
80122
{
81123
$token = Resque::enqueue('jobs', 'Test_Job', null, false);

test/bootstrap.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function sigint()
8585
pcntl_signal(SIGTERM, 'sigint');
8686
}
8787

88-
class Test_Job
88+
class Test_Job extends \Resque\Job\Job
8989
{
9090
public static $called = false;
9191

@@ -95,12 +95,20 @@ public function perform()
9595
}
9696
}
9797

98+
class Returning_Job extends \Resque\Job\Job
99+
{
100+
public function perform()
101+
{
102+
return $this->args['return'] ?? NULL;
103+
}
104+
}
105+
98106
class Failing_Job_Exception extends \Exception
99107
{
100108

101109
}
102110

103-
class Failing_Job
111+
class Failing_Job extends \Resque\Job\Job
104112
{
105113
public function perform()
106114
{
@@ -114,7 +122,7 @@ public function perform()
114122
*
115123
* CAUTION Use this test job only with Worker::work, i.e. only when you actually trigger the fork in tests.
116124
*/
117-
class InProgress_Job
125+
class InProgress_Job extends \Resque\Job\Job
118126
{
119127
public function perform()
120128
{
@@ -126,17 +134,12 @@ public function perform()
126134
}
127135
}
128136

129-
class Test_Job_Without_Perform_Method
130-
{
131-
132-
}
133-
134-
class Test_Job_With_SetUp
137+
class Test_Job_With_SetUp extends \Resque\Job\Job
135138
{
136139
public static $called = false;
137140
public $args = false;
138141

139-
public function setUp()
142+
public function setUp(): void
140143
{
141144
self::$called = true;
142145
}
@@ -148,7 +151,7 @@ public function perform()
148151
}
149152

150153

151-
class Test_Job_With_TearDown
154+
class Test_Job_With_TearDown extends \Resque\Job\Job
152155
{
153156
public static $called = false;
154157
public $args = false;
@@ -158,13 +161,13 @@ public function perform()
158161

159162
}
160163

161-
public function tearDown()
164+
public function tearDown(): void
162165
{
163166
self::$called = true;
164167
}
165168
}
166169

167-
class Test_Infinite_Recursion_Job
170+
class Test_Infinite_Recursion_Job extends \Resque\Job\Job
168171
{
169172
public function perform()
170173
{

0 commit comments

Comments
 (0)