|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File\Pipeline; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\File\File; |
|
8
|
|
|
use Ktomk\Pipelines\File\ParseException; |
|
9
|
|
|
use Ktomk\Pipelines\File\Pipeline; |
|
10
|
|
|
use Ktomk\Pipelines\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class PipelineTest |
|
14
|
|
|
* |
|
15
|
|
|
* @covers \Ktomk\Pipelines\File\Pipeline\Steps |
|
16
|
|
|
*/ |
|
17
|
|
|
class StepsTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @return Steps |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testCreation() |
|
23
|
|
|
{ |
|
24
|
|
|
$definition = array(array('step' => array('script' => array(':')))); |
|
25
|
|
|
$pipeline = $this->getTestPipeline(); |
|
26
|
|
|
$steps = new Steps($pipeline, $definition); |
|
27
|
|
|
self::assertInstanceOf('Ktomk\Pipelines\File\Pipeline\Steps', $steps); |
|
28
|
|
|
|
|
29
|
|
|
return $steps; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @covers \Ktomk\Pipelines\File\Pipeline\Step |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testParseErrors() |
|
36
|
|
|
{ |
|
37
|
|
|
$pipeline = $this->getTestPipeline(); |
|
38
|
|
|
|
|
39
|
|
|
# things are needed |
|
40
|
|
|
$this->assertParseException( |
|
41
|
|
|
$pipeline, |
|
42
|
|
|
array(), |
|
43
|
|
|
'Steps requires a tree of steps' |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
# steps (the list) is needed |
|
47
|
|
|
$this->assertParseException( |
|
48
|
|
|
$pipeline, |
|
49
|
|
|
array('foo'), |
|
50
|
|
|
'Step expected, got string' |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
# concrete steps are needed |
|
54
|
|
|
$this->assertParseException( |
|
55
|
|
|
$pipeline, |
|
56
|
|
|
array(array()), |
|
57
|
|
|
'Step expected, got empty array' |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
# concrete steps are needed |
|
61
|
|
|
$this->assertParseException( |
|
62
|
|
|
$pipeline, |
|
63
|
|
|
array(array('wrong-name' => array())), |
|
64
|
|
|
"Unexpected pipeline property 'wrong-name', expected 'step' or 'parallel'" |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
# concrete steps are needed |
|
68
|
|
|
$this->assertParseException( |
|
69
|
|
|
$pipeline, |
|
70
|
|
|
array(array('parallel' => array(array()))), |
|
71
|
|
|
'Parallel step must consist of steps only' |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
# trigger: manual not on first step |
|
75
|
|
|
$this->assertParseException( |
|
76
|
|
|
$pipeline, |
|
77
|
|
|
array(array('step' => array('trigger' => 'manual'))), |
|
78
|
|
|
"The first step of a pipeline can't be manually triggered" |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
# trigger not in parallel step |
|
82
|
|
|
$this->assertParseException( |
|
83
|
|
|
$pipeline, |
|
84
|
|
|
array( |
|
85
|
|
|
array('step' => array('trigger' => 'automatic', 'script' => array(':'))), |
|
86
|
|
|
array('parallel' => array(array('step' => array('trigger' => 'manual')))), |
|
87
|
|
|
), |
|
88
|
|
|
"Unexpected property 'trigger' in parallel step" |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
# trigger: manual or automatic only |
|
92
|
|
|
$this->assertParseException( |
|
93
|
|
|
$pipeline, |
|
94
|
|
|
array(array('step' => array('trigger' => 'foo'))), |
|
95
|
|
|
"'trigger' expects either 'manual' or 'automatic'" |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testGetSteps() |
|
100
|
|
|
{ |
|
101
|
|
|
$pipeline = $this->getTestPipeline(); |
|
102
|
|
|
$definition = array(array('step' => array('script' => array(':')))); |
|
103
|
|
|
$steps = new Steps($pipeline, $definition); |
|
104
|
|
|
$array = $steps->getSteps(); |
|
105
|
|
|
self::assertIsArray($array); |
|
106
|
|
|
self::assertCount(1, $array); |
|
107
|
|
|
self::assertArrayHasKey(0, $array); |
|
108
|
|
|
self::assertInstanceOf('Ktomk\Pipelines\File\Pipeline\Step', $array[0]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @covers \Ktomk\Pipelines\File\Pipeline\Steps::setGetIteratorFunctor |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testSetGetIteratorFunctor() |
|
115
|
|
|
{ |
|
116
|
|
|
$pipeline = $this->getTestPipeline(); |
|
117
|
|
|
$definition = array(array('step' => array('script' => array(':')))); |
|
118
|
|
|
$steps = new Steps($pipeline, $definition); |
|
119
|
|
|
self::assertCount(count($steps), $steps->getIterator(), 'pre-condition'); |
|
120
|
|
|
$steps->setGetIteratorFunctor(function (Steps $steps) { |
|
121
|
|
|
return new \ArrayIterator(array_merge($steps->getSteps(), $steps->getSteps())); |
|
122
|
|
|
}); |
|
123
|
|
|
self::assertCount(count($steps) * 2, $steps->getIterator()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testGetStepsInherited() |
|
127
|
|
|
{ |
|
128
|
|
|
$pipeline = $this->getTestPipeline(); |
|
129
|
|
|
$definition = array( |
|
130
|
|
|
array('step' => array('script' => array(': 0'))), |
|
131
|
|
|
array('parallel' => array( |
|
132
|
|
|
array('step' => array('script' => array(': 1'))), |
|
133
|
|
|
array('step' => array('script' => array(': 2'))), |
|
134
|
|
|
)), |
|
135
|
|
|
); |
|
136
|
|
|
$steps = new Steps($pipeline, $definition); |
|
137
|
|
|
$array = $steps->getSteps(); |
|
138
|
|
|
self::assertIsArray($array); |
|
139
|
|
|
self::assertCount(3, $array); |
|
140
|
|
|
|
|
141
|
|
|
self::assertArrayHasKey(0, $array); |
|
142
|
|
|
self::assertArrayHasKey(1, $array); |
|
143
|
|
|
self::assertArrayHasKey(2, $array); |
|
144
|
|
|
self::assertArrayNotHasKey(-1, $array); |
|
145
|
|
|
self::assertArrayNotHasKey(3, $array); |
|
146
|
|
|
|
|
147
|
|
|
self::assertContainsOnlyInstancesOf('Ktomk\Pipelines\File\Pipeline\Step', $array); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testGetStepsArrayInherited() |
|
151
|
|
|
{ |
|
152
|
|
|
$pipeline = $this->getTestPipeline(); |
|
153
|
|
|
$definition = array( |
|
154
|
|
|
array('step' => array('script' => array(': 0'))), |
|
155
|
|
|
array('parallel' => array( |
|
156
|
|
|
array('step' => array('script' => array(': 1'))), |
|
157
|
|
|
array('step' => array('script' => array(': 2'))), |
|
158
|
|
|
)), |
|
159
|
|
|
); |
|
160
|
|
|
$steps = new Steps($pipeline, $definition); |
|
161
|
|
|
self::assertCount(3, $steps); |
|
162
|
|
|
|
|
163
|
|
|
self::assertArrayHasKey(0, $steps); |
|
164
|
|
|
self::assertArrayHasKey(1, $steps); |
|
165
|
|
|
self::assertArrayHasKey(2, $steps); |
|
166
|
|
|
self::assertArrayNotHasKey(-1, $steps); |
|
167
|
|
|
self::assertArrayNotHasKey(3, $steps); |
|
168
|
|
|
|
|
169
|
|
|
self::assertContainsOnlyInstancesOf('Ktomk\Pipelines\File\Pipeline\Step', $steps); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
self::assertInstanceOf('Ktomk\Pipelines\File\Pipeline\Step', $steps[0]); |
|
172
|
|
|
|
|
173
|
|
|
self::assertTrue($steps->offsetExists(0)); |
|
174
|
|
|
self::assertTrue($steps->offsetExists(1)); |
|
175
|
|
|
self::assertTrue($steps->offsetExists(2)); |
|
176
|
|
|
self::assertFalse($steps->offsetExists(-1)); |
|
177
|
|
|
self::assertFalse($steps->offsetExists(3)); |
|
178
|
|
|
|
|
179
|
|
|
try { |
|
180
|
|
|
$steps[0] = 'foo'; |
|
181
|
|
|
self::fail('an expected exception has not been thrown'); |
|
182
|
|
|
} catch (\BadMethodCallException $ex) { |
|
183
|
|
|
$this->addToAssertionCount(1); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
try { |
|
187
|
|
|
unset($steps[0]); |
|
188
|
|
|
self::fail('an expected exception has not been thrown'); |
|
189
|
|
|
} catch (\BadMethodCallException $ex) { |
|
190
|
|
|
$this->addToAssertionCount(1); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function testGetPipeline() |
|
195
|
|
|
{ |
|
196
|
|
|
$definition = array(array('step' => array('script' => array(':')))); |
|
197
|
|
|
$pipeline = $this->getTestPipeline(); |
|
198
|
|
|
$steps = new Steps($pipeline, $definition); |
|
199
|
|
|
|
|
200
|
|
|
self::assertSame($pipeline, $steps->getPipeline()); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @depends testCreation |
|
205
|
|
|
* |
|
206
|
|
|
* @param Steps $steps |
|
207
|
|
|
*/ |
|
208
|
|
|
public function testJsonSerialize(Steps $steps) |
|
209
|
|
|
{ |
|
210
|
|
|
self::assertArrayHasKey( |
|
211
|
|
|
'steps', |
|
212
|
|
|
$steps->jsonSerialize() |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function testFullIter() |
|
217
|
|
|
{ |
|
218
|
|
|
self::assertNotNull(Steps::fullIter(null)); |
|
219
|
|
|
|
|
220
|
|
|
$steps = $this->createConfiguredMock('Ktomk\Pipelines\File\Pipeline\Steps', array( |
|
221
|
|
|
'getIterator' => $this->createMock('Ktomk\Pipelines\File\Pipeline\StepsIterator'), |
|
222
|
|
|
)); |
|
223
|
|
|
|
|
224
|
|
|
self::assertNotNull(Steps::fullIter($steps)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testTestParseEmptyStep() |
|
228
|
|
|
{ |
|
229
|
|
|
$file = File::createFromFile(__DIR__ . '/../../../data/yml/invalid-pipeline-step.yml'); |
|
230
|
|
|
|
|
231
|
|
|
$this->expectException('Ktomk\Pipelines\File\ParseException'); |
|
232
|
|
|
$this->expectExceptionMessage('file parse error: step requires a script'); |
|
233
|
|
|
$file->getDefault(); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
private function assertParseException(Pipeline $pipeline, array $array, $expected) |
|
237
|
|
|
{ |
|
238
|
|
|
try { |
|
239
|
|
|
new Steps($pipeline, $array); |
|
240
|
|
|
self::fail('an expected exception has not been thrown'); |
|
241
|
|
|
} catch (ParseException $e) { |
|
242
|
|
|
self::assertSame($expected, $e->getParseMessage()); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @return Pipeline |
|
248
|
|
|
*/ |
|
249
|
|
|
private function getTestPipeline() |
|
250
|
|
|
{ |
|
251
|
|
|
$file = new File(array('pipelines' => array('default' => array()))); |
|
252
|
|
|
$definition = array(array('step' => array('script' => array(':')))); |
|
253
|
|
|
|
|
254
|
|
|
return new Pipeline($file, $definition); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|