FunctionTest.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase
  11. {
  12. /**
  13. * @covers Twig_Node_Expression_Function::__construct
  14. */
  15. public function testConstructor()
  16. {
  17. $name = 'function';
  18. $args = new Twig_Node();
  19. $node = new Twig_Node_Expression_Function($name, $args, 1);
  20. $this->assertEquals($name, $node->getAttribute('name'));
  21. $this->assertEquals($args, $node->getNode('arguments'));
  22. }
  23. /**
  24. * @covers Twig_Node_Expression_Function::compile
  25. * @dataProvider getTests
  26. */
  27. public function testCompile($node, $source, $environment = null)
  28. {
  29. parent::testCompile($node, $source, $environment);
  30. }
  31. /**
  32. * @covers Twig_Node_Expression_Filter::compile
  33. * @expectedException Twig_Error_Syntax
  34. * @expectedExceptionMessage The function "cycl" does not exist. Did you mean "cycle" at line 1
  35. */
  36. public function testUnknownFunction()
  37. {
  38. $node = $this->createFunction('cycl', array());
  39. $node->compile($this->getCompiler());
  40. }
  41. public function getTests()
  42. {
  43. $environment = new Twig_Environment();
  44. $environment->addFunction('foo', new Twig_Function_Function('foo', array()));
  45. $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true)));
  46. $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true)));
  47. $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true)));
  48. $tests = array();
  49. $node = $this->createFunction('foo');
  50. $tests[] = array($node, 'foo()', $environment);
  51. $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1)));
  52. $tests[] = array($node, 'foo("bar", "foobar")', $environment);
  53. $node = $this->createFunction('bar');
  54. $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);
  57. $node = $this->createFunction('foofoo');
  58. $tests[] = array($node, 'foofoo($context)', $environment);
  59. $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 1)));
  60. $tests[] = array($node, 'foofoo($context, "bar")', $environment);
  61. $node = $this->createFunction('foobar');
  62. $tests[] = array($node, 'foobar($this->env, $context)', $environment);
  63. $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 1)));
  64. $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment);
  65. return $tests;
  66. }
  67. protected function createFunction($name, array $arguments = array())
  68. {
  69. return new Twig_Node_Expression_Function($name, new Twig_Node($arguments), 1);
  70. }
  71. }