1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/parallel-process. |
4
|
|
|
* |
5
|
|
|
* Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/parallel-process/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/parallel-process |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\ParallelProcess\Test\Unit; |
15
|
|
|
|
16
|
|
|
use Exception; |
17
|
|
|
use Graze\ParallelProcess\Event\RunEvent; |
18
|
|
|
use Graze\ParallelProcess\Lines; |
19
|
|
|
use Graze\ParallelProcess\Pool; |
20
|
|
|
use Graze\ParallelProcess\Run; |
21
|
|
|
use Graze\ParallelProcess\RunInterface; |
22
|
|
|
use Graze\ParallelProcess\Test\BufferDiffOutput; |
23
|
|
|
use Graze\ParallelProcess\Test\TestCase; |
24
|
|
|
use Mockery; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use Symfony\Component\Process\Process; |
27
|
|
|
|
28
|
|
|
class LinesTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** @var BufferDiffOutput */ |
31
|
|
|
private $bufferOutput; |
32
|
|
|
/** @var mixed */ |
33
|
|
|
private $pool; |
34
|
|
|
/** @var Lines */ |
35
|
|
|
private $lines; |
36
|
|
|
|
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
mb_internal_encoding("UTF-8"); |
40
|
|
|
$this->bufferOutput = new BufferDiffOutput(); |
41
|
|
|
$this->pool = Mockery::mock(Pool::class)->makePartial(); |
42
|
|
|
$this->lines = new Lines($this->bufferOutput, $this->pool); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testShowDuration() |
46
|
|
|
{ |
47
|
|
|
$output = Mockery::mock(OutputInterface::class); |
48
|
|
|
$lines = new Lines($output); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$this->assertTrue($lines->isShowDuration()); |
51
|
|
|
|
52
|
|
|
$this->assertSame($lines, $lines->setShowDuration(false)); |
53
|
|
|
|
54
|
|
|
$this->assertFalse($lines->isShowDuration()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testShowType() |
58
|
|
|
{ |
59
|
|
|
$output = Mockery::mock(OutputInterface::class); |
60
|
|
|
$lines = new Lines($output); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->assertTrue($lines->isShowType()); |
63
|
|
|
|
64
|
|
|
$this->assertSame($lines, $lines->setShowType(false)); |
65
|
|
|
|
66
|
|
|
$this->assertFalse($lines->isShowType()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testShowProcessColours() |
70
|
|
|
{ |
71
|
|
|
$output = Mockery::mock(OutputInterface::class); |
72
|
|
|
$lines = new Lines($output); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->assertTrue($lines->isColourProcesses()); |
75
|
|
|
|
76
|
|
|
$this->assertSame($lines, $lines->setColourProcesses(false)); |
77
|
|
|
|
78
|
|
|
$this->assertFalse($lines->isColourProcesses()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testSingleProcessOutput() |
82
|
|
|
{ |
83
|
|
|
$process = Mockery::mock(Process::class); |
84
|
|
|
$process->shouldReceive('stop'); |
85
|
|
|
$process->shouldReceive('start')->with( |
86
|
|
|
Mockery::on( |
87
|
|
|
function ($closure) { |
88
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
89
|
|
|
call_user_func($closure, Process::OUT, 'second line'); |
90
|
|
|
call_user_func($closure, Process::OUT, 'third line'); |
91
|
|
|
call_user_func($closure, Process::ERR, 'error line'); |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
) |
95
|
|
|
)->once(); |
96
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
97
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
98
|
|
|
false, // add |
99
|
|
|
false, // start |
100
|
|
|
true, // check |
101
|
|
|
true, // ... |
102
|
|
|
true, |
103
|
|
|
true, |
104
|
|
|
true, |
105
|
|
|
true, |
106
|
|
|
true, |
107
|
|
|
true, |
108
|
|
|
true, |
109
|
|
|
true, |
110
|
|
|
true, |
111
|
|
|
false // complete |
112
|
|
|
); |
113
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
114
|
|
|
|
115
|
|
|
$this->pool->add($process, ['key' => 'value']); |
116
|
|
|
|
117
|
|
|
$this->lines->run(0); |
118
|
|
|
|
119
|
|
|
$expected = [ |
120
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
121
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
122
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) second line%'], |
123
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) third line%'], |
124
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(err\) error line%'], |
125
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testProcessColoursDisabled() |
132
|
|
|
{ |
133
|
|
|
$process = Mockery::mock(Process::class); |
134
|
|
|
$process->shouldReceive('stop'); |
135
|
|
|
$process->shouldReceive('start')->with( |
136
|
|
|
Mockery::on( |
137
|
|
|
function ($closure) { |
138
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
139
|
|
|
call_user_func($closure, Process::OUT, 'second line'); |
140
|
|
|
call_user_func($closure, Process::OUT, 'third line'); |
141
|
|
|
call_user_func($closure, Process::ERR, 'error line'); |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
) |
145
|
|
|
)->once(); |
146
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
147
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
148
|
|
|
false, // add |
149
|
|
|
false, // start |
150
|
|
|
true, // check |
151
|
|
|
false // complete |
152
|
|
|
); |
153
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
154
|
|
|
|
155
|
|
|
$this->lines->setColourProcesses(false); |
156
|
|
|
$this->pool->add($process, ['key' => 'value']); |
157
|
|
|
|
158
|
|
|
$this->lines->run(0); |
159
|
|
|
|
160
|
|
|
$expected = [ |
161
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
162
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
163
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(out\) second line%'], |
164
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(out\) third line%'], |
165
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(err\) error line%'], |
166
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testValueOnlyData() |
173
|
|
|
{ |
174
|
|
|
$process = Mockery::mock(Process::class); |
175
|
|
|
$process->shouldReceive('stop'); |
176
|
|
|
$process->shouldReceive('start')->with( |
177
|
|
|
Mockery::on( |
178
|
|
|
function ($closure) { |
179
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
180
|
|
|
return true; |
181
|
|
|
} |
182
|
|
|
) |
183
|
|
|
)->once(); |
184
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
185
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
186
|
|
|
false, // add |
187
|
|
|
false, // start |
188
|
|
|
true, // check |
189
|
|
|
true, // ... |
190
|
|
|
true, |
191
|
|
|
true, |
192
|
|
|
false // complete |
193
|
|
|
); |
194
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
195
|
|
|
|
196
|
|
|
$this->pool->add($process, ['value']); |
197
|
|
|
|
198
|
|
|
$this->lines->run(0); |
199
|
|
|
|
200
|
|
|
$expected = [ |
201
|
|
|
['%<options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
202
|
|
|
['%<options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
203
|
|
|
['%<options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testFailureReturnsErrors() |
210
|
|
|
{ |
211
|
|
|
$process = Mockery::mock(Process::class); |
212
|
|
|
$process->shouldReceive('stop'); |
213
|
|
|
$process->shouldReceive('start')->with( |
214
|
|
|
Mockery::on( |
215
|
|
|
function ($closure) { |
216
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
217
|
|
|
return true; |
218
|
|
|
} |
219
|
|
|
) |
220
|
|
|
)->once(); |
221
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
222
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
223
|
|
|
false, // add |
224
|
|
|
false, // start |
225
|
|
|
true, // check |
226
|
|
|
true, // ... |
227
|
|
|
true, |
228
|
|
|
true, |
229
|
|
|
false // complete |
230
|
|
|
); |
231
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(false); |
232
|
|
|
$process->shouldReceive('getExitCode')->atLeast()->once()->andReturn(3); |
233
|
|
|
$process->shouldReceive('getExitCodeText')->atLeast()->once()->andReturn('some error'); |
234
|
|
|
$process->shouldReceive('getCommandLine')->andReturn('test'); |
235
|
|
|
$process->shouldReceive('getExitCode')->andReturn(3); |
236
|
|
|
$process->shouldReceive('getExitCodeText')->andReturn('some error'); |
237
|
|
|
$process->shouldReceive('getWorkingDirectory')->andReturn('/tmp'); |
238
|
|
|
$process->shouldReceive('isOutputDisabled')->andReturn(false); |
239
|
|
|
$process->shouldReceive('getErrorOutput')->andReturn('some error text'); |
240
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
241
|
|
|
|
242
|
|
|
$this->pool->add($process, ['key' => 'value']); |
243
|
|
|
|
244
|
|
|
$this->lines->run(0); |
245
|
|
|
|
246
|
|
|
$expected = [ |
247
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
248
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
249
|
|
|
[ |
250
|
|
|
<<<TEXT |
251
|
|
|
%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <error>x Failed</error> \(0\) The command "test" failed. |
252
|
|
|
|
253
|
|
|
Exit Code: 3\(some error\) |
254
|
|
|
|
255
|
|
|
Working directory: /tmp |
256
|
|
|
|
257
|
|
|
Output: |
258
|
|
|
================ |
259
|
|
|
first line |
260
|
|
|
|
261
|
|
|
Error Output: |
262
|
|
|
================ |
263
|
|
|
some error text% |
264
|
|
|
TEXT |
265
|
|
|
, |
266
|
|
|
], |
267
|
|
|
[ |
268
|
|
|
<<<TEXT |
269
|
|
|
%The command "test" failed. |
270
|
|
|
|
271
|
|
|
Exit Code: 3\(some error\) |
272
|
|
|
|
273
|
|
|
Working directory: /tmp |
274
|
|
|
|
275
|
|
|
Output: |
276
|
|
|
================ |
277
|
|
|
first line |
278
|
|
|
|
279
|
|
|
Error Output: |
280
|
|
|
================ |
281
|
|
|
some error text% |
282
|
|
|
TEXT |
283
|
|
|
, |
284
|
|
|
], |
285
|
|
|
]; |
286
|
|
|
|
287
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function testShowTypeDoesNotOutputTheStdOrErrInformation() |
291
|
|
|
{ |
292
|
|
|
$process = Mockery::mock(Process::class); |
293
|
|
|
$process->shouldReceive('stop'); |
294
|
|
|
$process->shouldReceive('start')->with( |
295
|
|
|
Mockery::on( |
296
|
|
|
function ($closure) { |
297
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
298
|
|
|
return true; |
299
|
|
|
} |
300
|
|
|
) |
301
|
|
|
)->once(); |
302
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
303
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
304
|
|
|
false, // add |
305
|
|
|
false, // start |
306
|
|
|
true, // check |
307
|
|
|
true, // ... |
308
|
|
|
true, |
309
|
|
|
true, |
310
|
|
|
false // complete |
311
|
|
|
); |
312
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
313
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
314
|
|
|
|
315
|
|
|
$this->lines->setShowType(false); |
316
|
|
|
$this->pool->add($process, ['key' => 'value']); |
317
|
|
|
|
318
|
|
|
$this->lines->run(0); |
319
|
|
|
|
320
|
|
|
$expected = [ |
321
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
322
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) first line%'], |
323
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
324
|
|
|
]; |
325
|
|
|
|
326
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function testShowDurationToFalseDoesNotShowTheDuration() |
330
|
|
|
{ |
331
|
|
|
$process = Mockery::mock(Process::class); |
332
|
|
|
$process->shouldReceive('stop'); |
333
|
|
|
$process->shouldReceive('start')->with( |
334
|
|
|
Mockery::on( |
335
|
|
|
function ($closure) { |
336
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
337
|
|
|
return true; |
338
|
|
|
} |
339
|
|
|
) |
340
|
|
|
)->once(); |
341
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
342
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
343
|
|
|
false, // add |
344
|
|
|
false, // start |
345
|
|
|
true, // check |
346
|
|
|
true, // ... |
347
|
|
|
true, |
348
|
|
|
true, |
349
|
|
|
false // complete |
350
|
|
|
); |
351
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
352
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
353
|
|
|
|
354
|
|
|
$this->lines->setShowDuration(false); |
355
|
|
|
$this->pool->add($process, ['key' => 'value']); |
356
|
|
|
|
357
|
|
|
$this->lines->run(0); |
358
|
|
|
|
359
|
|
|
$expected = [ |
360
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> <fg=blue>→ Started</>%'], |
361
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(out\) first line%'], |
362
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> <info>✓ Succeeded</info>%'], |
363
|
|
|
]; |
364
|
|
|
|
365
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function testShowProgressShowsTheProgress() |
369
|
|
|
{ |
370
|
|
|
$process = Mockery::mock(Process::class); |
371
|
|
|
$process->shouldReceive('stop'); |
372
|
|
|
$process->shouldReceive('start')->with( |
373
|
|
|
Mockery::on( |
374
|
|
|
function ($closure) { |
375
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
376
|
|
|
return true; |
377
|
|
|
} |
378
|
|
|
) |
379
|
|
|
)->once(); |
380
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
381
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
382
|
|
|
false, // add |
383
|
|
|
false, // start |
384
|
|
|
true, // check |
385
|
|
|
true, // ... |
386
|
|
|
true, |
387
|
|
|
true, |
388
|
|
|
false // complete |
389
|
|
|
); |
390
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
391
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
392
|
|
|
|
393
|
|
|
$run = Mockery::mock(Run::class, [$process, ['key' => 'value']])->makePartial(); |
394
|
|
|
$run->allows() |
395
|
|
|
->getProgress() |
396
|
|
|
->andReturns([0, 100, 0], [50, 100, 0.5], [100, 100, 1]); |
397
|
|
|
|
398
|
|
|
$this->lines->setShowProgress(true); |
399
|
|
|
$this->pool->add($run); |
400
|
|
|
|
401
|
|
|
$this->lines->run(0); |
402
|
|
|
|
403
|
|
|
$expected = [ |
404
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) |<comment> </comment>| <fg=black;bg=cyan> 0\%</> <fg=blue>→ Started</>%'], |
405
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) |<comment>█ </comment>| <fg=black;bg=cyan> 50\%</> \(out\) first line%'], |
406
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) |<comment>██</comment>| <fg=black;bg=cyan>100\%</> <info>✓ Succeeded</info>%'], |
407
|
|
|
]; |
408
|
|
|
|
409
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
public function testNonShowProgressShowsTheProgress() |
413
|
|
|
{ |
414
|
|
|
$process = Mockery::mock(Process::class); |
415
|
|
|
$process->shouldReceive('stop'); |
416
|
|
|
$process->shouldReceive('start')->with( |
417
|
|
|
Mockery::on( |
418
|
|
|
function ($closure) { |
419
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
420
|
|
|
return true; |
421
|
|
|
} |
422
|
|
|
) |
423
|
|
|
)->once(); |
424
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
425
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
426
|
|
|
false, // add |
427
|
|
|
false, // start |
428
|
|
|
true, // check |
429
|
|
|
true, // ... |
430
|
|
|
true, |
431
|
|
|
true, |
432
|
|
|
false // complete |
433
|
|
|
); |
434
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
435
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
436
|
|
|
|
437
|
|
|
$run = Mockery::mock(Run::class, [$process, ['key' => 'value']])->makePartial(); |
438
|
|
|
$run->allows() |
439
|
|
|
->getProgress() |
440
|
|
|
->andReturns([0, 100, 0], [50, 100, 0.5], [100, 100, 1]); |
441
|
|
|
|
442
|
|
|
$this->lines->setShowProgress(false); |
443
|
|
|
$this->assertFalse($this->lines->isShowProgress()); |
444
|
|
|
$this->pool->add($run); |
445
|
|
|
|
446
|
|
|
$this->lines->run(0); |
447
|
|
|
|
448
|
|
|
$expected = [ |
449
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
450
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
451
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
452
|
|
|
]; |
453
|
|
|
|
454
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
public function testNonProcessRunFailure() |
458
|
|
|
{ |
459
|
|
|
$run = Mockery::mock(RunInterface::class); |
460
|
|
|
$run->shouldReceive('stop'); |
461
|
|
|
$run->shouldReceive('start')->once(); |
462
|
|
|
$run->shouldReceive('hasStarted')->andReturn(true); |
463
|
|
|
$run->shouldReceive('isRunning')->andReturn( |
464
|
|
|
false, // add |
465
|
|
|
false, // start |
466
|
|
|
true, // check |
467
|
|
|
true, // ... |
468
|
|
|
true, |
469
|
|
|
true, |
470
|
|
|
false // complete |
471
|
|
|
); |
472
|
|
|
$run->shouldReceive('poll')->andReturn( |
473
|
|
|
true, // check |
474
|
|
|
true, // ... |
475
|
|
|
true, |
476
|
|
|
true, |
477
|
|
|
false // complete |
478
|
|
|
); |
479
|
|
|
$run->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(false); |
480
|
|
|
|
481
|
|
|
$startedEvent = $completedEvent = $updatedEvent = null; |
482
|
|
|
$failedEvents = []; |
483
|
|
|
|
484
|
|
|
$run->allows()->addListener( |
|
|
|
|
485
|
|
|
RunEvent::STARTED, |
486
|
|
|
Mockery::on(function (callable $callback) use (&$startedEvent) { |
487
|
|
|
$startedEvent = $callback; |
488
|
|
|
return true; |
489
|
|
|
}) |
490
|
|
|
); |
491
|
|
|
$run->allows()->addListener( |
492
|
|
|
RunEvent::COMPLETED, |
493
|
|
|
Mockery::on(function (callable $callback) use (&$completedEvent) { |
494
|
|
|
$completedEvent = $callback; |
495
|
|
|
return true; |
496
|
|
|
}) |
497
|
|
|
); |
498
|
|
|
$run->allows()->addListener( |
499
|
|
|
RunEvent::FAILED, |
500
|
|
|
Mockery::on(function (callable $callback) use (&$failedEvents) { |
501
|
|
|
$failedEvents[] = $callback; |
502
|
|
|
return true; |
503
|
|
|
}) |
504
|
|
|
); |
505
|
|
|
$run->allows()->addListener( |
506
|
|
|
RunEvent::UPDATED, |
507
|
|
|
Mockery::on(function (callable $callback) use (&$updatedEvent) { |
508
|
|
|
$updatedEvent = $callback; |
509
|
|
|
return true; |
510
|
|
|
}) |
511
|
|
|
); |
512
|
|
|
$run->allows(['getTags' => ['key' => 'value'], 'getProgress' => null]); |
513
|
|
|
|
514
|
|
|
$this->pool->add($run); |
515
|
|
|
|
516
|
|
|
$this->lines->run(0); |
517
|
|
|
|
518
|
|
|
$run->allows()->getDuration()->andReturns(0, 0.1); |
|
|
|
|
519
|
|
|
|
520
|
|
|
$run->allows()->getExceptions()->andReturns([new Exception('some error', 5)]); |
|
|
|
|
521
|
|
|
|
522
|
|
|
call_user_func($startedEvent, new RunEvent($run)); |
|
|
|
|
523
|
|
|
foreach ($failedEvents as $failedEvent) { |
524
|
|
|
call_user_func($failedEvent, new RunEvent($run)); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
$expected = [ |
528
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
529
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <error>x Failed</error> \(5\) some error%'], |
530
|
|
|
['%some error%'], |
531
|
|
|
]; |
532
|
|
|
|
533
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
public function testRunFailureWithNoException() |
537
|
|
|
{ |
538
|
|
|
$run = Mockery::mock(RunInterface::class); |
539
|
|
|
$run->shouldReceive('stop'); |
540
|
|
|
$run->shouldReceive('start')->once(); |
541
|
|
|
$run->shouldReceive('hasStarted')->andReturn(true); |
542
|
|
|
$run->shouldReceive('isRunning')->andReturn( |
543
|
|
|
false, // add |
544
|
|
|
false, // start |
545
|
|
|
true, // check |
546
|
|
|
true, // ... |
547
|
|
|
true, |
548
|
|
|
true, |
549
|
|
|
false // complete |
550
|
|
|
); |
551
|
|
|
$run->shouldReceive('poll')->andReturn( |
552
|
|
|
true, // check |
553
|
|
|
true, // ... |
554
|
|
|
true, |
555
|
|
|
true, |
556
|
|
|
false // complete |
557
|
|
|
); |
558
|
|
|
$run->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(false); |
559
|
|
|
|
560
|
|
|
$startedEvent = $completedEvent = $updatedEvent = null; |
561
|
|
|
$failedEvents = []; |
562
|
|
|
|
563
|
|
|
$run->allows()->addListener( |
564
|
|
|
RunEvent::STARTED, |
565
|
|
|
Mockery::on(function (callable $callback) use (&$startedEvent) { |
566
|
|
|
$startedEvent = $callback; |
567
|
|
|
return true; |
568
|
|
|
}) |
569
|
|
|
); |
570
|
|
|
$run->allows()->addListener( |
571
|
|
|
RunEvent::COMPLETED, |
572
|
|
|
Mockery::on(function (callable $callback) use (&$completedEvent) { |
573
|
|
|
$completedEvent = $callback; |
574
|
|
|
return true; |
575
|
|
|
}) |
576
|
|
|
); |
577
|
|
|
$run->allows()->addListener( |
578
|
|
|
RunEvent::FAILED, |
579
|
|
|
Mockery::on(function (callable $callback) use (&$failedEvents) { |
580
|
|
|
$failedEvents[] = $callback; |
581
|
|
|
return true; |
582
|
|
|
}) |
583
|
|
|
); |
584
|
|
|
$run->allows()->addListener( |
585
|
|
|
RunEvent::UPDATED, |
586
|
|
|
Mockery::on(function (callable $callback) use (&$updatedEvent) { |
587
|
|
|
$updatedEvent = $callback; |
588
|
|
|
return true; |
589
|
|
|
}) |
590
|
|
|
); |
591
|
|
|
$run->allows(['getTags' => ['key' => 'value'], 'getProgress' => null]); |
592
|
|
|
|
593
|
|
|
$this->pool->add($run); |
594
|
|
|
|
595
|
|
|
$this->lines->run(0); |
596
|
|
|
|
597
|
|
|
$run->allows()->getDuration()->andReturns(0, 0.1); |
598
|
|
|
|
599
|
|
|
$run->allows()->getExceptions()->andReturns([]); |
600
|
|
|
|
601
|
|
|
call_user_func($startedEvent, new RunEvent($run)); |
|
|
|
|
602
|
|
|
foreach ($failedEvents as $failedEvent) { |
603
|
|
|
call_user_func($failedEvent, new RunEvent($run)); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
$expected = [ |
607
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
608
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <error>x Failed</error>%'], |
609
|
|
|
]; |
610
|
|
|
|
611
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
612
|
|
|
} |
613
|
|
|
} |
614
|
|
|
|