1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Twig; |
4
|
|
|
|
5
|
|
|
use Jasny\Twig\PcreExtension; |
6
|
|
|
|
7
|
|
|
class PcreExtensionTest extends PHPUnit_Framework_TestCase { |
8
|
|
|
|
9
|
|
View Code Duplication |
private function buildEnv($template) { |
|
|
|
|
10
|
|
|
$loader = new Twig_Loader_Array(array( |
11
|
|
|
'template' => $template, |
12
|
|
|
)); |
13
|
|
|
$twig = new Twig_Environment($loader); |
14
|
|
|
$twig->addExtension(new PcreExtension()); |
15
|
|
|
return $twig; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
private function process($template, $data = array()) { |
19
|
|
|
$twig = $this->buildEnv($template); |
20
|
|
|
$result = $twig->render('template', $data); |
21
|
|
|
return $result; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function check($expected, $template) { |
25
|
|
|
$result = $this->process($template); |
26
|
|
|
$this->assertEquals($expected, $result); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testQuote() { |
30
|
|
|
$this->check('foo\(\)', '{{ "foo()"|preg_quote }}'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testQuoteDelimiter() { |
34
|
|
|
$this->check('foo\@bar', '{{ "foo@bar"|preg_quote("@") }}'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testPregMatch() { |
38
|
|
|
$this->check('YES', '{% if "foo"|preg_match("/oo/") %}YES{% else %}NO{% endif %}'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testPregMatchNo() { |
42
|
|
|
$this->check('NO', '{% if "fod"|preg_match("/oo/") %}YES{% else %}NO{% endif %}'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @expectedException Twig_Error_Runtime |
47
|
|
|
*/ |
48
|
|
|
public function testPregMatchError() { |
49
|
|
|
$this->check('NO', '{% if "fod"|preg_match("/o//o/") %}YES{% else %}NO{% endif %}'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testPregGet() { |
53
|
|
|
$this->check('d', '{{ "food"|preg_get("/oo(.)/", 1) }}'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testPregGetDefault() { |
57
|
|
|
$this->check('ood', '{{ "food"|preg_get("/oo(.)/") }}'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.