diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
commit | 4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch) | |
tree | 6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/twig/twig/test/Twig/Tests/Node/Expression/FilterTest.php | |
parent | 2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff) | |
download | wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip |
twig implementation
Diffstat (limited to 'vendor/twig/twig/test/Twig/Tests/Node/Expression/FilterTest.php')
-rw-r--r-- | vendor/twig/twig/test/Twig/Tests/Node/Expression/FilterTest.php | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/vendor/twig/twig/test/Twig/Tests/Node/Expression/FilterTest.php b/vendor/twig/twig/test/Twig/Tests/Node/Expression/FilterTest.php new file mode 100644 index 00000000..8089b9cb --- /dev/null +++ b/vendor/twig/twig/test/Twig/Tests/Node/Expression/FilterTest.php | |||
@@ -0,0 +1,133 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of Twig. | ||
5 | * | ||
6 | * (c) Fabien Potencier | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase | ||
13 | { | ||
14 | /** | ||
15 | * @covers Twig_Node_Expression_Filter::__construct | ||
16 | */ | ||
17 | public function testConstructor() | ||
18 | { | ||
19 | $expr = new Twig_Node_Expression_Constant('foo', 1); | ||
20 | $name = new Twig_Node_Expression_Constant('upper', 1); | ||
21 | $args = new Twig_Node(); | ||
22 | $node = new Twig_Node_Expression_Filter($expr, $name, $args, 1); | ||
23 | |||
24 | $this->assertEquals($expr, $node->getNode('node')); | ||
25 | $this->assertEquals($name, $node->getNode('filter')); | ||
26 | $this->assertEquals($args, $node->getNode('arguments')); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * @covers Twig_Node_Expression_Filter::compile | ||
31 | * @dataProvider getTests | ||
32 | */ | ||
33 | public function testCompile($node, $source, $environment = null) | ||
34 | { | ||
35 | parent::testCompile($node, $source, $environment); | ||
36 | } | ||
37 | |||
38 | public function getTests() | ||
39 | { | ||
40 | $tests = array(); | ||
41 | |||
42 | $expr = new Twig_Node_Expression_Constant('foo', 1); | ||
43 | $node = $this->createFilter($expr, 'upper'); | ||
44 | $node = $this->createFilter($node, 'number_format', array(new Twig_Node_Expression_Constant(2, 1), new Twig_Node_Expression_Constant('.', 1), new Twig_Node_Expression_Constant(',', 1))); | ||
45 | |||
46 | if (function_exists('mb_get_info')) { | ||
47 | $tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")'); | ||
48 | } else { | ||
49 | $tests[] = array($node, 'twig_number_format_filter($this->env, strtoupper("foo"), 2, ".", ",")'); | ||
50 | } | ||
51 | |||
52 | // named arguments | ||
53 | $date = new Twig_Node_Expression_Constant(0, 1); | ||
54 | $node = $this->createFilter($date, 'date', array( | ||
55 | 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1), | ||
56 | 'format' => new Twig_Node_Expression_Constant('d/m/Y H:i:s P', 1), | ||
57 | )); | ||
58 | $tests[] = array($node, 'twig_date_format_filter($this->env, 0, "d/m/Y H:i:s P", "America/Chicago")'); | ||
59 | |||
60 | // skip an optional argument | ||
61 | $date = new Twig_Node_Expression_Constant(0, 1); | ||
62 | $node = $this->createFilter($date, 'date', array( | ||
63 | 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1), | ||
64 | )); | ||
65 | $tests[] = array($node, 'twig_date_format_filter($this->env, 0, null, "America/Chicago")'); | ||
66 | |||
67 | // underscores vs camelCase for named arguments | ||
68 | $string = new Twig_Node_Expression_Constant('abc', 1); | ||
69 | $node = $this->createFilter($string, 'reverse', array( | ||
70 | 'preserve_keys' => new Twig_Node_Expression_Constant(true, 1), | ||
71 | )); | ||
72 | $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)'); | ||
73 | $node = $this->createFilter($string, 'reverse', array( | ||
74 | 'preserveKeys' => new Twig_Node_Expression_Constant(true, 1), | ||
75 | )); | ||
76 | $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)'); | ||
77 | |||
78 | // filter as an anonymous function | ||
79 | if (version_compare(phpversion(), '5.3.0', '>=')) { | ||
80 | $node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous'); | ||
81 | $tests[] = array($node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))'); | ||
82 | } | ||
83 | |||
84 | return $tests; | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * @expectedException Twig_Error_Syntax | ||
89 | * @expectedExceptionMessage Unknown argument "foobar" for filter "date". | ||
90 | */ | ||
91 | public function testCompileWithWrongNamedArgumentName() | ||
92 | { | ||
93 | $date = new Twig_Node_Expression_Constant(0, 1); | ||
94 | $node = $this->createFilter($date, 'date', array( | ||
95 | 'foobar' => new Twig_Node_Expression_Constant('America/Chicago', 1), | ||
96 | )); | ||
97 | |||
98 | $compiler = $this->getCompiler(); | ||
99 | $compiler->compile($node); | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * @expectedException Twig_Error_Syntax | ||
104 | * @expectedExceptionMessage Value for argument "from" is required for filter "replace". | ||
105 | */ | ||
106 | public function testCompileWithMissingNamedArgument() | ||
107 | { | ||
108 | $value = new Twig_Node_Expression_Constant(0, 1); | ||
109 | $node = $this->createFilter($value, 'replace', array( | ||
110 | 'to' => new Twig_Node_Expression_Constant('foo', 1), | ||
111 | )); | ||
112 | |||
113 | $compiler = $this->getCompiler(); | ||
114 | $compiler->compile($node); | ||
115 | } | ||
116 | |||
117 | protected function createFilter($node, $name, array $arguments = array()) | ||
118 | { | ||
119 | $name = new Twig_Node_Expression_Constant($name, 1); | ||
120 | $arguments = new Twig_Node($arguments); | ||
121 | |||
122 | return new Twig_Node_Expression_Filter($node, $name, $arguments, 1); | ||
123 | } | ||
124 | |||
125 | protected function getEnvironment() | ||
126 | { | ||
127 | if (version_compare(phpversion(), '5.3.0', '>=')) { | ||
128 | return include 'PHP53/FilterInclude.php'; | ||
129 | } | ||
130 | |||
131 | return parent::getEnvironment(); | ||
132 | } | ||
133 | } | ||