4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase
14 public function testConstructor()
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);
21 $this->assertEquals($expr, $node->getNode('node'));
22 $this->assertEquals($name, $node->getNode('filter'));
23 $this->assertEquals($args, $node->getNode('arguments'));
26 public function getTests()
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)));
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)));
38 if (function_exists('mb_get_info')) {
39 $tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")');
41 $tests[] = array($node, 'twig_number_format_filter($this->env, strtoupper("foo"), 2, ".", ",")');
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),
50 $tests[] = array($node, 'twig_date_format_filter($this->env, 0, "d/m/Y H:i:s P", "America/Chicago")');
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),
57 $tests[] = array($node, 'twig_date_format_filter($this->env, 0, null, "America/Chicago")');
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),
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),
68 $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
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"))');
77 $node = $this->createFilter($string, 'bar');
78 $tests[] = array($node, 'bar($this->env, "abc")', $environment);
80 $node = $this->createFilter($string, 'bar', array(new Twig_Node_Expression_Constant('bar', 1)));
81 $tests[] = array($node, 'bar($this->env, "abc", "bar")', $environment);
83 // arbitrary named arguments
84 $node = $this->createFilter($string, 'barbar');
85 $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc")', $environment);
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);
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);
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),
99 $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
105 * @expectedException Twig_Error_Syntax
106 * @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)" at line 1.
108 public function testCompileWithWrongNamedArgumentName()
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),
115 $compiler = $this->getCompiler();
116 $compiler->compile($node);
120 * @expectedException Twig_Error_Syntax
121 * @expectedExceptionMessage Value for argument "from" is required for filter "replace" at line 1.
123 public function testCompileWithMissingNamedArgument()
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),
130 $compiler = $this->getCompiler();
131 $compiler->compile($node);
134 protected function createFilter($node, $name, array $arguments = array())
136 $name = new Twig_Node_Expression_Constant($name, 1);
137 $arguments = new Twig_Node($arguments);
139 return new Twig_Node_Expression_Filter($node, $name, $arguments, 1);
142 protected function getEnvironment()
144 if (PHP_VERSION_ID >= 50300) {
145 return include 'PHP53/FilterInclude.php';
148 return parent::getEnvironment();
152 function twig_tests_filter_barbar($context, $string, $arg1 = null, $arg2 = null, array $args = array())