From 46eb49e7e5dd6653caf51ecd57a5134ff2b14e2b Mon Sep 17 00:00:00 2001 From: Martin Hason Date: Wed, 2 Feb 2011 12:46:49 +0100 Subject: [PATCH] fixed functions --- lib/Twig/Node/Expression/Function.php | 10 ++- test/Twig/Tests/Node/Expression/FunctionTest.php | 92 ++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 test/Twig/Tests/Node/Expression/FunctionTest.php diff --git a/lib/Twig/Node/Expression/Function.php b/lib/Twig/Node/Expression/Function.php index 81d243b..ffa0515 100644 --- a/lib/Twig/Node/Expression/Function.php +++ b/lib/Twig/Node/Expression/Function.php @@ -24,15 +24,21 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression $compiler ->raw($function->compile().'(') - ->raw($function->needsEnvironment() ? '$this->env, ' : '') - ->raw($function->needsContext() ? '$context, ' : '') + ->raw($function->needsEnvironment() ? '$this->env' : '') ; + if ($function->needsContext()) { + $compiler->raw($function->needsEnvironment() ? ', $context' : '$context'); + } + $first = true; foreach ($this->getNode('arguments') as $node) { if (!$first) { $compiler->raw(', '); } else { + if ($function->needsEnvironment() || $function->needsContext()) { + $compiler->raw(', '); + } $first = false; } $compiler->subcompile($node); diff --git a/test/Twig/Tests/Node/Expression/FunctionTest.php b/test/Twig/Tests/Node/Expression/FunctionTest.php new file mode 100644 index 0000000..69f5225 --- /dev/null +++ b/test/Twig/Tests/Node/Expression/FunctionTest.php @@ -0,0 +1,92 @@ +assertEquals($name, $node->getNode('name')); + $this->assertEquals($args, $node->getNode('arguments')); + } + + /** + * @covers Twig_Node_Expression_Function::compile + * @dataProvider getTests + */ + public function testCompile($node, $source, $environment = null) + { + parent::testCompile($node, $source, $environment); + } + + public function testUnknownFunction() + { + $node = $this->createFunction('unknown', array()); + try { + $node->compile($this->getCompiler()); + $this->fail(); + } catch (Exception $e) { + $this->assertEquals('Twig_Error_Syntax', get_class($e)); + } + } + + public function getTests() + { + $environment = new Twig_Environment(); + $environment->addFunction('foo', new Twig_Function_Function('foo', array())); + $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true))); + $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true))); + $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true))); + + $tests = array(); + + $node = $this->createFunction('foo'); + $tests[] = array($node, 'foo()', $environment); + + $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 0), new Twig_Node_Expression_Constant('foobar', 0))); + $tests[] = array($node, 'foo("bar", "foobar")', $environment); + + $node = $this->createFunction('bar'); + $tests[] = array($node, 'bar($this->env)', $environment); + + $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 0))); + $tests[] = array($node, 'bar($this->env, "bar")', $environment); + + $node = $this->createFunction('foofoo'); + $tests[] = array($node, 'foofoo($context)', $environment); + + $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 0))); + $tests[] = array($node, 'foofoo($context, "bar")', $environment); + + $node = $this->createFunction('foobar'); + $tests[] = array($node, 'foobar($this->env, $context)', $environment); + + $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 0))); + $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment); + + return $tests; + } + + protected function createFunction($name, array $arguments = array()) + { + $name = new Twig_Node_Expression_Name($name, 0); + $arguments = new Twig_Node($arguments); + return new Twig_Node_Expression_Function($name, $arguments, 0); + } +} -- 1.7.2.5