FunctionTest.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public function testUnknownFunction()
  33. {
  34. $node = $this->createFunction('unknown', array());
  35. try {
  36. $node->compile($this->getCompiler());
  37. $this->fail();
  38. } catch (Exception $e) {
  39. $this->assertEquals('Twig_Error_Syntax', get_class($e));
  40. }
  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. $arguments = new Twig_Node($arguments);
  71. return new Twig_Node_Expression_Function($name, $arguments, 0);
  72. }
  73. }