FunctionTest.php 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. require_once dirname(__FILE__).'/../TestCase.php';
  11. class Twig_Tests_Node_Expression_FunctionTest extends Twig_Tests_Node_TestCase
  12. {
  13. /**
  14. * @covers Twig_Node_Expression_Function::__construct
  15. */
  16. public function testConstructor()
  17. {
  18. $name = 'function';
  19. $args = new Twig_Node();
  20. $node = new Twig_Node_Expression_Function($name, $args, 0);
  21. $this->assertEquals($name, $node->getAttribute('name'));
  22. $this->assertEquals($args, $node->getNode('arguments'));
  23. }
  24. /**
  25. * @covers Twig_Node_Expression_Function::compile
  26. * @dataProvider getTests
  27. */
  28. public function testCompile($node, $source, $environment = null)
  29. {
  30. parent::testCompile($node, $source, $environment);
  31. }
  32. /**
  33. * @covers Twig_Node_Expression_Filter::compile
  34. * @expectedException Twig_Error_Syntax
  35. * @expectedExceptionMessage The function "cycl" does not exist. Did you mean "cycle" at line 0
  36. */
  37. public function testUnknownFunction()
  38. {
  39. $node = $this->createFunction('cycl', array());
  40. $node->compile($this->getCompiler());
  41. }
  42. public function getTests()
  43. {
  44. $environment = new Twig_Environment();
  45. $environment->addFunction('foo', new Twig_Function_Function('foo', array()));
  46. $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true)));
  47. $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true)));
  48. $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true)));
  49. $tests = array();
  50. $node = $this->createFunction('foo');
  51. $tests[] = array($node, 'foo()', $environment);
  52. $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 0), new Twig_Node_Expression_Constant('foobar', 0)));
  53. $tests[] = array($node, 'foo("bar", "foobar")', $environment);
  54. $node = $this->createFunction('bar');
  55. $tests[] = array($node, 'bar($this->env)', $environment);
  56. $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 0)));
  57. $tests[] = array($node, 'bar($this->env, "bar")', $environment);
  58. $node = $this->createFunction('foofoo');
  59. $tests[] = array($node, 'foofoo($context)', $environment);
  60. $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 0)));
  61. $tests[] = array($node, 'foofoo($context, "bar")', $environment);
  62. $node = $this->createFunction('foobar');
  63. $tests[] = array($node, 'foobar($this->env, $context)', $environment);
  64. $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 0)));
  65. $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment);
  66. return $tests;
  67. }
  68. protected function createFunction($name, array $arguments = array())
  69. {
  70. return new Twig_Node_Expression_Function($name, new Twig_Node($arguments), 0);
  71. }
  72. }