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_FunctionTest
extends Twig_Test_NodeTestCase
15 * @covers Twig_Node_Expression_Function::__construct
17 public function testConstructor()
20 $args = new Twig_Node();
21 $node = new Twig_Node_Expression_Function($name, $args, 1);
23 $this->assertEquals($name, $node->getAttribute('name'));
24 $this->assertEquals($args, $node->getNode('arguments'));
28 * @covers Twig_Node_Expression_Function::compile
29 * @dataProvider getTests
31 public function testCompile($node, $source, $environment = null)
33 parent
::testCompile($node, $source, $environment);
36 public function getTests()
38 $environment = new Twig_Environment();
39 $environment->addFunction('foo', new Twig_Function_Function('foo', array()));
40 $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true)));
41 $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true)));
42 $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true)));
46 $node = $this->createFunction('foo');
47 $tests[] = array($node, 'foo()', $environment);
49 $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1)));
50 $tests[] = array($node, 'foo("bar", "foobar")', $environment);
52 $node = $this->createFunction('bar');
53 $tests[] = array($node, 'bar($this->env)', $environment);
55 $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 1)));
56 $tests[] = array($node, 'bar($this->env, "bar")', $environment);
58 $node = $this->createFunction('foofoo');
59 $tests[] = array($node, 'foofoo($context)', $environment);
61 $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 1)));
62 $tests[] = array($node, 'foofoo($context, "bar")', $environment);
64 $node = $this->createFunction('foobar');
65 $tests[] = array($node, 'foobar($this->env, $context)', $environment);
67 $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 1)));
68 $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment);
71 $node = $this->createFunction('date', array(
72 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1),
73 'date' => new Twig_Node_Expression_Constant(0, 1),
75 $tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")');
77 // function as an anonymous function
78 if (version_compare(phpversion(), '5.3.0', '>=')) {
79 $node = $this->createFunction('anonymous', array(new Twig_Node_Expression_Constant('foo', 1)));
80 $tests[] = array($node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), array("foo"))');
86 protected function createFunction($name, array $arguments = array())
88 return new Twig_Node_Expression_Function($name, new Twig_Node($arguments), 1);
91 protected function getEnvironment()
93 if (version_compare(phpversion(), '5.3.0', '>=')) {
94 return include 'PHP53/FunctionInclude.php';
97 return parent
::getEnvironment();