Failed Conditions
Push — master ( 05acd6...202119 )
by Arnold
07:31
created

PcreExtensionTest::filterProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Jasny\Twig;
4
5
use Jasny\Twig\PcreExtension;
6
use Jasny\Twig\TestHelper;
7
8
/**
9
 * @covers Jasny\Twig\PcreExtension
10
 */
11
class PcreExtensionTest extends \PHPUnit_Framework_TestCase
12
{
13
    use TestHelper;
14
    
15
    protected function getExtension()
16
    {
17
        return new PcreExtension();
18
    }
19
    
20
    
21
    public function testGetName()
22
    {
23
        $this->assertEquals('jasny/pcre', $this->getExtension()->getName());
24
    }
25
    
26
27
    public function testQuote()
28
    {
29
        $this->assertRender('foo\(\)', '{{ "foo()"|preg_quote }}');
30
    }
31
    
32
    public function testQuoteDelimiter()
33
    {
34
        $this->assertRender('foo\@bar', '{{ "foo@bar"|preg_quote("@") }}');
35
    }
36
    
37
    public function testPregMatch()
38
    {
39
        $this->assertRender('YES', '{% if "foo"|preg_match("/oo/") %}YES{% else %}NO{% endif %}');
40
    }
41
42
    public function testPregMatchNo()
43
    {
44
        $this->assertRender('NO', '{% if "fod"|preg_match("/oo/") %}YES{% else %}NO{% endif %}');
45
    }
46
47
    /**
48
     * @expectedException \Twig_Error_Runtime
49
     */
50
    public function testPregMatchError()
51
    {
52
        $this->render('{% if "fod"|preg_match("/o//o/") %}YES{% else %}NO{% endif %}');
53
    }
54
    
55
    
56
    public function testPregGet()
57
    {
58
        $this->assertRender('d', '{{ "food"|preg_get("/oo(.)/", 1) }}');
59
    }
60
    
61
    public function testPregGetDefault()
62
    {
63
        $this->assertRender('ood', '{{ "food"|preg_get("/oo(.)/") }}');
64
    }
65
    
66
    
67
    public function testPregGetAll()
68
    {
69
        $this->assertRender('d|t|m', '{{ "food woot should doom"|preg_get_all("/oo(.)/", 1)|join("|") }}');
70
    }
71
    
72
    public function testPregGetAllDefault()
73
    {
74
        $this->assertRender('ood|oot|oom', '{{ "food woot doom"|preg_get_all("/oo(.)/")|join("|") }}');
75
    }
76
    
77
    
78
    public function testPregGrep()
79
    {
80
        $this->assertRender(
81
            'world|how|you',
82
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_grep("/o./")|join("|") }}'
83
        );
84
    }
85
    
86
    public function testPregGrepInvert()
87
    {
88
        $this->assertRender(
89
            'hello|sweet|are',
90
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_grep("/o./", "invert")|join("|") }}'
91
        );
92
    }
93
    
94
    
95
    public function testReplace()
96
    {
97
        $this->assertRender(
98
            'the quick brawen faxe jumped aveer the lazy dage',
99
            '{{ "the quick brown fox jumped over the lazy dog"|preg_replace("/o(\\\\w)/", "a$1e") }}'
100
        );
101
    }
102
    
103
    public function testReplaceLimit()
104
    {
105
        $this->assertRender(
106
            'the quick brawen faxe jumped over the lazy dog',
107
            '{{ "the quick brown fox jumped over the lazy dog"|preg_replace("/o(\\\\w)/", "a$1e", 2) }}'
108
        );
109
    }
110
    
111
    public function testReplaceWithArray()
112
    {
113
        $this->assertRender(
114
            'hello|sweet|wareld|hawe|are|yaue',
115
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_replace("/o(.)/", "a$1e")|join("|") }}'
116
        );
117
    }
118
    
119
    /**
120
     * @expectedException Twig_Error_Runtime
121
     */
122
    public function testReplaceAssertNoEval()
123
    {
124
        $this->render('{{ "foo"|preg_replace("/o/ei", "strtoupper") }}');
125
    }
126
    
127
    
128
    public function testFilter()
129
    {
130
        $this->assertRender(
131
            'wareld|hawe|yaue',
132
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_filter("/o(.)/", "a$1e")|join("|") }}'
133
        );
134
    }
135
    
136
    /**
137
     * @expectedException Twig_Error_Runtime
138
     */
139
    public function testFilterAssertNoEval()
140
    {
141
        $this->render('{{ "foo"|preg_filter("/o/ei", "strtoupper") }}');
142
    }
143
    
144
    
145
    public function testSplit()
146
    {
147
        $this->assertRender(
148
            'the quick br|n f| jumped |er the lazy d|',
149
            '{{ "the quick brown fox jumped over the lazy dog"|preg_split("/o(\\\\w)/", "a$1e")|join("|") }}'
150
        );
151
    }
152
    
153
    
154
    public function filterProvider()
155
    {
156
        return [
157
            ['preg_quote'],
158
            ['preg_match'],
159
            ['preg_get'],
160
            ['preg_get_all'],
161
            ['preg_grep'],
162
            ['preg_replace'],
163
            ['preg_filter'],
164
            ['preg_split']
165
        ];
166
    }
167
    
168
    /**
169
     * @dataProvider filterProvider
170
     * 
171
     * @param string $filter
172
     */
173
    public function testWithNull($filter)
174
    {
175
        $this->assertRender('-', '{{ null|' . $filter . '("//")|default("-") }}');
176
    }    
177
}
178