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

ReferenceTest::testParsing()   A

Complexity

Conditions 6
Paths 22

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 22
nop 2
dl 0
loc 16
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\TestCase;
9
10
/**
11
 * @covers \Ktomk\Pipelines\Runner\Reference
12
 */
13
class ReferenceTest extends TestCase
14
{
15
    public function testCreation()
16
    {
17
        $ref = Reference::create(null);
18
        self::assertNotNull($ref);
19
    }
20
21
    /**
22
     * @return array
23
     */
24
    public function provideRefs()
25
    {
26
        return array(
27
            array(null, false),
28
            array('', false),
29
            array('bar:', false),
30
            array('tag', false),
31
            array('tag:', false),
32
            array('tag:1.0.0', true),
33
            array('branch:feature/drop-support', true),
34
            array('pr:feature-42', true),
35
        );
36
    }
37
38
    /**
39
     * @dataProvider provideRefs
40
     *
41
     * @param string $string
42
     * @param bool $valid
43
     */
44
    public function testValidation($string, $valid)
45
    {
46
        self::assertSame($valid, Reference::valid($string), sprintf('reference "%s"', $string));
47
    }
48
49
    /**
50
     * @dataProvider provideRefs
51
     *
52
     * @param $string
53
     * @param $valid
54
     */
55
    public function testParsing($string, $valid)
56
    {
57
        try {
58
            $this->addToAssertionCount(1);
59
            $type = Reference::create($string);
60
            if (null !== $string && !$valid) {
61
                self::fail('An expected exception has not been thrown');
62
            }
63
            self::assertNotNull($type);
64
            if (null !== $string) {
65
                self::assertNotNull($type->getType());
66
                self::assertNotNull($type->getName());
67
            }
68
        } catch (InvalidArgumentException $e) {
69
            if ($valid) {
70
                self::fail('Exception');
71
            }
72
        }
73
    }
74
75
    public function testNullObject()
76
    {
77
        $ref = new Reference(null);
78
        self::assertNull($ref->getType());
79
        self::assertNull($ref->getName());
80
    }
81
82
    public function testGetPipelinesType()
83
    {
84
        $f = function ($ref) {
85
            return Reference::create($ref)->getPipelinesType();
86
        };
87
88
        self::assertNull($f(null));
89
        self::assertSame('bookmarks', $f('bookmark:stable'));
90
        self::assertSame('branches', $f('branch:master'));
91
        self::assertSame('tags', $f('tag:1.0.0'));
92
        self::assertSame('pull-requests', $f('pr:feature'));
93
    }
94
}
95