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
|
|
|
* |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* @see \Cake\Console\Shell::_stop() |
54
|
|
|
*/ |
55
|
|
|
protected function _stop($status = 0) |
56
|
|
|
{ |
57
|
|
|
return $status; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
* @see \Queue\Shell\QueueShell::_timeNeeded() |
64
|
|
|
*/ |
65
|
|
|
public function timeNeeded() |
66
|
|
|
{ |
67
|
|
|
return parent::_timeNeeded(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
* @see \Queue\Shell\QueueShell::_memoryUsage() |
74
|
|
|
*/ |
75
|
|
|
public function memoryUsage() |
76
|
|
|
{ |
77
|
|
|
return parent::_memoryUsage(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
* @see \Queue\Shell\QueueShell::_stringToArray() |
84
|
|
|
*/ |
85
|
|
|
public function stringToArray($param) { |
86
|
|
|
return parent::_stringToArray($param); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
class QueueShellTest extends TestCase |
91
|
|
|
{ |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* @var QueueShellWrapper |
96
|
|
|
*/ |
97
|
|
|
public $QueueShell; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Fixtures to load |
101
|
|
|
* |
102
|
|
|
* @var array |
103
|
|
|
*/ |
104
|
|
|
public $fixtures = [ |
105
|
|
|
'plugin.Queue.QueuedTasks' |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Setup Defaults |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function setUp() |
114
|
|
|
{ |
115
|
|
|
parent::setUp(); |
116
|
|
|
|
117
|
|
|
$this->QueueShell = $this->getMockBuilder(QueueShellWrapper::class) |
|
|
|
|
118
|
|
|
->setMethods([ |
119
|
|
|
'in', |
120
|
|
|
'err', |
121
|
|
|
'_stop' |
122
|
|
|
]) |
123
|
|
|
->getMock(); |
124
|
|
|
|
125
|
|
|
$this->QueueShell->initialize(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
Configure::write('Queue', [ |
128
|
|
|
'sleepTime' => 2, |
129
|
|
|
'defaultWorkerTimeout' => 3, |
130
|
|
|
'workerMaxRuntime' => 5, |
131
|
|
|
'cleanupTimeout' => 10, |
132
|
|
|
'exitWhenNothingToDo' => false, |
133
|
|
|
'log' => false |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function testObject() |
142
|
|
|
{ |
143
|
|
|
$this->assertTrue(is_object($this->QueueShell)); |
144
|
|
|
$this->assertInstanceOf(QueueShell::class, $this->QueueShell); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
public function testStats() |
152
|
|
|
{ |
153
|
|
|
$this->_needsConnection(); |
154
|
|
|
|
155
|
|
|
$this->QueueShell->stats(); |
156
|
|
|
$this->assertContains('Total unfinished jobs: 0', $this->QueueShell->_out); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
public function testSettings() |
164
|
|
|
{ |
165
|
|
|
$this->QueueShell->settings(); |
166
|
|
|
$this->assertContains('* cleanuptimeout: 10', $this->QueueShell->_out); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
public function testAddInexistent() |
174
|
|
|
{ |
175
|
|
|
$this->QueueShell->args[] = 'FooBar'; |
176
|
|
|
$this->QueueShell->add(); |
177
|
|
|
$this->assertContains('Error: Task not found: FooBar', $this->QueueShell->_out); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* |
182
|
|
|
* @return void |
183
|
|
|
*/ |
184
|
|
|
public function testAdd() |
185
|
|
|
{ |
186
|
|
|
$this->QueueShell->args[] = 'Example'; |
187
|
|
|
$this->QueueShell->add(); |
188
|
|
|
|
189
|
|
|
$this->assertContains('OK, job created, now run the worker', $this->QueueShell->_out, print_r($this->QueueShell->_out, true)); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function testRetry() |
197
|
|
|
{ |
198
|
|
|
$file = TMP . 'task_retry.txt'; |
199
|
|
|
if (file_exists($file)) { |
200
|
|
|
unlink($file); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->_needsConnection(); |
204
|
|
|
|
205
|
|
|
$this->QueueShell->args[] = 'RetryExample'; |
206
|
|
|
$this->QueueShell->add(); |
207
|
|
|
|
208
|
|
|
$expected = 'This is a very simple example of a QueueTask and how retries work'; |
209
|
|
|
$this->assertContains($expected, $this->QueueShell->_out); |
|
|
|
|
210
|
|
|
|
211
|
|
|
$this->QueueShell->runworker(); |
212
|
|
|
|
213
|
|
|
$this->assertContains('Job did not finish, requeued after try 1.', $this->QueueShell->_out); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function testTimeNeeded() |
221
|
|
|
{ |
222
|
|
|
$this->QueueShell = $this->getMockBuilder(QueueShell::class) |
|
|
|
|
223
|
|
|
->setMethods([ |
224
|
|
|
'_time' |
225
|
|
|
]) |
226
|
|
|
->getMock(); |
227
|
|
|
|
228
|
|
|
$first = time(); |
229
|
|
|
$second = $first - HOUR + MINUTE; |
230
|
|
|
$this->QueueShell->expects($this->at(0)) |
231
|
|
|
->method('_time') |
232
|
|
|
->will($this->returnValue($first)); |
233
|
|
|
$this->QueueShell->expects($this->at(1)) |
234
|
|
|
->method('_time') |
235
|
|
|
->will($this->returnValue($second)); |
236
|
|
|
$this->QueueShell->expects($this->exactly(2)) |
237
|
|
|
->method('_time') |
238
|
|
|
->withAnyParameters(); |
239
|
|
|
|
240
|
|
|
$result = $this->QueueShell->timeNeeded(); |
|
|
|
|
241
|
|
|
$this->assertSame('3540s', $result); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
public function testMemoryUsage() |
249
|
|
|
{ |
250
|
|
|
$result = $this->QueueShell->memoryUsage(); |
251
|
|
|
$this->assertRegExp('/^\d+MB/', $result, 'Should be e.g. `17MB` or `17MB/1GB` etc.'); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* |
256
|
|
|
* @return void |
257
|
|
|
*/ |
258
|
|
|
public function testStringToArray() |
259
|
|
|
{ |
260
|
|
|
$string = 'Foo,Bar,'; |
261
|
|
|
$result = $this->QueueShell->stringToArray($string); |
262
|
|
|
|
263
|
|
|
$expected = [ |
264
|
|
|
'Foo', |
265
|
|
|
'Bar' |
266
|
|
|
]; |
267
|
|
|
$this->assertSame($expected, $result); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Helper method for skipping tests that need a real connection. |
272
|
|
|
* |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
|
protected function _needsConnection() |
276
|
|
|
{ |
277
|
|
|
$config = ConnectionManager::getConfig('test'); |
278
|
|
|
$this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
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..