1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\ParallelProcess\Test\Unit; |
4
|
|
|
|
5
|
|
|
use Graze\ParallelProcess\Lines; |
6
|
|
|
use Graze\ParallelProcess\Pool; |
7
|
|
|
use Graze\ParallelProcess\Test\BufferDiffOutput; |
8
|
|
|
use Graze\ParallelProcess\Test\TestCase; |
9
|
|
|
use Mockery; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
|
13
|
|
|
class LinesTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var BufferDiffOutput */ |
16
|
|
|
private $bufferOutput; |
17
|
|
|
/** @var mixed */ |
18
|
|
|
private $pool; |
19
|
|
|
/** @var Lines */ |
20
|
|
|
private $lines; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
mb_internal_encoding("UTF-8"); |
25
|
|
|
$this->bufferOutput = new BufferDiffOutput(); |
26
|
|
|
$this->pool = Mockery::mock(Pool::class)->makePartial(); |
27
|
|
|
$this->lines = new Lines($this->bufferOutput, $this->pool); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function testShowDuration() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$output = Mockery::mock(OutputInterface::class); |
33
|
|
|
$lines = new Lines($output); |
34
|
|
|
|
35
|
|
|
$this->assertTrue($lines->isShowDuration()); |
36
|
|
|
|
37
|
|
|
$this->assertSame($lines, $lines->setShowDuration(false)); |
38
|
|
|
|
39
|
|
|
$this->assertFalse($lines->isShowDuration()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testShowType() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$output = Mockery::mock(OutputInterface::class); |
45
|
|
|
$lines = new Lines($output); |
46
|
|
|
|
47
|
|
|
$this->assertTrue($lines->isShowType()); |
48
|
|
|
|
49
|
|
|
$this->assertSame($lines, $lines->setShowType(false)); |
50
|
|
|
|
51
|
|
|
$this->assertFalse($lines->isShowType()); |
52
|
|
|
} |
53
|
|
View Code Duplication |
public function testShowProcessColours() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$output = Mockery::mock(OutputInterface::class); |
56
|
|
|
$lines = new Lines($output); |
57
|
|
|
|
58
|
|
|
$this->assertTrue($lines->isColourProcesses()); |
59
|
|
|
|
60
|
|
|
$this->assertSame($lines, $lines->setColourProcesses(false)); |
61
|
|
|
|
62
|
|
|
$this->assertFalse($lines->isColourProcesses()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testSingleProcessOutput() |
66
|
|
|
{ |
67
|
|
|
$process = Mockery::mock(Process::class); |
68
|
|
|
$process->shouldReceive('stop'); |
69
|
|
|
$process->shouldReceive('start')->with( |
70
|
|
|
Mockery::on( |
71
|
|
View Code Duplication |
function ($closure) { |
|
|
|
|
72
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
73
|
|
|
call_user_func($closure, Process::OUT, 'second line'); |
74
|
|
|
call_user_func($closure, Process::OUT, 'third line'); |
75
|
|
|
call_user_func($closure, Process::ERR, 'error line'); |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
) |
79
|
|
|
)->once(); |
80
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
81
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
82
|
|
|
false, // add |
83
|
|
|
false, // start |
84
|
|
|
true, // check |
85
|
|
|
true, // ... |
86
|
|
|
true, |
87
|
|
|
true, |
88
|
|
|
true, |
89
|
|
|
true, |
90
|
|
|
true, |
91
|
|
|
true, |
92
|
|
|
true, |
93
|
|
|
true, |
94
|
|
|
true, |
95
|
|
|
false // complete |
96
|
|
|
); |
97
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
98
|
|
|
|
99
|
|
|
$this->lines->add($process, ['key' => 'value']); |
100
|
|
|
|
101
|
|
|
$this->lines->run(0); |
102
|
|
|
|
103
|
|
|
$expected = [ |
104
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
105
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
106
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) second line%'], |
107
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) third line%'], |
108
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(err\) error line%'], |
109
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testProcessColoursDisabled() |
116
|
|
|
{ |
117
|
|
|
$process = Mockery::mock(Process::class); |
118
|
|
|
$process->shouldReceive('stop'); |
119
|
|
|
$process->shouldReceive('start')->with( |
120
|
|
|
Mockery::on( |
121
|
|
View Code Duplication |
function ($closure) { |
|
|
|
|
122
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
123
|
|
|
call_user_func($closure, Process::OUT, 'second line'); |
124
|
|
|
call_user_func($closure, Process::OUT, 'third line'); |
125
|
|
|
call_user_func($closure, Process::ERR, 'error line'); |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
) |
129
|
|
|
)->once(); |
130
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
131
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
132
|
|
|
false, // add |
133
|
|
|
false, // start |
134
|
|
|
true, // check |
135
|
|
|
false // complete |
136
|
|
|
); |
137
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
138
|
|
|
|
139
|
|
|
$this->lines->setColourProcesses(false); |
140
|
|
|
$this->lines->add($process, ['key' => 'value']); |
141
|
|
|
|
142
|
|
|
$this->lines->run(0); |
143
|
|
|
|
144
|
|
|
$expected = [ |
145
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
146
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
147
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(out\) second line%'], |
148
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(out\) third line%'], |
149
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) \(err\) error line%'], |
150
|
|
|
['%<info>key</info>: value \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testValueOnlyData() |
157
|
|
|
{ |
158
|
|
|
$process = Mockery::mock(Process::class); |
159
|
|
|
$process->shouldReceive('stop'); |
160
|
|
|
$process->shouldReceive('start')->with( |
161
|
|
|
Mockery::on( |
162
|
|
|
function ($closure) { |
163
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
) |
167
|
|
|
)->once(); |
168
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
169
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
170
|
|
|
false, // add |
171
|
|
|
false, // start |
172
|
|
|
true, // check |
173
|
|
|
true, // ... |
174
|
|
|
true, |
175
|
|
|
true, |
176
|
|
|
false // complete |
177
|
|
|
); |
178
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
179
|
|
|
|
180
|
|
|
$this->lines->add($process, ['value']); |
181
|
|
|
|
182
|
|
|
$this->lines->run(0); |
183
|
|
|
|
184
|
|
|
$expected = [ |
185
|
|
|
['%<options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
186
|
|
|
['%<options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
187
|
|
|
['%<options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
188
|
|
|
]; |
189
|
|
|
|
190
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testFailureReturnsErrors() |
194
|
|
|
{ |
195
|
|
|
$process = Mockery::mock(Process::class); |
196
|
|
|
$process->shouldReceive('stop'); |
197
|
|
|
$process->shouldReceive('start')->with( |
198
|
|
|
Mockery::on( |
199
|
|
|
function ($closure) { |
200
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
201
|
|
|
return true; |
202
|
|
|
} |
203
|
|
|
) |
204
|
|
|
)->once(); |
205
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
206
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
207
|
|
|
false, // add |
208
|
|
|
false, // start |
209
|
|
|
true, // check |
210
|
|
|
true, // ... |
211
|
|
|
true, |
212
|
|
|
true, |
213
|
|
|
false // complete |
214
|
|
|
); |
215
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(false); |
216
|
|
|
$process->shouldReceive('getExitCode')->atLeast()->once()->andReturn(3); |
217
|
|
|
$process->shouldReceive('getExitCodeText')->atLeast()->once()->andReturn('some error'); |
218
|
|
|
$process->shouldReceive('getCommandLine')->andReturn('test'); |
219
|
|
|
$process->shouldReceive('getExitCode')->andReturn(3); |
220
|
|
|
$process->shouldReceive('getExitCodeText')->andReturn('some error'); |
221
|
|
|
$process->shouldReceive('getWorkingDirectory')->andReturn('/tmp'); |
222
|
|
|
$process->shouldReceive('isOutputDisabled')->andReturn(false); |
223
|
|
|
$process->shouldReceive('getErrorOutput')->andReturn('some error text'); |
224
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
225
|
|
|
|
226
|
|
|
$this->lines->add($process, ['key' => 'value']); |
227
|
|
|
|
228
|
|
|
$this->lines->run(0); |
229
|
|
|
|
230
|
|
|
$expected = [ |
231
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
232
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) \(out\) first line%'], |
233
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <error>x Failed</error> \(code: 3\) some error%'], |
234
|
|
|
[<<<TEXT |
235
|
|
|
%The command "test" failed. |
236
|
|
|
|
237
|
|
|
Exit Code: 3\(some error\) |
238
|
|
|
|
239
|
|
|
Working directory: /tmp |
240
|
|
|
|
241
|
|
|
Output: |
242
|
|
|
================ |
243
|
|
|
first line |
244
|
|
|
|
245
|
|
|
Error Output: |
246
|
|
|
================ |
247
|
|
|
some error text% |
248
|
|
|
TEXT |
249
|
|
|
], |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
View Code Duplication |
public function testShowTypeDoesNotOutputTheStdOrErrInformation() |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$process = Mockery::mock(Process::class); |
258
|
|
|
$process->shouldReceive('stop'); |
259
|
|
|
$process->shouldReceive('start')->with( |
260
|
|
|
Mockery::on( |
261
|
|
|
function ($closure) { |
262
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
263
|
|
|
return true; |
264
|
|
|
} |
265
|
|
|
) |
266
|
|
|
)->once(); |
267
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
268
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
269
|
|
|
false, // add |
270
|
|
|
false, // start |
271
|
|
|
true, // check |
272
|
|
|
true, // ... |
273
|
|
|
true, |
274
|
|
|
true, |
275
|
|
|
false // complete |
276
|
|
|
); |
277
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
278
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
279
|
|
|
|
280
|
|
|
$this->lines->setShowType(false); |
281
|
|
|
$this->lines->add($process, ['key' => 'value']); |
282
|
|
|
|
283
|
|
|
$this->lines->run(0); |
284
|
|
|
|
285
|
|
|
$expected = [ |
286
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <fg=blue>→ Started</>%'], |
287
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) first line%'], |
288
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(<comment>[ 0-9\.s]+</comment>\) <info>✓ Succeeded</info>%'], |
289
|
|
|
]; |
290
|
|
|
|
291
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
View Code Duplication |
public function testShowDurationDoesNotShowTheDuration() |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
$process = Mockery::mock(Process::class); |
297
|
|
|
$process->shouldReceive('stop'); |
298
|
|
|
$process->shouldReceive('start')->with( |
299
|
|
|
Mockery::on( |
300
|
|
|
function ($closure) { |
301
|
|
|
call_user_func($closure, Process::OUT, 'first line'); |
302
|
|
|
return true; |
303
|
|
|
} |
304
|
|
|
) |
305
|
|
|
)->once(); |
306
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
307
|
|
|
$process->shouldReceive('isRunning')->andReturn( |
308
|
|
|
false, // add |
309
|
|
|
false, // start |
310
|
|
|
true, // check |
311
|
|
|
true, // ... |
312
|
|
|
true, |
313
|
|
|
true, |
314
|
|
|
false // complete |
315
|
|
|
); |
316
|
|
|
$process->shouldReceive('isSuccessful')->atLeast()->once()->andReturn(true); |
317
|
|
|
$process->shouldReceive('getOutput')->andReturn('first line'); |
318
|
|
|
|
319
|
|
|
$this->lines->setShowDuration(false); |
320
|
|
|
$this->lines->add($process, ['key' => 'value']); |
321
|
|
|
|
322
|
|
|
$this->lines->run(0); |
323
|
|
|
|
324
|
|
|
$expected = [ |
325
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> <fg=blue>→ Started</>%'], |
326
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> \(out\) first line%'], |
327
|
|
|
['%<info>key</info>: <options=bold;fg=\w+>value</> <info>✓ Succeeded</info>%'], |
328
|
|
|
]; |
329
|
|
|
|
330
|
|
|
$this->compareOutputs($expected, $this->bufferOutput->getWritten()); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.