Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / twig / twig / test / Twig / Tests / Node / Expression / FilterTest.php
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     public function testConstructor()
15     {
16         $expr = new Twig_Node_Expression_Constant('foo', 1);
17         $name = new Twig_Node_Expression_Constant('upper', 1);
18         $args = new Twig_Node();
19         $node = new Twig_Node_Expression_Filter($expr, $name, $args, 1);
20
21         $this->assertEquals($expr, $node->getNode('node'));
22         $this->assertEquals($name, $node->getNode('filter'));
23         $this->assertEquals($args, $node->getNode('arguments'));
24     }
25
26     public function getTests()
27     {
28         $environment = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
29         $environment->addFilter(new Twig_SimpleFilter('bar', 'bar', array('needs_environment' => true)));
30         $environment->addFilter(new Twig_SimpleFilter('barbar', 'twig_tests_filter_barbar', array('needs_context' => true, 'is_variadic' => true)));
31
32         $tests = array();
33
34         $expr = new Twig_Node_Expression_Constant('foo', 1);
35         $node = $this->createFilter($expr, 'upper');
36         $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)));
37
38         if (function_exists('mb_get_info')) {
39             $tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")');
40         } else {
41             $tests[] = array($node, 'twig_number_format_filter($this->env, strtoupper("foo"), 2, ".", ",")');
42         }
43
44         // named arguments
45         $date = new Twig_Node_Expression_Constant(0, 1);
46         $node = $this->createFilter($date, 'date', array(
47             'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1),
48             'format' => new Twig_Node_Expression_Constant('d/m/Y H:i:s P', 1),
49         ));
50         $tests[] = array($node, 'twig_date_format_filter($this->env, 0, "d/m/Y H:i:s P", "America/Chicago")');
51
52         // skip an optional argument
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         ));
57         $tests[] = array($node, 'twig_date_format_filter($this->env, 0, null, "America/Chicago")');
58
59         // underscores vs camelCase for named arguments
60         $string = new Twig_Node_Expression_Constant('abc', 1);
61         $node = $this->createFilter($string, 'reverse', array(
62             'preserve_keys' => new Twig_Node_Expression_Constant(true, 1),
63         ));
64         $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
65         $node = $this->createFilter($string, 'reverse', array(
66             'preserveKeys' => new Twig_Node_Expression_Constant(true, 1),
67         ));
68         $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
69
70         // filter as an anonymous function
71         if (PHP_VERSION_ID >= 50300) {
72             $node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous');
73             $tests[] = array($node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))');
74         }
75
76         // needs environment
77         $node = $this->createFilter($string, 'bar');
78         $tests[] = array($node, 'bar($this->env, "abc")', $environment);
79
80         $node = $this->createFilter($string, 'bar', array(new Twig_Node_Expression_Constant('bar', 1)));
81         $tests[] = array($node, 'bar($this->env, "abc", "bar")', $environment);
82
83         // arbitrary named arguments
84         $node = $this->createFilter($string, 'barbar');
85         $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc")', $environment);
86
87         $node = $this->createFilter($string, 'barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
88         $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, null, array("foo" => "bar"))', $environment);
89
90         $node = $this->createFilter($string, 'barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
91         $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, "bar")', $environment);
92
93         $node = $this->createFilter($string, 'barbar', array(
94             new Twig_Node_Expression_Constant('1', 1),
95             new Twig_Node_Expression_Constant('2', 1),
96             new Twig_Node_Expression_Constant('3', 1),
97             'foo' => new Twig_Node_Expression_Constant('bar', 1),
98         ));
99         $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
100
101         return $tests;
102     }
103
104     /**
105      * @expectedException        Twig_Error_Syntax
106      * @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)" at line 1.
107      */
108     public function testCompileWithWrongNamedArgumentName()
109     {
110         $date = new Twig_Node_Expression_Constant(0, 1);
111         $node = $this->createFilter($date, 'date', array(
112             'foobar' => new Twig_Node_Expression_Constant('America/Chicago', 1),
113         ));
114
115         $compiler = $this->getCompiler();
116         $compiler->compile($node);
117     }
118
119     /**
120      * @expectedException        Twig_Error_Syntax
121      * @expectedExceptionMessage Value for argument "from" is required for filter "replace" at line 1.
122      */
123     public function testCompileWithMissingNamedArgument()
124     {
125         $value = new Twig_Node_Expression_Constant(0, 1);
126         $node = $this->createFilter($value, 'replace', array(
127             'to' => new Twig_Node_Expression_Constant('foo', 1),
128         ));
129
130         $compiler = $this->getCompiler();
131         $compiler->compile($node);
132     }
133
134     protected function createFilter($node, $name, array $arguments = array())
135     {
136         $name = new Twig_Node_Expression_Constant($name, 1);
137         $arguments = new Twig_Node($arguments);
138
139         return new Twig_Node_Expression_Filter($node, $name, $arguments, 1);
140     }
141
142     protected function getEnvironment()
143     {
144         if (PHP_VERSION_ID >= 50300) {
145             return include 'PHP53/FilterInclude.php';
146         }
147
148         return parent::getEnvironment();
149     }
150 }
151
152 function twig_tests_filter_barbar($context, $string, $arg1 = null, $arg2 = null, array $args = array())
153 {
154 }