ParserTest.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_ParserTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @expectedException Twig_Error_Syntax
  14. */
  15. public function testSetMacroThrowsExceptionOnReservedMethods()
  16. {
  17. $parser = new Twig_Parser(new Twig_Environment());
  18. $parser->setMacro('display', $this->getMock('Twig_Node_Macro', null, array(), '', null));
  19. }
  20. /**
  21. * @dataProvider getFilterBodyNodesData
  22. */
  23. public function testFilterBodyNodes($input, $expected)
  24. {
  25. list($parser, $invoker) = $this->getParserForFilterBodyNodes();
  26. $this->assertEquals($expected, $invoker->invoke($parser, $input));
  27. }
  28. public function getFilterBodyNodesData()
  29. {
  30. return array(
  31. array(
  32. new Twig_Node(array(new Twig_Node_Text(' ', 0))),
  33. new Twig_Node(array()),
  34. ),
  35. array(
  36. $input = new Twig_Node(array(new Twig_Node_Set(false, new Twig_Node(), new Twig_Node(), 0))),
  37. $input,
  38. ),
  39. );
  40. }
  41. /**
  42. * @dataProvider getFilterBodyNodesDataThrowsException
  43. * @expectedException Twig_Error_Syntax
  44. */
  45. public function testFilterBodyNodesThrowsException($input)
  46. {
  47. list($parser, $invoker) = $this->getParserForFilterBodyNodes();
  48. $invoker->invoke($parser, $input);
  49. }
  50. public function getFilterBodyNodesDataThrowsException()
  51. {
  52. return array(
  53. array(new Twig_Node_Text('foo', 0)),
  54. array(new Twig_Node(array(new Twig_Node(array(new Twig_Node_Text('foo', 0)))))),
  55. );
  56. }
  57. protected function getParserForFilterBodyNodes()
  58. {
  59. $invoker = new ReflectionMethod('Twig_Parser', 'filterBodyNodes');
  60. $invoker->setAccessible(true);
  61. $p = new ReflectionProperty('Twig_Parser', 'stream');
  62. $p->setAccessible(true);
  63. $parser = new Twig_Parser(new Twig_Environment());
  64. $parser->setParent(new Twig_Node());
  65. $p->setValue($parser, $this->getMockBuilder('Twig_TokenStream')->disableOriginalConstructor()->getMock());
  66. return array($parser, $invoker);
  67. }
  68. }