1
|
|
|
<?php |
2
|
|
|
namespace Queue\Test\TestCase\Shell; |
3
|
|
|
|
4
|
|
|
use Cake\Console\Shell; |
5
|
|
|
use Cake\Core\Configure; |
6
|
|
|
use Cake\Datasource\ConnectionManager; |
7
|
|
|
use Cake\TestSuite\TestCase; |
8
|
|
|
use Queue\Shell\QueueShell; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* QueueShell Wrapper. |
12
|
|
|
*/ |
13
|
|
|
class QueueShellWrapper extends QueueShell |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A list with error messages. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $_err = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A list with out messages. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $_out = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Test double of `parent::err`. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function err($message = null, $newlines = 1) |
36
|
|
|
{ |
37
|
|
|
$this->_err[] = $message; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test double of `parent::out`. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function out($message = null, $newlines = 1, $level = Shell::NORMAL) |
46
|
|
|
{ |
47
|
|
|
$this->_out[] = $message; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Test double of `parent::_stop`. |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
protected function _stop($status = 0) |
56
|
|
|
{ |
57
|
|
|
return $status; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
class QueueShellTest extends TestCase |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @var QueueShellWrapper |
67
|
|
|
*/ |
68
|
|
|
public $QueueShell; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Fixtures to load |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
public $fixtures = [ |
76
|
|
|
'plugin.Queue.QueuedTasks' |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Setup Defaults |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function setUp() |
85
|
|
|
{ |
86
|
|
|
parent::setUp(); |
87
|
|
|
|
88
|
|
|
$this->QueueShell = $this->getMockBuilder(QueueShellWrapper::class) |
|
|
|
|
89
|
|
|
->setMethods([ |
90
|
|
|
'in', |
91
|
|
|
'err', |
92
|
|
|
'_stop' |
93
|
|
|
]) |
94
|
|
|
->getMock(); |
95
|
|
|
|
96
|
|
|
$this->QueueShell->initialize(); |
|
|
|
|
97
|
|
|
|
98
|
|
|
Configure::write('Queue', [ |
99
|
|
|
'sleepTime' => 2, |
100
|
|
|
'defaultWorkerTimeout' => 3, |
101
|
|
|
'workerMaxRuntime' => 5, |
102
|
|
|
'cleanupTimeout' => 10, |
103
|
|
|
'exitWhenNothingToDo' => false, |
104
|
|
|
'log' => false |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function testObject() |
113
|
|
|
{ |
114
|
|
|
$this->assertTrue(is_object($this->QueueShell)); |
115
|
|
|
$this->assertInstanceOf(QueueShell::class, $this->QueueShell); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function testStats() |
123
|
|
|
{ |
124
|
|
|
$this->_needsConnection(); |
125
|
|
|
|
126
|
|
|
$this->QueueShell->stats(); |
127
|
|
|
$this->assertContains('Total unfinished jobs: 0', $this->out); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function testSettings() |
135
|
|
|
{ |
136
|
|
|
$this->QueueShell->settings(); |
137
|
|
|
$this->assertContains('* cleanuptimeout: 10', $this->out); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function testAddInexistent() |
145
|
|
|
{ |
146
|
|
|
$this->QueueShell->args[] = 'FooBar'; |
147
|
|
|
$this->QueueShell->add(); |
148
|
|
|
$this->assertContains('Error: Task not found: FooBar', $this->out); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function testAdd() |
156
|
|
|
{ |
157
|
|
|
$this->QueueShell->args[] = 'Example'; |
158
|
|
|
$this->QueueShell->add(); |
159
|
|
|
|
160
|
|
|
$this->assertContains('OK, job created, now run the worker', $this->out, print_r($this->out->output, true)); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
public function testRetry() |
168
|
|
|
{ |
169
|
|
|
$file = TMP . 'task_retry.txt'; |
170
|
|
|
if (file_exists($file)) { |
171
|
|
|
unlink($file); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->_needsConnection(); |
175
|
|
|
|
176
|
|
|
$this->QueueShell->args[] = 'RetryExample'; |
177
|
|
|
$this->QueueShell->add(); |
178
|
|
|
|
179
|
|
|
$expected = 'This is a very simple example of a QueueTask and how retries work'; |
180
|
|
|
$this->assertContains($expected, $this->out); |
|
|
|
|
181
|
|
|
|
182
|
|
|
$this->QueueShell->runworker(); |
183
|
|
|
|
184
|
|
|
$this->assertContains('Job did not finish, requeued after try 1.', $this->out); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function testTimeNeeded() |
192
|
|
|
{ |
193
|
|
|
$this->QueueShell = $this->getMockBuilder(QueueShell::class) |
|
|
|
|
194
|
|
|
->setMethods([ |
195
|
|
|
'_time' |
196
|
|
|
]) |
197
|
|
|
->getMock(); |
198
|
|
|
|
199
|
|
|
$first = time(); |
200
|
|
|
$second = $first - HOUR + MINUTE; |
201
|
|
|
$this->QueueShell->expects($this->at(0)) |
202
|
|
|
->method('_time') |
203
|
|
|
->will($this->returnValue($first)); |
204
|
|
|
$this->QueueShell->expects($this->at(1)) |
205
|
|
|
->method('_time') |
206
|
|
|
->will($this->returnValue($second)); |
207
|
|
|
$this->QueueShell->expects($this->exactly(2)) |
208
|
|
|
->method('_time') |
209
|
|
|
->withAnyParameters(); |
210
|
|
|
|
211
|
|
|
$result = $this->invokeMethod($this->QueueShell, '_timeNeeded'); |
|
|
|
|
212
|
|
|
$this->assertSame('3540s', $result); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function testMemoryUsage() |
220
|
|
|
{ |
221
|
|
|
$result = $this->invokeMethod($this->QueueShell, '_memoryUsage'); |
222
|
|
|
$this->assertRegExp('/^\d+MB/', $result, 'Should be e.g. `17MB` or `17MB/1GB` etc.'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
public function testStringToArray() |
230
|
|
|
{ |
231
|
|
|
$string = 'Foo,Bar,'; |
232
|
|
|
$result = $this->invokeMethod($this->QueueShell, '_stringToArray', [ |
233
|
|
|
$string |
234
|
|
|
]); |
235
|
|
|
|
236
|
|
|
$expected = [ |
237
|
|
|
'Foo', |
238
|
|
|
'Bar' |
239
|
|
|
]; |
240
|
|
|
$this->assertSame($expected, $result); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Helper method for skipping tests that need a real connection. |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
protected function _needsConnection() |
249
|
|
|
{ |
250
|
|
|
$config = ConnectionManager::getConfig('test'); |
251
|
|
|
$this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..