From cf80966d7692beb36a5f0f8f2599cb04b5197f8b Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Tue, 26 Nov 2019 15:06:14 +0100 Subject: [PATCH 1/5] Force data to be an array --- Console/Command/Task/QueueExampleTask.php | 6 +++--- Model/QueuedTask.php | 4 ++-- Test/Case/Model/QueuedTaskTest.php | 18 +++++++++--------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Console/Command/Task/QueueExampleTask.php b/Console/Command/Task/QueueExampleTask.php index 882c3e1..e7a72c5 100644 --- a/Console/Command/Task/QueueExampleTask.php +++ b/Console/Command/Task/QueueExampleTask.php @@ -63,7 +63,7 @@ public function add() { $this->out(' '); // Adding a task of type 'example' with no additionally passed data - if ($this->QueuedTask->createJob('Example', null)) { + if ($this->QueuedTask->createJob('Example', [])) { $this->out(__d('queue', 'OK, job created, now run the worker')); } else { $this->err(__d('queue', 'Could not create Job')); @@ -76,10 +76,10 @@ public function add() { * This function is executed, when a worker is executing a task. * The return parameter will determine, if the task will be marked completed, or be requeued. * - * @param mixed $data Job data (passed on creation) + * @param array $data Job data (passed on creation) * @return bool Success */ - public function run($data) { + public function run(array $data) { $this->hr(); $this->out(__d('queue', 'CakePHP Queue Example task.')); $this->hr(); diff --git a/Model/QueuedTask.php b/Model/QueuedTask.php index 5d0540b..7fa0b11 100644 --- a/Model/QueuedTask.php +++ b/Model/QueuedTask.php @@ -18,11 +18,11 @@ class QueuedTask extends AppModel { * Adds a new Job to the queue. * * @param string $taskName A queue task name - * @param mixed $data Any data + * @param array $data Any data * @param string $notBefore A datetime which indicates when the job may be executed * @return mixed On success `Model::$data` if its not empty or true, false on failure */ - public function createJob($taskName, $data, $notBefore = null) { + public function createJob($taskName, array $data, $notBefore = null) { $data = [ 'task' => $taskName, 'data' => serialize($data), diff --git a/Test/Case/Model/QueuedTaskTest.php b/Test/Case/Model/QueuedTaskTest.php index ea32035..9c0f8e5 100644 --- a/Test/Case/Model/QueuedTaskTest.php +++ b/Test/Case/Model/QueuedTaskTest.php @@ -183,9 +183,9 @@ public function testSequence() { * @return void */ public function testNotBefore() { - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '+ 1 Min')); - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '+ 1 Day')); - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '2009-07-01 12:00:00')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '+ 1 Min')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '+ 1 Day')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '2009-07-01 12:00:00')); $data = $this->QueuedTask->find('all'); $this->assertEqual($data[4]['QueuedTask']['not_before'], date('Y-m-d H:i:s', strtotime('+ 1 Min'))); $this->assertEqual($data[5]['QueuedTask']['not_before'], date('Y-m-d H:i:s', strtotime('+ 1 Day'))); @@ -212,12 +212,12 @@ public function testNotBeforeOrder() { 'retries' => 2 ] ]; - $this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', null)); - $this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', null)); + $this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', [])); + $this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', [])); // Create a task with it's execution target some seconds in the past, so it should jump to the top of the list - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'three', '- 3 Seconds')); - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'two', '- 4 Seconds')); - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'one', '- 5 Seconds')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '- 3 Seconds')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '- 4 Seconds')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '- 5 Seconds')); // When usin requestJob, the jobs we just created should be delivered in this order, // NOT the order in which they where created @@ -284,7 +284,7 @@ public function testRequeueAfterTimeout() { * @return void */ public function testMarkJobFailed() { - $this->QueuedTask->createJob('dummytask', null); + $this->QueuedTask->createJob('dummytask', []); $id = $this->QueuedTask->id; $expected = 'Timeout: 100'; $this->QueuedTask->markJobFailed($id, $expected); From 683130ff6871a8203974fdf866f398a42a544d95 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Tue, 26 Nov 2019 15:09:04 +0100 Subject: [PATCH 2/5] Add return typehint --- Console/Command/Task/QueueExampleTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Console/Command/Task/QueueExampleTask.php b/Console/Command/Task/QueueExampleTask.php index e7a72c5..4de2bff 100644 --- a/Console/Command/Task/QueueExampleTask.php +++ b/Console/Command/Task/QueueExampleTask.php @@ -79,7 +79,7 @@ public function add() { * @param array $data Job data (passed on creation) * @return bool Success */ - public function run(array $data) { + public function run(array $data) : bool { $this->hr(); $this->out(__d('queue', 'CakePHP Queue Example task.')); $this->hr(); From b8843cefe8ebf56c4b9b1cc1d0be6a38cc75207f Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Wed, 27 Nov 2019 12:43:02 +0100 Subject: [PATCH 3/5] Fix failing tests --- Model/QueuedTask.php | 4 ++-- Test/Case/Model/QueuedTaskTest.php | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Model/QueuedTask.php b/Model/QueuedTask.php index 7fa0b11..657054b 100644 --- a/Model/QueuedTask.php +++ b/Model/QueuedTask.php @@ -19,10 +19,10 @@ class QueuedTask extends AppModel { * * @param string $taskName A queue task name * @param array $data Any data - * @param string $notBefore A datetime which indicates when the job may be executed + * @param ?string $notBefore A datetime which indicates when the job may be executed * @return mixed On success `Model::$data` if its not empty or true, false on failure */ - public function createJob($taskName, array $data, $notBefore = null) { + public function createJob(string $taskName, array $data, ?string $notBefore = null) { $data = [ 'task' => $taskName, 'data' => serialize($data), diff --git a/Test/Case/Model/QueuedTaskTest.php b/Test/Case/Model/QueuedTaskTest.php index 9c0f8e5..f465ba1 100644 --- a/Test/Case/Model/QueuedTaskTest.php +++ b/Test/Case/Model/QueuedTaskTest.php @@ -215,32 +215,32 @@ public function testNotBeforeOrder() { $this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', [])); $this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', [])); // Create a task with it's execution target some seconds in the past, so it should jump to the top of the list - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '- 3 Seconds')); - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '- 4 Seconds')); - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '- 5 Seconds')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['three'], '- 3 Seconds')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['two'], '- 4 Seconds')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['one'], '- 5 Seconds')); // When usin requestJob, the jobs we just created should be delivered in this order, // NOT the order in which they where created $expected = [ [ 'name' => 'task1', - 'data' => 'one' + 'data' => ['one'] ], [ 'name' => 'task1', - 'data' => 'two' + 'data' => ['two'] ], [ 'name' => 'task1', - 'data' => 'three' + 'data' => ['three'] ], [ 'name' => 'dummytask', - 'data' => '' + 'data' => [''] ], [ 'name' => 'dummytask', - 'data' => '' + 'data' => [''] ] ]; @@ -265,15 +265,15 @@ public function testRequeueAfterTimeout() { ] ]; - $this->assertTrue((bool)$this->QueuedTask->createJob('task1', '1')); + $this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['1'])); $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['task'], 'task1'); - $this->assertEqual(unserialize($tmp['data']), '1'); + $this->assertEqual(unserialize($tmp['data']), ['1']); $this->assertEqual($tmp['failed_count'], '0'); sleep(2); $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['task'], 'task1'); - $this->assertEqual(unserialize($tmp['data']), '1'); + $this->assertEqual(unserialize($tmp['data']), ['1']); $this->assertEqual($tmp['failed_count'], '1'); $this->assertEqual($tmp['failure_message'], 'Restart after timeout'); } From b5bc62afb570dafbcc7338f63ad59fcf57a8c896 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Wed, 27 Nov 2019 12:44:36 +0100 Subject: [PATCH 4/5] Fix failing tests --- Test/Case/Model/QueuedTaskTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Test/Case/Model/QueuedTaskTest.php b/Test/Case/Model/QueuedTaskTest.php index f465ba1..dab417f 100644 --- a/Test/Case/Model/QueuedTaskTest.php +++ b/Test/Case/Model/QueuedTaskTest.php @@ -236,11 +236,11 @@ public function testNotBeforeOrder() { ], [ 'name' => 'dummytask', - 'data' => [''] + 'data' => [] ], [ 'name' => 'dummytask', - 'data' => [''] + 'data' => [] ] ]; From 8ddd60e4e0643b8976378f753485d151d2647ae5 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Thu, 28 Nov 2019 12:56:56 +0100 Subject: [PATCH 5/5] Fix php 7 issues --- Model/QueuedTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/QueuedTask.php b/Model/QueuedTask.php index 657054b..7825df2 100644 --- a/Model/QueuedTask.php +++ b/Model/QueuedTask.php @@ -22,7 +22,7 @@ class QueuedTask extends AppModel { * @param ?string $notBefore A datetime which indicates when the job may be executed * @return mixed On success `Model::$data` if its not empty or true, false on failure */ - public function createJob(string $taskName, array $data, ?string $notBefore = null) { + public function createJob(string $taskName, array $data, $notBefore = null) { $data = [ 'task' => $taskName, 'data' => serialize($data),