1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\ParallelProcess\Test\Unit; |
4
|
|
|
|
5
|
|
|
use Graze\ParallelProcess\Run; |
6
|
|
|
use Graze\ParallelProcess\RunInterface; |
7
|
|
|
use Graze\ParallelProcess\Test\TestCase; |
8
|
|
|
use Mockery; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
class RunTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testRunImplementsRunInterface() |
14
|
|
|
{ |
15
|
|
|
$process = Mockery::mock(Process::class); |
16
|
|
|
$process->shouldReceive('stop'); |
17
|
|
|
$run = new Run($process); |
18
|
|
|
$this->assertInstanceOf(RunInterface::class, $run); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testInitialState() |
22
|
|
|
{ |
23
|
|
|
$process = Mockery::mock(Process::class); |
24
|
|
|
$process->shouldReceive('stop'); |
25
|
|
|
|
26
|
|
|
$run = new Run($process); |
27
|
|
|
|
28
|
|
|
$this->assertSame($process, $run->getProcess()); |
29
|
|
|
|
30
|
|
|
$process->shouldReceive('isRunning') |
31
|
|
|
->andReturn(false); |
32
|
|
|
$process->shouldReceive('isStarted') |
33
|
|
|
->andReturn(false); |
34
|
|
|
$process->shouldReceive('isSuccessful') |
35
|
|
|
->andReturn(false); |
36
|
|
|
|
37
|
|
|
$this->assertFalse($run->isRunning(), 'should not be running'); |
38
|
|
|
$this->assertFalse($run->hasStarted(), 'should not have started'); |
39
|
|
|
$this->assertFalse($run->isSuccessful(), 'should not be successful'); |
40
|
|
|
$this->assertTrue($run->isUpdateOnPoll(), 'update on poll should be on by deafult'); |
41
|
|
|
$this->assertTrue($run->isUpdateOnProcessOutput(), 'update on poll should be on by deafult'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testUpdateOnPoll() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$process = Mockery::mock(Process::class); |
47
|
|
|
$process->shouldReceive('stop'); |
48
|
|
|
|
49
|
|
|
$run = new Run($process); |
50
|
|
|
|
51
|
|
|
$this->assertTrue($run->isUpdateOnPoll()); |
52
|
|
|
$this->assertSame($run, $run->setUpdateOnPoll(false)); |
53
|
|
|
$this->assertFalse($run->isUpdateOnPoll()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testUpdateOnProcessOutput() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$process = Mockery::mock(Process::class); |
59
|
|
|
$process->shouldReceive('stop'); |
60
|
|
|
|
61
|
|
|
$run = new Run($process); |
62
|
|
|
|
63
|
|
|
$this->assertTrue($run->isUpdateOnProcessOutput()); |
64
|
|
|
$this->assertSame($run, $run->setUpdateOnProcessOutput(false)); |
65
|
|
|
$this->assertFalse($run->isUpdateOnProcessOutput()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testProcessRunning() |
69
|
|
|
{ |
70
|
|
|
$process = Mockery::mock(Process::class); |
71
|
|
|
$process->shouldReceive('stop'); |
72
|
|
|
|
73
|
|
|
$run = new Run($process); |
74
|
|
|
|
75
|
|
|
$process->shouldReceive('isRunning') |
76
|
|
|
->andReturn(false, true); |
77
|
|
|
$process->shouldReceive('start'); |
78
|
|
|
|
79
|
|
|
$run->start(); |
80
|
|
|
|
81
|
|
|
$process->shouldReceive('isStarted') |
82
|
|
|
->andReturn(true); |
83
|
|
|
|
84
|
|
|
$this->assertTrue($run->isRunning()); |
85
|
|
|
$this->assertTrue($run->hasStarted()); |
86
|
|
|
$this->assertFalse($run->isSuccessful()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testRun() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$process = Mockery::mock(Process::class); |
92
|
|
|
$process->shouldReceive('stop'); |
93
|
|
|
|
94
|
|
|
$run = new Run($process); |
95
|
|
|
|
96
|
|
|
$process->shouldReceive('isRunning') |
97
|
|
|
->andReturn(false, true, false); |
98
|
|
|
|
99
|
|
|
$process->shouldReceive('start'); |
100
|
|
|
$run->start(); |
101
|
|
|
|
102
|
|
|
$process->shouldReceive('isStarted') |
103
|
|
|
->andReturn(true); |
104
|
|
|
$process->shouldReceive('isSuccessful') |
105
|
|
|
->andReturn(true); |
106
|
|
|
|
107
|
|
|
$this->assertTrue($run->poll()); |
108
|
|
|
$this->assertFalse($run->poll()); |
109
|
|
|
$this->assertTrue($run->isSuccessful()); |
110
|
|
|
$this->assertTrue($run->hasStarted()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function testOnSuccess() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$process = Mockery::mock(Process::class); |
116
|
|
|
$process->shouldReceive('stop'); |
117
|
|
|
|
118
|
|
|
$hit = false; |
119
|
|
|
|
120
|
|
|
$run = new Run( |
121
|
|
|
$process, |
122
|
|
|
function ($proc) use ($process, &$hit) { |
123
|
|
|
$this->assertSame($proc, $process); |
124
|
|
|
$hit = true; |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$process->shouldReceive('start')->once(); |
129
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
130
|
|
|
$process->shouldReceive('isRunning')->andReturn(false); |
131
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(true); |
132
|
|
|
|
133
|
|
|
$run->start(); |
134
|
|
|
$this->assertFalse($run->poll()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function testOnFailure() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$process = Mockery::mock(Process::class); |
140
|
|
|
$process->shouldReceive('stop'); |
141
|
|
|
|
142
|
|
|
$hit = false; |
143
|
|
|
|
144
|
|
|
$run = new Run( |
145
|
|
|
$process, |
146
|
|
|
null, |
147
|
|
|
function ($proc) use ($process, &$hit) { |
148
|
|
|
$this->assertSame($proc, $process); |
149
|
|
|
$hit = true; |
150
|
|
|
} |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$process->shouldReceive('start')->once(); |
154
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
155
|
|
|
$process->shouldReceive('isRunning')->andReturn(false); |
156
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(false); |
157
|
|
|
|
158
|
|
|
$run->start(); |
159
|
|
|
$this->assertFalse($run->poll()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
View Code Duplication |
public function testOnProgress() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$process = Mockery::mock(Process::class); |
165
|
|
|
$process->shouldReceive('stop'); |
166
|
|
|
|
167
|
|
|
$hit = false; |
168
|
|
|
|
169
|
|
|
$run = new Run( |
170
|
|
|
$process, |
171
|
|
|
null, |
172
|
|
|
null, |
173
|
|
|
function ($proc) use ($process, &$hit) { |
174
|
|
|
$this->assertSame($proc, $process); |
175
|
|
|
$hit = true; |
176
|
|
|
} |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$process->shouldReceive('start')->once(); |
180
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
181
|
|
|
$process->shouldReceive('isRunning')->andReturn(false, true, false); |
182
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(true); |
183
|
|
|
|
184
|
|
|
$run->start(); |
185
|
|
|
$this->assertTrue($run->poll()); |
186
|
|
|
$this->assertFalse($run->poll()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testStartingAfterStartedWillDoNothing() |
190
|
|
|
{ |
191
|
|
|
$process = Mockery::mock(Process::class); |
192
|
|
|
$process->shouldReceive('stop'); |
193
|
|
|
|
194
|
|
|
$run = new Run($process); |
195
|
|
|
|
196
|
|
|
$process->shouldReceive('isRunning') |
197
|
|
|
->andReturn(true); |
198
|
|
|
|
199
|
|
|
$this->assertSame($run, $run->start()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testEventsProvideDurationAndLastMessage() |
203
|
|
|
{ |
204
|
|
|
$process = Mockery::mock(Process::class); |
205
|
|
|
$process->shouldReceive('stop'); |
206
|
|
|
|
207
|
|
|
$process->shouldReceive('start') |
208
|
|
|
->with( |
209
|
|
|
Mockery::on( |
210
|
|
|
function (callable $fn) { |
211
|
|
|
$this->assertNotNull($fn); |
212
|
|
|
$fn(Process::OUT, 'some text'); |
213
|
|
|
return true; |
214
|
|
|
} |
215
|
|
|
) |
216
|
|
|
) |
217
|
|
|
->once(); |
218
|
|
|
|
219
|
|
|
$run = new Run( |
220
|
|
|
$process, |
221
|
|
|
function ($proc, $dur, $last, $lastType) use ($process) { |
222
|
|
|
$this->assertSame($process, $proc); |
223
|
|
|
$this->assertInternalType('float', $dur); |
224
|
|
|
$this->assertEquals('some text', $last); |
225
|
|
|
$this->assertEquals(Process::OUT, $lastType); |
226
|
|
|
} |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
230
|
|
|
$process->shouldReceive('isRunning')->andReturn(false); |
231
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(true); |
232
|
|
|
|
233
|
|
|
$run->start(); |
234
|
|
|
$this->assertFalse($run->poll()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
View Code Duplication |
public function testUpdateOnPollOffDoesNotUpdateOnPoll() |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$process = Mockery::mock(Process::class); |
240
|
|
|
$process->shouldReceive('stop'); |
241
|
|
|
|
242
|
|
|
$process->shouldReceive('start')->once(); |
243
|
|
|
|
244
|
|
|
$run = new Run( |
245
|
|
|
$process, |
246
|
|
|
null, |
247
|
|
|
null, |
248
|
|
|
function () { |
249
|
|
|
$this->fail('onProgress should not be called'); |
250
|
|
|
} |
251
|
|
|
); |
252
|
|
|
$run->setUpdateOnPoll(false); |
253
|
|
|
|
254
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
255
|
|
|
$process->shouldReceive('isRunning')->andReturn(false, true, false); |
256
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(true); |
257
|
|
|
|
258
|
|
|
$run->start(); |
259
|
|
|
$this->assertTrue($run->poll()); |
260
|
|
|
$this->assertFalse($run->poll()); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
View Code Duplication |
public function testUpdateOnOutputOffDoesNotCallUpdateOnOutput() |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
$process = Mockery::mock(Process::class); |
266
|
|
|
$process->shouldReceive('stop'); |
267
|
|
|
|
268
|
|
|
$process->shouldReceive('start') |
269
|
|
|
->with( |
270
|
|
|
Mockery::on( |
271
|
|
|
function (callable $fn) { |
272
|
|
|
$this->assertNotNull($fn); |
273
|
|
|
$fn(Process::STDOUT, 'some text'); |
274
|
|
|
return true; |
275
|
|
|
} |
276
|
|
|
) |
277
|
|
|
) |
278
|
|
|
->once(); |
279
|
|
|
|
280
|
|
|
$run = new Run( |
281
|
|
|
$process, |
282
|
|
|
null, |
283
|
|
|
null, |
284
|
|
|
function () { |
285
|
|
|
$this->fail('onProgress should not be called'); |
286
|
|
|
} |
287
|
|
|
); |
288
|
|
|
$run->setUpdateOnProcessOutput(false); |
289
|
|
|
|
290
|
|
|
$process->shouldReceive('isStarted')->andReturn(true); |
291
|
|
|
$process->shouldReceive('isRunning')->andReturn(false); |
292
|
|
|
$process->shouldReceive('isSuccessful')->once()->andReturn(true); |
293
|
|
|
|
294
|
|
|
$run->start(); |
295
|
|
|
$this->assertFalse($run->poll()); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
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.