Passed
Push — test ( d838cf...cef4a5 )
by Tom
03:29
created

StepsTest::testParseErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 35
nc 1
nop 0
dl 0
loc 61
rs 9.36
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Bug introduced by
It seems like $steps can also be of type ArrayAccess; however, parameter $haystack of PHPUnit\Framework\Assert...ntainsOnlyInstancesOf() does only seem to accept iterable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
        self::assertContainsOnlyInstancesOf('Ktomk\Pipelines\File\Pipeline\Step', /** @scrutinizer ignore-type */ $steps);
Loading history...
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