-
Notifications
You must be signed in to change notification settings - Fork 40
/
ProcessSetTest.php
384 lines (315 loc) · 15.1 KB
/
ProcessSetTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php declare(strict_types=1);
namespace Lmc\Steward\Process;
use Assert\InvalidArgumentException;
use Graphp\Algorithms\Tree\OutTree;
use Lmc\Steward\Process\Fixtures\MockOrderStrategy;
use Lmc\Steward\Publisher\XmlPublisher;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
class ProcessSetTest extends TestCase
{
/** @var ProcessSet */
protected $set;
protected function setUp(): void
{
$this->set = new ProcessSet();
}
public function testShouldBeCountable(): void
{
$this->assertCount(0, $this->set);
$this->set->add(new ProcessWrapper(new Process(''), 'Foo'));
$this->assertCount(1, $this->set);
$this->set->add(new ProcessWrapper(new Process(''), 'Bar'));
$this->set->add(new ProcessWrapper(new Process(''), 'Baz'));
$this->assertCount(3, $this->set);
}
public function testShouldFailWhenAddingTestWithNonUniqueName(): void
{
$this->set->add(new ProcessWrapper(new Process(''), 'Foo\Bar'));
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Testcase with name "Foo\Bar" was already added');
$this->set->add(new ProcessWrapper(new Process(''), 'Foo\Bar'));
}
public function testShouldHasNewlyAddedProcessInQueuedState(): void
{
$this->set->add(new ProcessWrapper(new Process(''), 'Foo'));
$this->set->add(new ProcessWrapper(new Process(''), 'Bar'));
$this->assertCount(2, $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED));
$this->assertCount(0, $this->set->get(ProcessWrapper::PROCESS_STATUS_DONE));
$this->assertCount(0, $this->set->get(ProcessWrapper::PROCESS_STATUS_PREPARED));
}
public function testShouldAddAndGetWrappedProcesses(): void
{
$processFoo = new ProcessWrapper(new Process(''), 'Foo');
$processBaz = new ProcessWrapper(new Process(''), 'Baz');
$this->set->add($processFoo);
$this->set->add($processBaz);
$processes = $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED);
$this->assertSame($processBaz, $processes['Baz']);
$this->assertSame($processFoo, $processes['Foo']);
}
public function testShouldRetrieveProcessesByStatus(): void
{
$doneTest1 = new ProcessWrapper(new Process(''), 'DoneTest1');
$doneTest2 = new ProcessWrapper(new Process(''), 'DoneTest2');
$preparedTest = new ProcessWrapper(new Process(''), 'PreparedTest');
$this->set->add($doneTest1);
$this->set->add($doneTest2);
$this->set->add($preparedTest);
// all processes are queued by default
$this->assertCount(3, $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED));
// set statuses
$doneTest1->setStatus(ProcessWrapper::PROCESS_STATUS_DONE);
$doneTest2->setStatus(ProcessWrapper::PROCESS_STATUS_DONE);
$preparedTest->setStatus(ProcessWrapper::PROCESS_STATUS_PREPARED);
// retrieve processes by status
$doneProcesses = $this->set->get(ProcessWrapper::PROCESS_STATUS_DONE);
$preparedProcesses = $this->set->get(ProcessWrapper::PROCESS_STATUS_PREPARED);
// check both done processes
$this->assertCount(2, $doneProcesses);
$this->assertSame($doneTest1, $doneProcesses['DoneTest1']);
$this->assertSame($doneTest2, $doneProcesses['DoneTest2']);
// check one prepared process
$this->assertCount(1, $preparedProcesses);
$this->assertSame($preparedTest, $preparedProcesses['PreparedTest']);
// no queued process left
$this->assertCount(0, $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED));
}
public function testShouldPublishProcessWhenAdded(): void
{
$publisherMock = $this->createMock(XmlPublisher::class);
$publisherMock->expects($this->once())
->method('publishResults')
->with(
'FooClassName',
ProcessWrapper::PROCESS_STATUS_QUEUED,
$this->identicalTo(null),
$this->identicalTo(null),
$this->identicalTo(null)
);
$set = new ProcessSet($publisherMock);
$set->add(new ProcessWrapper(new Process(''), 'FooClassName'));
}
public function testShouldAllowToDefinePublisherUsingSetter(): void
{
$set = new ProcessSet();
$publisherMock = $this->createMock(XmlPublisher::class);
$publisherMock->expects($this->once())
->method('publishResults')
->with(
'FooClassName',
ProcessWrapper::PROCESS_STATUS_QUEUED,
$this->identicalTo(null),
$this->identicalTo(null),
$this->identicalTo(null)
);
$set->setPublisher($publisherMock);
$set->add(new ProcessWrapper(new Process(''), 'FooClassName'));
}
public function testShouldCountStatusesOfWrappedProcesses(): void
{
$doneTest1 = new ProcessWrapper(new Process(''), 'DoneTest1');
$doneTest1->setStatus(ProcessWrapper::PROCESS_STATUS_DONE);
$doneTest2 = new ProcessWrapper(new Process(''), 'DoneTest2');
$doneTest2->setStatus(ProcessWrapper::PROCESS_STATUS_DONE);
$queuedTest = new ProcessWrapper(new Process(''), 'QueuedTest');
$queuedTest->setStatus(ProcessWrapper::PROCESS_STATUS_QUEUED);
$preparedTest = new ProcessWrapper(new Process(''), 'PreparedTest');
$preparedTest->setStatus(ProcessWrapper::PROCESS_STATUS_PREPARED);
$this->set->add($doneTest1);
$this->set->add($queuedTest);
$this->set->add($preparedTest);
$this->set->add($doneTest2);
$this->assertEquals(
[
ProcessWrapper::PROCESS_STATUS_PREPARED => 1,
ProcessWrapper::PROCESS_STATUS_QUEUED => 1,
ProcessWrapper::PROCESS_STATUS_DONE => 2,
],
$this->set->countStatuses()
);
}
public function testShouldCountResultsOfDoneProcesses(): void
{
$processWrapperMock = $this->createMock(ProcessWrapper::class);
$processWrapperMock->expects($this->exactly(4))
->method('getClassName')
->willReturnOnConsecutiveCalls('Process1', 'Process2', 'Process3', 'Process4');
$processWrapperMock->expects($this->exactly(4))
->method('getStatus')
->willReturn(ProcessWrapper::PROCESS_STATUS_DONE);
$processWrapperMock->expects($this->exactly(4))
->method('getResult')
->willReturnOnConsecutiveCalls(
ProcessWrapper::PROCESS_RESULT_PASSED,
ProcessWrapper::PROCESS_RESULT_FAILED,
ProcessWrapper::PROCESS_RESULT_FATAL,
ProcessWrapper::PROCESS_RESULT_PASSED
);
$this->set->add($processWrapperMock);
$this->set->add($processWrapperMock);
$this->set->add($processWrapperMock);
$this->set->add($processWrapperMock);
$this->assertEquals(
[
ProcessWrapper::PROCESS_RESULT_PASSED => 2,
ProcessWrapper::PROCESS_RESULT_FAILED => 1,
ProcessWrapper::PROCESS_RESULT_FATAL => 1,
],
$this->set->countResults()
);
}
public function testShouldFailBuildingTreeIfTestHasDependencyOnNotExistingTest(): void
{
$process = new ProcessWrapper(new Process(''), 'Foo');
$process->setDelay('NotExisting', 5);
$this->set->add($process);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(
'Testcase "Foo" has @delayAfter dependency on "NotExisting", but this testcase was not defined.'
);
$this->set->buildTree();
}
public function testShouldFailBuildingTreeIfCycleDetected(): void
{
/*
ROOT
A <--> B
*/
$processA = new ProcessWrapper(new Process(''), 'A');
$processA->setDelay('B', 1);
$processB = new ProcessWrapper(new Process(''), 'B');
$processB->setDelay('A', 1);
$this->set->add($processA);
$this->set->add($processB);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot build tree graph from tests dependencies.');
$this->set->buildTree();
}
public function testShouldBuildGraphTreeFromProcessDependencies(): void
{
// ROOT
// / \
// A B
// 3 / \ 5
// C D
// Order in which dependencies are added before buildTree() is called is not important,
// so add leafs to the tree first.
$processC = new ProcessWrapper(new Process(''), 'C');
$processC->setDelay('B', 3);
$processD = new ProcessWrapper(new Process(''), 'D');
$processD->setDelay('B', 5);
$processA = new ProcessWrapper(new Process(''), 'A');
$processB = new ProcessWrapper(new Process(''), 'B');
$this->set->add($processC);
$this->set->add($processD);
$this->set->add($processA);
$this->set->add($processB);
$tree = $this->set->buildTree();
$this->assertInstanceOf(OutTree::class, $tree);
$this->assertEquals(2, $tree->getHeight()); // 2, because single vertex graph has height 0
$root = $tree->getVertexRoot();
// Get vertices linked to root node (A, B)
$verticesToRoot = $root->getVerticesEdgeTo();
$this->assertCount(2, $verticesToRoot);
$this->assertEquals(['A', 'B'], $verticesToRoot->getIds());
// Default weight (if no dependency is specified) should be 0
$this->assertSame(0, $verticesToRoot->getVertexId('A')->getEdgesFrom($root)->getEdgeFirst()->getWeight());
// Vertex A should be a leaf but B should not
$this->assertTrue($tree->isVertexLeaf($verticesToRoot->getVertexId('A')));
$this->assertFalse($tree->isVertexLeaf($verticesToRoot->getVertexId('B')));
// Get vertices linked to node B (C, D)
$verticesToB = $tree->getVerticesChildren($verticesToRoot->getVertexId('B'));
$this->assertCount(2, $verticesToB);
$this->assertEquals(['C', 'D'], $verticesToB->getIds());
$this->assertTrue($tree->isVertexLeaf($verticesToB->getVertexId('C')));
$this->assertTrue($tree->isVertexLeaf($verticesToB->getVertexId('D')));
// Check weights of edges to C (= 3) and D (= 5
$this->assertEquals(3, $verticesToB->getVertexId('C')->getEdgesIn()->getEdgeFirst()->getWeight());
$this->assertEquals(5, $verticesToB->getVertexId('D')->getEdgesIn()->getEdgeFirst()->getWeight());
}
public function testShouldChangeOrderOfProcessesByGivenStrategy(): void
{
// ROOT
// / \
// A B
// 3 / \ 5
// C D
$processA = new ProcessWrapper(new Process(''), 'A');
$processB = new ProcessWrapper(new Process(''), 'B');
$processC = new ProcessWrapper(new Process(''), 'C');
$processC->setDelay('B', 3);
$processD = new ProcessWrapper(new Process(''), 'D');
$processD->setDelay('B', 5);
$this->set->add($processA);
$this->set->add($processB);
$this->set->add($processC);
$this->set->add($processD);
// Check original order, not yet affected by order strategy
$processesBefore = $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED);
$this->assertSame(['A', 'B', 'C', 'D'], array_keys($processesBefore));
// The MockOrderStrategy return processes with order values A - 0, B - 1, C - 2, D - 3.
// Thus after optimization the processes in processSet should be sorted descending from the highest number
$this->set->optimizeOrder(new MockOrderStrategy());
$processesAfter = $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED);
$this->assertSame(['D', 'C', 'B', 'A'], array_keys($processesAfter));
}
public function testShouldFailDependantsOfGivenProcess(): void
{
// ROOT
// / \
// A B
// 3 / \ 5
// C D
// | 0 (zero delay is also allowed)
// E
$processA = new ProcessWrapper(new Process(''), 'A');
$processB = new ProcessWrapper(new Process(''), 'B');
$processC = new ProcessWrapper(new Process(''), 'C');
$processC->setDelay('B', 3);
$processD = new ProcessWrapper(new Process(''), 'D');
$processD->setDelay('B', 5);
$processE = new ProcessWrapper(new Process(''), 'E');
$processE->setDelay('D', 0);
$this->set->add($processA);
$this->set->add($processB);
$this->set->add($processC);
$this->set->add($processD);
$this->set->add($processE);
$this->set->buildTree();
$processB->setStatus(ProcessWrapper::PROCESS_STATUS_DONE);
// Test preconditions - one process is done, other are queued
$this->assertCount(1, $this->set->get(ProcessWrapper::PROCESS_STATUS_DONE));
$this->assertCount(4, $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED));
$this->assertEquals(1, $this->set->countResults()[ProcessWrapper::PROCESS_RESULT_FAILED]);
$failedProcesses = $this->set->failDependants('B');
$this->assertCount(3, $failedProcesses);
$this->assertContainsOnlyInstancesOf(ProcessWrapper::class, $failedProcesses);
$this->assertArrayHasKey('C', $failedProcesses);
$this->assertArrayHasKey('D', $failedProcesses);
$this->assertArrayHasKey('E', $failedProcesses);
// A will still be queued
$this->assertCount(1, $this->set->get(ProcessWrapper::PROCESS_STATUS_QUEUED));
$this->assertSame(ProcessWrapper::PROCESS_STATUS_QUEUED, $processA->getStatus());
// C, D and E processes will be done and failed
$this->assertCount(4, $this->set->get(ProcessWrapper::PROCESS_STATUS_DONE));
$this->assertSame(ProcessWrapper::PROCESS_STATUS_DONE, $processC->getStatus());
$this->assertSame(ProcessWrapper::PROCESS_STATUS_DONE, $processD->getStatus());
$this->assertSame(ProcessWrapper::PROCESS_STATUS_DONE, $processE->getStatus());
$this->assertEquals(4, $this->set->countResults()[ProcessWrapper::PROCESS_RESULT_FAILED]);
$this->assertSame(ProcessWrapper::PROCESS_RESULT_FAILED, $processC->getResult());
$this->assertSame(ProcessWrapper::PROCESS_RESULT_FAILED, $processD->getResult());
$this->assertSame(ProcessWrapper::PROCESS_RESULT_FAILED, $processE->getResult());
}
public function testShouldFailWhenFailingDependantsButTheTreeWasNotYetBuilt(): void
{
$processA = new ProcessWrapper(new Process(''), 'A');
$processB = new ProcessWrapper(new Process(''), 'B');
$processB->setDelay('A', 3);
$this->set->add($processA);
$this->set->add($processB);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot get dependency tree - the tree was not yet build using buildTree()');
$this->set->failDependants('A');
}
}