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

GlobTest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 31
nc 1
nop 3
dl 0
loc 41
rs 9.424
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines;
6
7
/**
8
 * @covers \Ktomk\Pipelines\Glob
9
 */
10
class GlobTest extends TestCase
11
{
12
    /**
13
     * @var array
14
     */
15
    private $levels;
16
17
    public function __construct($name = null, array $data = array(), $dataName = '')
18
    {
19
        $this->levels = array(
20
            # single asterisk matching
21
            'level-1' => array(
22
                'equality' => array('master', 'master', true),
23
                'globally' => array('*', 'master', true),
24
                'partially' => array('*ter', 'master', true),
25
                'emptily' => array('*master', 'master', true),
26
                'emptily suffix' => array('master*', 'master', true),
27
                'emptily in between' => array('mas*ter*', 'master', true),
28
                'partially in between' => array('ma*er*', 'master', true),
29
                'multi-partially in between' => array('m*s*r', 'master', true),
30
                array('*/master', 'feature/master', true),
31
                array('*/master', 'feature/minor', false),
32
                array('feature/*', 'feature/master', true),
33
                array('feature/*', 'hotfix/minor', false),
34
                array('feature/bb-123-fix-links', 'feature/minor', false),
35
                array('feature/bb-123-fix-links', 'feature/bb-123-fix-links', true),
36
                'version-tag-test' => array('v?*.*?.?*', 'v0.0.0', true),
37
            ),
38
            # differentiation between single and double asterisk
39
            'level-2' => array(
40
                '* not match dir separator' => array('*', 'feature/blue', false),
41
                '** matches' => array('**', 'feature/blue', true),
42
                '** partially' => array('**ue', 'feature/blue', true),
43
                '** partially no match' => array('**zz', 'feature/blue', false),
44
            ),
45
            # brace support
46
            'level-3' => array(
47
                'as seen t1' => array('{foo,bar}', 'foo', true),
48
                'as seen t2' => array('{foo,bar}', 'bar', true),
49
                'as seen t3' => array('{foo,bar}', 'foo,bar', false),
50
                'braces {,} do match' => array('feature/{red,blue}', 'feature/blue', true),
51
                'empty braces not' => array('{}', '{}', true),
52
                'recursive' => array('{f{,o,{oo,o}},bar}', 'foo', true),
53
                'recursive end' => array('{f{,o,{oo,o}},bar}', 'bar', true),
54
            ),
55
        );
56
57
        parent::__construct($name, $data, $dataName);
58
    }
59
60
    public function provideBracePattern()
61
    {
62
        return array(
63
            array(array(''), ''),
64
            array(array('{}'), '{}'),
65
            'no duplicates' => array(array(''), '{,}'),
66
            'no duplicates 2' => array(array('ab'), 'a{,}b'),
67
            array(array('acb', 'adb'), 'a{c,d}b'),
68
            'hangover left' => array(array('a{cb', 'a{db'), 'a{{c,d}b'),
69
            'hangover right' => array(array('ac}b', 'ad}b'), 'a{c,d}}b'),
70
            array(array('abe', 'ace', 'ade'), 'a{b,{c,d}}e'),
71
            'brace' => array(array('ab', 'ac'), 'a{b,c}'),
72
            'escaped brace' => array(array('a{b,c}'), "a\\{b,c}"),
73
            'escaped comma' => array(array('a,', 'ab'), "a{\\,,b}"),
74
            'multiple' => array(
75
                array('abdh', 'abefh', 'abgh', 'abcdh', 'abcefh', 'abcgh'),
76
                'ab{,c}{d,{ef,g}}h',
77
            ),
78
            'src-src/**/*.php-test-1' => array(
79
                array('src/*/*.php', 'src/*.php'),
80
                'src/{*/,}*.php',
81
            ),
82
            'src-src/**/*.php-test-2' => array(
83
                array('src/*/*/*.php', 'src/*.php', 'src/*/*.php'),
84
                'src/{{*/,}*/,}*.php',
85
            ),
86
            'src-src/**/*.php-test-2-reverse-bug' => array(
87
                array('src/*/*.php', 'src/*.php'),
88
                'src/{*/,{*/,}}*.php',
89
            ),
90
        );
91
    }
92
93
    public function provideMatchPatternLevel1()
94
    {
95
        return $this->levels['level-1'];
96
    }
97
98
    public function provideMatchPatternLevel2()
99
    {
100
        return $this->levels['level-2'];
101
    }
102
103
    public function provideMatchPatternLevel3()
104
    {
105
        return $this->levels['level-3'];
106
    }
107
108
    /**
109
     * @dataProvider provideMatchPatternLevel1
110
     *
111
     * @param mixed $pattern
112
     * @param mixed $subject
113
     * @param mixed $expected
114
     */
115
    public function testMatchLevel1($pattern, $subject, $expected)
116
    {
117
        $this->assert($pattern, $subject, $expected);
118
    }
119
120
    /**
121
     * @dataProvider provideMatchPatternLevel2
122
     *
123
     * @param mixed $pattern
124
     * @param mixed $subject
125
     * @param mixed $expected
126
     */
127
    public function testMatchLevel2($pattern, $subject, $expected)
128
    {
129
        $this->assert($pattern, $subject, $expected);
130
    }
131
132
    /**
133
     * @dataProvider provideMatchPatternLevel3
134
     *
135
     * @param mixed $pattern
136
     * @param mixed $subject
137
     * @param mixed $expected
138
     */
139
    public function testMatchLevel3($pattern, $subject, $expected)
140
    {
141
        $this->assert($pattern, $subject, $expected);
142
    }
143
144
    /**
145
     * @param $expected
146
     * @param $subject
147
     * @dataProvider provideBracePattern
148
     */
149
    public function testExpandBrace($expected, $subject)
150
    {
151
        self::assertSame($expected, Glob::expandBrace($subject), $subject);
152
    }
153
154
    /**
155
     * @param string $pattern
156
     * @param string $subject
157
     * @param bool $expected
158
     */
159
    private function assert($pattern, $subject, $expected)
160
    {
161
        $actual = Glob::match($pattern, $subject);
162
        self::assertSame($expected, $actual, sprintf(
163
            "pattern '%s' %s subject '%s'",
164
            $pattern,
165
            $expected ? 'matches' : 'mismatches',
166
            $subject
167
        ));
168
    }
169
}
170