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

PipelinesTest::testGetPipelines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\File;
6
7
use Ktomk\Pipelines\Runner\Reference;
8
use Ktomk\Pipelines\TestCase;
9
use Ktomk\Pipelines\Yaml\Yaml;
10
use ReflectionException;
11
use ReflectionObject;
12
13
/**
14
 * Class PipelinesTest
15
 *
16
 * @package Ktomk\Pipelines\File
17
 * @covers \Ktomk\Pipelines\File\Pipelines
18
 */
19
class PipelinesTest extends TestCase
20
{
21
    /**
22
     * @return Pipelines
23
     */
24
    public function testCreateFromDefaultFile()
25
    {
26
        $path = __DIR__ . '/../../../' . File::FILE_NAME;
27
28
        $result = Yaml::file($path);
29
30
        self::assertArrayHasKey('pipelines', $result, 'file fixture broken');
31
        self::assertIsArray($result['pipelines'], 'file fixture broken');
32
33
        $pipelines = new Pipelines($result['pipelines'], $this->createMock('Ktomk\Pipelines\File\File'));
34
35
        self::assertInstanceOf('Ktomk\Pipelines\File\Pipelines', $pipelines);
36
37
        return $pipelines;
38
    }
39
40
    /**
41
     * @depends testCreateFromDefaultFile
42
     *
43
     * @param Pipelines $pipelines
44
     *
45
     * @return void
46
     */
47
    public function testGetPipelines(Pipelines $pipelines)
48
    {
49
        $actual = $pipelines->getPipelines();
50
        self::assertGreaterThan(1, count($actual));
51
        self::assertContainsOnlyInstancesOf(
52
            'Ktomk\Pipelines\File\Pipeline',
53
            $actual
54
        );
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function provideParseErrors()
61
    {
62
        return array(
63
            array(array(), "'pipelines' requires at least a default,"),
64
            array(
65
                array('default' => null),
66
                "'pipelines' requires at least a default,",
67
            ),
68
            array(
69
                array('default' => 'foo'),
70
                "'default' requires a list of steps",
71
            ),
72
            array(
73
                array('branches' => 'foo'),
74
                "'branches' requires a list",
75
            ),
76
        );
77
    }
78
79
    /**
80
     * @dataProvider provideParseErrors
81
     *
82
     * @param array $array
83
     * @param string $message
84
     */
85
    public function testParseErrors(array $array, $message)
86
    {
87
        $this->expectException('Ktomk\Pipelines\File\ParseException');
88
        $this->expectExceptionMessage($message);
89
90
        new Pipelines($array);
91
    }
92
93
    /**
94
     * @return void
95
     */
96
    public function testGetFileUnassociated()
97
    {
98
        $pipelines = new Pipelines($this->getMinimalArray(), $mock = $this->createMock('Ktomk\Pipelines\File\File'));
99
        self::assertSame($mock, $pipelines->getFile());
100
101
        $pipelines = new Pipelines($this->getMinimalArray());
102
        self::assertNull($pipelines->getFile());
103
    }
104
105
    /**
106
     * test that in the internal file array, the pipelines
107
     * data gets referenced to the concrete pipeline object
108
     * when it once hast been acquired.
109
     *
110
     * @throws ReflectionException
111
     *
112
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
113
     */
114
    public function testFlyweightPatternWithPatternSection()
115
    {
116
        $withBranch = array(
117
            'branches' => array(
118
                'master' => array(array('step' => array(
119
                    'name' => 'master branch',
120
                    'script' => array('1st line'),
121
                ))),
122
            ),
123
        );
124
        $file = $this->createMock('Ktomk\Pipelines\File\File');
125
        $pipelines = new Pipelines($withBranch, $file);
126
127
        $pipeline = $pipelines->searchTypeReference('branches', 'master');
128
        $this->asPlFiStName('master branch', $pipeline);
129
130
        $reflection = new ReflectionObject($pipelines);
131
        $prop = $reflection->getProperty('array');
132
        $prop->setAccessible(true);
133
        $array = $prop->getValue($pipelines);
134
        $actual = $array['branches']['master'];
135
        self::assertSame($pipeline, $actual);
136
    }
137
138
    public function testGetReference()
139
    {
140
        $file = File::createFromFile(__DIR__ . '/../../data/yml/bitbucket-pipelines.yml');
141
142
        $pipeline = $file->getById('branches/master');
143
        self::assertNotNull($pipeline);
144
145
        # test instance count
146
        $default = $file->getById('default');
147
        self::assertSame($default, $file->getDefault());
148
    }
149
150
    public function testGetPipelineIds()
151
    {
152
        $pipelines = File::createFromFile(__DIR__ . '/../../data/yml/bitbucket-pipelines.yml')->getPipelines();
153
        $ids = $pipelines->getPipelineIds();
154
        self::assertIsArray($ids);
155
        self::assertArrayHasKey(12, $ids);
156
        self::assertSame('custom/unit-tests', $ids[12]);
157
    }
158
159
    /**
160
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
161
     */
162
    public function testSearchReference()
163
    {
164
        $pipelines = File::createFromFile(__DIR__ . '/../../data/yml/bitbucket-pipelines.yml')->getPipelines();
165
166
        $pipeline = $pipelines->searchTypeReference('branches', 'master');
167
        $this->asPlFiStName('master duplicate', $pipeline, 'direct match');
168
169
        $pipeline = $pipelines->searchTypeReference('branches', 'my/feature');
170
        $this->asPlFiStName('*/feature', $pipeline);
171
    }
172
173
    /**
174
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
175
     */
176
    public function testDefaultAsFallBack()
177
    {
178
        $withDefault = array(
179
            'default' => array(
180
                array('step' => array(
181
                    'name' => 'default',
182
                    'script' => array('1st line'),
183
                )),
184
            ),
185
            'branches' => array(
186
                'master' => array(array('step' => array(
187
                    'name' => 'master branch',
188
                    'script' => array('1st line'),
189
                ))),
190
            ),
191
        );
192
        $pipelines = new Pipelines($withDefault, $this->createMock('Ktomk\Pipelines\File\File'));
193
194
        $reference = Reference::create('bookmark:xy');
195
        $pipeline = $pipelines->searchReference($reference);
196
        $this->asPlFiStName('default', $pipeline);
0 ignored issues
show
Bug introduced by
It seems like $pipeline can also be of type null; however, parameter $pipeline of Ktomk\Pipelines\File\PipelinesTest::asPlFiStName() does only seem to accept Ktomk\Pipelines\File\Pipeline, 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

196
        $this->asPlFiStName('default', /** @scrutinizer ignore-type */ $pipeline);
Loading history...
197
198
        $pipeline = $pipelines->searchTypeReference('bookmarks', 'xy');
199
        $this->asPlFiStName('default', $pipeline);
200
201
        $pipeline = $pipelines->searchTypeReference('branches', 'feature/xy');
202
        $this->asPlFiStName('default', $pipeline);
203
    }
204
205
    /**
206
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
207
     */
208
    public function testNoDefaultAsFallBack()
209
    {
210
        $withoutDefault = array(
211
            'branches' => array(
212
                'master' => array(array('step' => array(
213
                    'name' => 'master branch',
214
                    'script' => array('1st line'),
215
                ))),
216
            ),
217
        );
218
        $pipelines = new Pipelines($withoutDefault);
219
220
        self::assertNull($pipelines->getIdDefault());
221
        self::assertNull($pipelines->getDefault());
222
223
        $reference = Reference::create();
224
        $pipeline = $pipelines->searchReference($reference);
225
        self::assertNull($pipeline);
226
227
        $reference = Reference::create();
228
        $pipeline = $pipelines->searchIdByReference($reference);
229
        self::assertNull($pipeline);
230
231
        $reference = Reference::create('bookmark:xy');
232
        $pipeline = $pipelines->searchIdByReference($reference);
233
        self::assertNull($pipeline);
234
    }
235
236
    /**
237
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
238
     */
239
    public function testSearchReferenceInvalidScopeException()
240
    {
241
        $this->expectException('InvalidArgumentException');
242
        $this->expectExceptionMessage('Invalid type \'invalid\'');
243
244
        $file = File::createFromFile(__DIR__ . '/../../data/yml/bitbucket-pipelines.yml');
245
        $file->getPipelines()->searchTypeReference('invalid', '');
246
    }
247
248
    /**
249
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
250
     */
251
    public function testParseErrorOnGetById()
252
    {
253
        $this->expectException('Ktomk\Pipelines\File\ParseException');
254
        $this->expectExceptionMessage('custom/0: named pipeline required');
255
256
        $file = new File(array(
257
            'pipelines' => array(
258
                'custom' => array(
259
                    'void',
260
                ),
261
            ),
262
        ));
263
        $file->getById('custom/0');
264
    }
265
266
    /**
267
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
268
     */
269
    public function testGetIdOfPipeline()
270
    {
271
        $file = new File(array('pipelines' => array('default' => array(
272
            array('step' => array('script' => array(':'))),
273
        ))));
274
        $pipelines = $file->getPipelines();
275
        $pipeline = $pipelines->getById('default');
276
        self::assertNotNull($pipeline);
277
        $actual = $pipelines->getId($pipeline);
0 ignored issues
show
Bug introduced by
It seems like $pipeline can also be of type null; however, parameter $pipeline of Ktomk\Pipelines\File\Pipelines::getId() does only seem to accept Ktomk\Pipelines\File\Pipeline, 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

277
        $actual = $pipelines->getId(/** @scrutinizer ignore-type */ $pipeline);
Loading history...
278
        self::assertSame('default', $actual);
279
    }
280
281
    /**
282
     * An non-associated pipeline in a pipelines object can't be obtained
283
     * an id from
284
     *
285
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
286
     */
287
    public function testGetIdOfNonFilePipelineReturnsNull()
288
    {
289
        $file = new File(array('pipelines' => array('default' => array(
290
            array('step' => array('script' => array(':'))),
291
        ))));
292
        $pipelines = $file->getPipelines();
293
294
        $pipeline = new Pipeline($file, array(array('step' => array('script' => array(':')))));
295
        self::assertNull($pipelines->getId($pipeline));
296
    }
297
298
    /**
299
     * @covers \Ktomk\Pipelines\File\PipelinesReferences
300
     */
301
    public function testInvalidReferenceName()
302
    {
303
        $this->expectException('InvalidArgumentException');
304
        $this->expectExceptionMessage('Invalid id \'branch/master\'');
305
306
        File::createFromFile(__DIR__ . '/../../data/yml/bitbucket-pipelines.yml')
307
            ->getById('branch/master'); # must be branch_es_
308
    }
309
310
    /**
311
     * @return void
312
     */
313
    public function testGetPipelinesWithInvalidIdParseError()
314
    {
315
        $path = __DIR__ . '/../../data/yml/invalid-pipeline-id.yml';
316
317
        $file = File::createFromFile($path);
318
319
        self::assertNotNull($file);
320
321
        $this->expectException('Ktomk\Pipelines\File\ParseException');
322
        $this->expectExceptionMessage('file parse error: invalid pipeline id \'');
323
324
        $file->getPipelines()->getPipelines();
325
    }
326
327
    /**
328
     * pipelines fixture
329
     *
330
     * @return array
331
     */
332
    private function getMinimalArray()
333
    {
334
        return array('default' => array());
335
    }
336
337
    /**
338
     * assertPipelineFirstStepName
339
     *
340
     * @param string $expected
341
     * @param Pipeline $pipeline
342
     * @param string $message [optional]
343
     */
344
    private function asPlFiStName($expected, Pipeline $pipeline, $message = '')
345
    {
346
        $steps = $pipeline->getSteps();
347
        $first = $steps[0];
348
        self::assertSame($expected, $first->getName(), $message);
349
    }
350
}
351