Skip to content

Commit bdceb43

Browse files
committed
Fix queue shell task loading and ide autocomplete.
1 parent 969fad7 commit bdceb43

File tree

13 files changed

+252
-43
lines changed

13 files changed

+252
-43
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"psr-4": {
4545
"Queue\\Test\\": "tests/",
4646
"App\\": "tests/test_app/src/",
47+
"Foo\\": "tests/test_app/plugins/Foo/src/",
4748
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
4849
}
4950
},

docs/CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Testing MySQL
2+
3+
By default it will usually use SQLite DB (out of the box available).
4+
Not all tests currently work with SQLite or any non MySQL db yet.
5+
6+
If you want to run all tests, including MySQL ones, you need to set
7+
```
8+
export db_dsn="mysql://root:[email protected]/cake_test"
9+
```
10+
before you actually run
11+
```
12+
php phpunit.phar
13+
```
14+
15+
Make sure such a `cake_test` database exists.

src/Generator/Task/QueuedJobTask.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ protected function collectQueuedJobTasks() {
4545

4646
foreach ($tasks as $task) {
4747
$className = App::className($task, 'Shell/Task', 'Task');
48-
49-
if (substr($task, 0, 6) === 'Queue.') {
50-
$task = 'Queue.' . substr($task, 11);
51-
} else {
52-
$task = substr($task, 5);
53-
}
48+
list(, $task) = pluginSplit($task);
49+
$task = substr($task, 5);
5450
$result[$task] = $className;
5551
}
5652

src/Model/Table/QueuedJobsTable.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ public function clearDoublettes() {
504504
$this->deleteAll([
505505
'id' => array_slice($x, $start, 10),
506506
]);
507-
//debug(array_slice($x, $start, 10));
508507
$start = $start + 100;
509508
}
510509
}

src/Queue/TaskFinder.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class TaskFinder {
1414
protected $tasks;
1515

1616
/**
17+
* Returns all possible Queue tasks.
18+
*
19+
* Makes sure that app tasks are prioritized over plugin ones.
20+
*
1721
* @return array
1822
*/
1923
public function allAppAndPluginTasks() {
@@ -26,26 +30,53 @@ public function allAppAndPluginTasks() {
2630

2731
foreach ($paths as $path) {
2832
$Folder = new Folder($path);
29-
$res = array_merge($this->tasks, $Folder->find('Queue.+\.php'));
30-
foreach ($res as &$r) {
31-
$r = basename($r, 'Task.php');
32-
}
33-
$this->tasks = $res;
33+
$this->tasks = $this->getAppPaths($Folder);
3434
}
3535
$plugins = Plugin::loaded();
3636
foreach ($plugins as $plugin) {
3737
$pluginPaths = App::path('Shell/Task', $plugin);
3838
foreach ($pluginPaths as $pluginPath) {
3939
$Folder = new Folder($pluginPath);
40-
$res = $Folder->find('Queue.+Task\.php');
41-
foreach ($res as &$r) {
42-
$r = $plugin . '.' . basename($r, 'Task.php');
43-
}
44-
$this->tasks = array_merge($this->tasks, $res);
40+
$pluginTasks = $this->getPluginPaths($Folder, $plugin);
41+
$this->tasks = array_merge($this->tasks, $pluginTasks);
4542
}
4643
}
4744

4845
return $this->tasks;
4946
}
5047

48+
/**
49+
* @param \Cake\Filesystem\Folder $Folder
50+
*
51+
* @return array
52+
*/
53+
protected function getAppPaths(Folder $Folder) {
54+
$res = array_merge($this->tasks, $Folder->find('Queue.+\.php'));
55+
foreach ($res as &$r) {
56+
$r = basename($r, 'Task.php');
57+
}
58+
59+
return $res;
60+
}
61+
62+
/**
63+
* @param \Cake\Filesystem\Folder $Folder
64+
* @param string $plugin
65+
*
66+
* @return array
67+
*/
68+
protected function getPluginPaths(Folder $Folder, $plugin) {
69+
$res = $Folder->find('Queue.+Task\.php');
70+
foreach ($res as $key => $r) {
71+
$name = basename($r, 'Task.php');
72+
if (in_array($name, $this->tasks)) {
73+
unset($res[$key]);
74+
continue;
75+
}
76+
$res[$key] = $plugin . '.' . $name;
77+
}
78+
79+
return $res;
80+
}
81+
5182
}

tests/TestCase/Controller/QueueControllerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Queue\Test\TestCase\Controller;
33

4+
use Cake\Datasource\ConnectionManager;
45
use Cake\TestSuite\IntegrationTestCase;
56

67
/**
@@ -23,9 +24,21 @@ class QueueControllerTest extends IntegrationTestCase {
2324
* @return void
2425
*/
2526
public function testIndex() {
27+
$this->_needsConnection();
28+
2629
$this->get(['prefix' => 'admin', 'plugin' => 'Queue', 'controller' => 'Queue', 'action' => 'index']);
2730

2831
$this->assertResponseCode(200);
2932
}
3033

34+
/**
35+
* Helper method for skipping tests that need a real connection.
36+
*
37+
* @return void
38+
*/
39+
protected function _needsConnection() {
40+
$config = ConnectionManager::config('test');
41+
$this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.');
42+
}
43+
3144
}

tests/TestCase/Generator/Task/QueuedJobGeneratorTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ public function testCollect() {
2828

2929
$expected = [
3030
'\Queue\Model\Table\QueuedJobsTable::createJob(0)' => [
31-
'Queue.Email' => '\Queue\Shell\Task\QueueEmailTask::class',
32-
'Queue.Example' => '\Queue\Shell\Task\QueueExampleTask::class',
33-
'Queue.Execute' => '\Queue\Shell\Task\QueueExecuteTask::class',
34-
'Queue.ProgressExample' => '\Queue\Shell\Task\QueueProgressExampleTask::class',
35-
'Queue.RetryExample' => '\Queue\Shell\Task\QueueRetryExampleTask::class',
36-
'Queue.SuperExample' => '\Queue\Shell\Task\QueueSuperExampleTask::class',
31+
'Email' => '\Queue\Shell\Task\QueueEmailTask::class',
32+
'Example' => '\Queue\Shell\Task\QueueExampleTask::class',
33+
'Execute' => '\Queue\Shell\Task\QueueExecuteTask::class',
34+
'ProgressExample' => '\Queue\Shell\Task\QueueProgressExampleTask::class',
35+
'RetryExample' => '\Queue\Shell\Task\QueueRetryExampleTask::class',
36+
'SuperExample' => '\Queue\Shell\Task\QueueSuperExampleTask::class',
37+
'Foo' => '\App\Shell\Task\QueueFooTask::class',
3738
],
3839
];
3940
$this->assertSame($expected, $result);

tests/TestCase/Model/Table/QueuedJobsTableTest.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Queue\Test\TestCase\Model\Table;
99

10+
use Cake\Datasource\ConnectionManager;
1011
use Cake\I18n\Time;
1112
use Cake\ORM\TableRegistry;
1213
use Cake\TestSuite\TestCase;
@@ -100,6 +101,8 @@ public function testCreateAndCount() {
100101
* @return void
101102
*/
102103
public function testCreateAndFetch() {
104+
$this->_needsConnection();
105+
103106
//$capabilities is a list of tasks the worker can run.
104107
$capabilities = [
105108
'task1' => [
@@ -152,6 +155,8 @@ public function testCreateAndFetch() {
152155
* @return void
153156
*/
154157
public function testSequence() {
158+
$this->_needsConnection();
159+
155160
//$capabilities is a list of tasks the worker can run.
156161
$capabilities = [
157162
'task1' => [
@@ -176,9 +181,7 @@ public function testSequence() {
176181
foreach (range(0, 4) as $num) {
177182
$this->QueuedJobs->clearKey();
178183
$array[$num] = $this->QueuedJobs->requestJob($capabilities);
179-
//debug($job);ob_flush();
180184
$jobData = unserialize($array[$num]['data']);
181-
//debug($jobData);ob_flush();
182185
$this->assertEquals($num, $jobData['tasknum']);
183186
}
184187
// now mark them as done
@@ -217,9 +220,11 @@ public function testNotBefore() {
217220
* Test Job reordering depending on 'notBefore' field.
218221
* Jobs with an expired notbefore field should be executed before any other job without specific timing info.
219222
*
220-
* @return null
223+
* @return void
221224
*/
222225
public function testNotBeforeOrder() {
226+
$this->_needsConnection();
227+
223228
$capabilities = [
224229
'task1' => [
225230
'name' => 'task1',
@@ -279,6 +284,8 @@ public function testNotBeforeOrder() {
279284
* @return void
280285
*/
281286
public function testRateLimit() {
287+
$this->_needsConnection();
288+
282289
$capabilities = [
283290
'task1' => [
284291
'name' => 'task1',
@@ -363,7 +370,7 @@ public function testRateLimit() {
363370
}
364371

365372
/**
366-
* Are those tests still valid?
373+
* Are those tests still valid? //FIXME
367374
*
368375
* @return void
369376
*/
@@ -401,7 +408,7 @@ public function _testRequeueAfterTimeout() {
401408
* Tests whether the timeout of second tasks doesn't interfere with
402409
* requeue of tasks
403410
*
404-
* Are those tests still valid?
411+
* Are those tests still valid? //FIXME
405412
*
406413
* @return void
407414
*/
@@ -444,6 +451,8 @@ public function _testRequeueAfterTimeout2() {
444451
* @return void
445452
*/
446453
public function testRequestGroup() {
454+
$this->_needsConnection();
455+
447456
$capabilities = [
448457
'task1' => [
449458
'name' => 'task1',
@@ -513,6 +522,8 @@ public function testRequestGroup() {
513522
* @return void
514523
*/
515524
public function testPriority() {
525+
$this->_needsConnection();
526+
516527
$capabilities = [
517528
'task1' => [
518529
'name' => 'task1',
@@ -536,4 +547,14 @@ public function testPriority() {
536547
$this->assertSame(['key' => 'k2'], $data);
537548
}
538549

550+
/**
551+
* Helper method for skipping tests that need a real connection.
552+
*
553+
* @return void
554+
*/
555+
protected function _needsConnection() {
556+
$config = ConnectionManager::config('test');
557+
$this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.');
558+
}
559+
539560
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Queue\Test\TestCase\Shell;
4+
5+
use Cake\TestSuite\TestCase;
6+
use Queue\Queue\TaskFinder;
7+
8+
class TaskFinderTest extends TestCase {
9+
10+
/**
11+
* @var \Queue\Shell\QueueShell|\PHPUnit_Framework_MockObject_MockObject
12+
*/
13+
public $QueueShell;
14+
15+
/**
16+
* @var \Tools\TestSuite\ConsoleOutput
17+
*/
18+
public $out;
19+
20+
/**
21+
* @var \Tools\TestSuite\ConsoleOutput
22+
*/
23+
public $err;
24+
25+
/**
26+
* Fixtures to load
27+
*
28+
* @var array
29+
*/
30+
public $fixtures = [
31+
'plugin.Queue.QueuedJobs',
32+
'plugin.Queue.QueueProcesses',
33+
];
34+
35+
/**
36+
* Setup Defaults
37+
*
38+
* @return void
39+
*/
40+
public function setUp() {
41+
parent::setUp();
42+
}
43+
44+
/**
45+
* @return void
46+
*/
47+
public function testAllAppAndPluginTasks() {
48+
$this->taskFinder = new TaskFinder();
49+
50+
$result = $this->taskFinder->allAppAndPluginTasks();
51+
$this->assertCount(7, $result);
52+
$this->assertArraySubset(['QueueFoo'], $result);
53+
$this->assertTrue(!in_array('Foo.QueueFoo', $result));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)