Skip to content

Commit 18bf935

Browse files
pprkutdanhunsaker
authored andcommitted
Add more strict types for Job class properties
1 parent 4a7d48f commit 18bf935

File tree

8 files changed

+22
-38
lines changed

8 files changed

+22
-38
lines changed

lib/Job/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Factory implements FactoryInterface
1313
* @return \Resque\Job\Job
1414
* @throws \Resque\Exceptions\ResqueException
1515
*/
16-
public function create($className, $args, $queue): Job
16+
public function create(string $className, array $args, string $queue): Job
1717
{
1818
if (!class_exists($className)) {
1919
throw new ResqueException(

lib/Job/FactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ interface FactoryInterface
1010
* @param $queue
1111
* @return \Resque\Job\Job
1212
*/
13-
public function create($className, $args, $queue): Job;
13+
public function create(string $className, array $args, string $queue): Job;
1414
}

lib/JobHandler.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Resque;
44

5-
use InvalidArgumentException;
65
use Resque\Job\PID;
76
use Resque\Job\Status;
87
use Resque\Exceptions\DoNotPerformException;
@@ -84,19 +83,13 @@ public function __construct($queue, $payload)
8483
* @param string $prefix The prefix needs to be set for the status key
8584
*
8685
* @return string
87-
* @throws \InvalidArgumentException
8886
*/
89-
public static function create($queue, $class, $args = null, $monitor = false, $id = null, $prefix = "")
87+
public static function create($queue, $class, array $args = [], $monitor = false, $id = null, $prefix = "")
9088
{
9189
if (is_null($id)) {
9290
$id = Resque::generateJobId();
9391
}
9492

95-
if ($args !== null && !is_array($args)) {
96-
throw new InvalidArgumentException(
97-
'Supplied $args must be an array.'
98-
);
99-
}
10093
Resque::push($queue, array(
10194
'class' => $class,
10295
'args' => array($args),
@@ -184,7 +177,7 @@ public function getStatus()
184177
*
185178
* @return array Array of arguments.
186179
*/
187-
public function getArguments()
180+
public function getArguments(): array
188181
{
189182
if (!isset($this->payload['args'])) {
190183
return array();

lib/Resque.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public static function size($queue)
234234
*
235235
* @return string|boolean Job ID when the job was created, false if creation was cancelled due to beforeEnqueue
236236
*/
237-
public static function enqueue($queue, $class, $args = null, $trackStatus = false, $prefix = "")
237+
public static function enqueue($queue, $class, array $args = [], $trackStatus = false, $prefix = "")
238238
{
239239
$id = Resque::generateJobId();
240240
$hookParams = array(

test/Resque/Tests/JobHandlerTest.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,6 @@ public function testQeueuedJobCanBeReserved()
6868
$this->assertEquals('Test_Job', $job->payload['class']);
6969
}
7070

71-
public function testObjectArgumentsCannotBePassedToJob()
72-
{
73-
$this->expectException('InvalidArgumentException');
74-
75-
$args = new stdClass();
76-
$args->test = 'somevalue';
77-
Resque::enqueue('jobs', 'Test_Job', $args);
78-
}
79-
8071
public function testQueuedJobReturnsExactSamePassedInArguments()
8172
{
8273
$args = array(
@@ -168,10 +159,10 @@ public function testJobWithSetUpCallbackFiresSetUp()
168159
{
169160
$payload = array(
170161
'class' => 'Test_Job_With_SetUp',
171-
'args' => array(
162+
'args' => array(array(
172163
'somevar',
173164
'somevar2',
174-
),
165+
)),
175166
);
176167
$job = new JobHandler('jobs', $payload);
177168
$job->perform();
@@ -183,10 +174,10 @@ public function testJobWithTearDownCallbackFiresTearDown()
183174
{
184175
$payload = array(
185176
'class' => 'Test_Job_With_TearDown',
186-
'args' => array(
177+
'args' => array(array(
187178
'somevar',
188179
'somevar2',
189-
),
180+
)),
190181
);
191182
$job = new JobHandler('jobs', $payload);
192183
$job->perform();

test/Resque/Tests/JobPIDTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testQueuedJobDoesNotReturnPID()
3434
$this->logger->expects($this->never())
3535
->method('log');
3636

37-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
37+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
3838
$this->assertEquals(0, PID::get($token));
3939
}
4040

@@ -43,14 +43,14 @@ public function testRunningJobReturnsPID()
4343
// Cannot use InProgress_Job on non-forking OS.
4444
if(!function_exists('pcntl_fork')) return;
4545

46-
$token = Resque::enqueue('jobs', 'InProgress_Job', null, true);
46+
$token = Resque::enqueue('jobs', 'InProgress_Job', [], true);
4747
$this->worker->work(0);
4848
$this->assertNotEquals(0, PID::get($token));
4949
}
5050

5151
public function testFinishedJobDoesNotReturnPID()
5252
{
53-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
53+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
5454
$this->worker->work(0);
5555
$this->assertEquals(0, PID::get($token));
5656
}

test/Resque/Tests/JobStatusTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,28 @@ public static function performResultProvider(): array
5252

5353
public function testJobStatusCanBeTracked()
5454
{
55-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
55+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
5656
$status = new Status($token);
5757
$this->assertTrue($status->isTracking());
5858
}
5959

6060
public function testJobStatusIsReturnedViaJobInstance()
6161
{
62-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
62+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
6363
$job = JobHandler::reserve('jobs');
6464
$this->assertEquals(Status::STATUS_WAITING, $job->getStatus());
6565
}
6666

6767
public function testQueuedJobReturnsQueuedStatus()
6868
{
69-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
69+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
7070
$status = new Status($token);
7171
$this->assertEquals(Status::STATUS_WAITING, $status->get());
7272
}
7373

7474
public function testRunningJobReturnsRunningStatus()
7575
{
76-
$token = Resque::enqueue('jobs', 'Failing_Job', null, true);
76+
$token = Resque::enqueue('jobs', 'Failing_Job', [], true);
7777
$job = $this->worker->reserve();
7878
$this->worker->workingOn($job);
7979
$status = new Status($token);
@@ -82,15 +82,15 @@ public function testRunningJobReturnsRunningStatus()
8282

8383
public function testFailedJobReturnsFailedStatus()
8484
{
85-
$token = Resque::enqueue('jobs', 'Failing_Job', null, true);
85+
$token = Resque::enqueue('jobs', 'Failing_Job', [], true);
8686
$this->worker->work(0);
8787
$status = new Status($token);
8888
$this->assertEquals(Status::STATUS_FAILED, $status->get());
8989
}
9090

9191
public function testCompletedJobReturnsCompletedStatus()
9292
{
93-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
93+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
9494
$this->worker->work(0);
9595
$status = new Status($token);
9696
$this->assertEquals(Status::STATUS_COMPLETE, $status->get());
@@ -120,7 +120,7 @@ public function testCompletedJobReturnsObjectResultAsArray()
120120

121121
public function testStatusIsNotTrackedWhenToldNotTo()
122122
{
123-
$token = Resque::enqueue('jobs', 'Test_Job', null, false);
123+
$token = Resque::enqueue('jobs', 'Test_Job', [], false);
124124
$status = new Status($token);
125125
$this->assertFalse($status->isTracking());
126126
}
@@ -136,7 +136,7 @@ public function testStatusTrackingCanBeStopped()
136136

137137
public function testRecreatedJobWithTrackingStillTracksStatus()
138138
{
139-
$originalToken = Resque::enqueue('jobs', 'Test_Job', null, true);
139+
$originalToken = Resque::enqueue('jobs', 'Test_Job', [], true);
140140
$job = $this->worker->reserve();
141141

142142
// Mark this job as being worked on to ensure that the new status is still

test/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function perform()
137137
class Test_Job_With_SetUp extends \Resque\Job\Job
138138
{
139139
public static $called = false;
140-
public $args = false;
140+
public $args = [];
141141

142142
public function setUp(): void
143143
{
@@ -154,7 +154,7 @@ public function perform()
154154
class Test_Job_With_TearDown extends \Resque\Job\Job
155155
{
156156
public static $called = false;
157-
public $args = false;
157+
public $args = [];
158158

159159
public function perform()
160160
{

0 commit comments

Comments
 (0)