Skip to content

Commit fd7896e

Browse files
committed
Add more strict types for Job class properties
1 parent b56df98 commit fd7896e

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
@@ -32,28 +32,28 @@ public function setUp(): void
3232

3333
public function testJobStatusCanBeTracked()
3434
{
35-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
35+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
3636
$status = new Status($token);
3737
$this->assertTrue($status->isTracking());
3838
}
3939

4040
public function testJobStatusIsReturnedViaJobInstance()
4141
{
42-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
42+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
4343
$job = JobHandler::reserve('jobs');
4444
$this->assertEquals(Status::STATUS_WAITING, $job->getStatus());
4545
}
4646

4747
public function testQueuedJobReturnsQueuedStatus()
4848
{
49-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
49+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
5050
$status = new Status($token);
5151
$this->assertEquals(Status::STATUS_WAITING, $status->get());
5252
}
5353

5454
public function testRunningJobReturnsRunningStatus()
5555
{
56-
$token = Resque::enqueue('jobs', 'Failing_Job', null, true);
56+
$token = Resque::enqueue('jobs', 'Failing_Job', [], true);
5757
$job = $this->worker->reserve();
5858
$this->worker->workingOn($job);
5959
$status = new Status($token);
@@ -62,23 +62,23 @@ public function testRunningJobReturnsRunningStatus()
6262

6363
public function testFailedJobReturnsFailedStatus()
6464
{
65-
$token = Resque::enqueue('jobs', 'Failing_Job', null, true);
65+
$token = Resque::enqueue('jobs', 'Failing_Job', [], true);
6666
$this->worker->work(0);
6767
$status = new Status($token);
6868
$this->assertEquals(Status::STATUS_FAILED, $status->get());
6969
}
7070

7171
public function testCompletedJobReturnsCompletedStatus()
7272
{
73-
$token = Resque::enqueue('jobs', 'Test_Job', null, true);
73+
$token = Resque::enqueue('jobs', 'Test_Job', [], true);
7474
$this->worker->work(0);
7575
$status = new Status($token);
7676
$this->assertEquals(Status::STATUS_COMPLETE, $status->get());
7777
}
7878

7979
public function testStatusIsNotTrackedWhenToldNotTo()
8080
{
81-
$token = Resque::enqueue('jobs', 'Test_Job', null, false);
81+
$token = Resque::enqueue('jobs', 'Test_Job', [], false);
8282
$status = new Status($token);
8383
$this->assertFalse($status->isTracking());
8484
}
@@ -94,7 +94,7 @@ public function testStatusTrackingCanBeStopped()
9494

9595
public function testRecreatedJobWithTrackingStillTracksStatus()
9696
{
97-
$originalToken = Resque::enqueue('jobs', 'Test_Job', null, true);
97+
$originalToken = Resque::enqueue('jobs', 'Test_Job', [], true);
9898
$job = $this->worker->reserve();
9999

100100
// 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
@@ -129,7 +129,7 @@ public function perform(): void
129129
class Test_Job_With_SetUp extends \Resque\Job\Job
130130
{
131131
public static $called = false;
132-
public $args = false;
132+
public $args = [];
133133

134134
public function setUp(): void
135135
{
@@ -146,7 +146,7 @@ public function perform(): void
146146
class Test_Job_With_TearDown extends \Resque\Job\Job
147147
{
148148
public static $called = false;
149-
public $args = false;
149+
public $args = [];
150150

151151
public function perform(): void
152152
{

0 commit comments

Comments
 (0)