Blame view

vendor/jasny/twig-extensions/tests/PcreExtensionTest.php 4.36 KB
c4650843   Etienne Pallier   Ajout du dossier ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php

namespace Jasny\Twig;

use Jasny\Twig\PcreExtension;
use Jasny\Twig\TestHelper;

/**
 * @covers Jasny\Twig\PcreExtension
 */
class PcreExtensionTest extends \PHPUnit_Framework_TestCase
{
    use TestHelper;
    
    protected function getExtension()
    {
        return new PcreExtension();
    }
    
    
    public function testGetName()
    {
        $this->assertEquals('jasny/pcre', $this->getExtension()->getName());
    }
    

    public function testQuote()
    {
        $this->assertRender('foo\(\)', '{{ "foo()"|preg_quote }}');
    }
    
    public function testQuoteDelimiter()
    {
        $this->assertRender('foo\@bar', '{{ "foo@bar"|preg_quote("@") }}');
    }
    
    public function testPregMatch()
    {
        $this->assertRender('YES', '{% if "foo"|preg_match("/oo/") %}YES{% else %}NO{% endif %}');
    }

    public function testPregMatchNo()
    {
        $this->assertRender('NO', '{% if "fod"|preg_match("/oo/") %}YES{% else %}NO{% endif %}');
    }

    /**
     * @expectedException \Twig_Error_Runtime
     */
    public function testPregMatchError()
    {
        $this->render('{% if "fod"|preg_match("/o//o/") %}YES{% else %}NO{% endif %}');
    }
    
    
    public function testPregGet()
    {
        $this->assertRender('d', '{{ "food"|preg_get("/oo(.)/", 1) }}');
    }
    
    public function testPregGetDefault()
    {
        $this->assertRender('ood', '{{ "food"|preg_get("/oo(.)/") }}');
    }
    
    
    public function testPregGetAll()
    {
        $this->assertRender('d|t|m', '{{ "food woot should doom"|preg_get_all("/oo(.)/", 1)|join("|") }}');
    }
    
    public function testPregGetAllDefault()
    {
        $this->assertRender('ood|oot|oom', '{{ "food woot doom"|preg_get_all("/oo(.)/")|join("|") }}');
    }
    
    
    public function testPregGrep()
    {
        $this->assertRender(
            'world|how|you',
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_grep("/o./")|join("|") }}'
        );
    }
    
    public function testPregGrepInvert()
    {
        $this->assertRender(
            'hello|sweet|are',
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_grep("/o./", "invert")|join("|") }}'
        );
    }
    
    
    public function testReplace()
    {
        $this->assertRender(
            'the quick brawen faxe jumped aveer the lazy dage',
            '{{ "the quick brown fox jumped over the lazy dog"|preg_replace("/o(\\\\w)/", "a$1e") }}'
        );
    }
    
    public function testReplaceLimit()
    {
        $this->assertRender(
            'the quick brawen faxe jumped over the lazy dog',
            '{{ "the quick brown fox jumped over the lazy dog"|preg_replace("/o(\\\\w)/", "a$1e", 2) }}'
        );
    }
    
    public function testReplaceWithArray()
    {
        $this->assertRender(
            'hello|sweet|wareld|hawe|are|yaue',
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_replace("/o(.)/", "a$1e")|join("|") }}'
        );
    }
    
    /**
     * @expectedException Twig_Error_Runtime
     */
    public function testReplaceAssertNoEval()
    {
        $this->render('{{ "foo"|preg_replace("/o/ei", "strtoupper") }}');
    }
    
    
    public function testFilter()
    {
        $this->assertRender(
            'wareld|hawe|yaue',
            '{{ ["hello", "sweet", "world", "how", "are", "you"]|preg_filter("/o(.)/", "a$1e")|join("|") }}'
        );
    }
    
    /**
     * @expectedException Twig_Error_Runtime
     */
    public function testFilterAssertNoEval()
    {
        $this->render('{{ "foo"|preg_filter("/o/ei", "strtoupper") }}');
    }
    
    
    public function testSplit()
    {
        $this->assertRender(
            'the quick br|n f| jumped |er the lazy d|',
            '{{ "the quick brown fox jumped over the lazy dog"|preg_split("/o(\\\\w)/", "a$1e")|join("|") }}'
        );
    }
    
    
    public function filterProvider()
    {
        return [
            ['preg_quote'],
            ['preg_match'],
            ['preg_get'],
            ['preg_get_all'],
            ['preg_grep'],
            ['preg_replace'],
            ['preg_filter'],
            ['preg_split']
        ];
    }
    
    /**
     * @dataProvider filterProvider
     * 
     * @param string $filter
     */
    public function testWithNull($filter)
    {
        $this->assertRender('-', '{{ null|' . $filter . '("//")|default("-") }}');
    }    
}