ParserTest.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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', array(), array(), '', null));
  19. }
  20. /**
  21. * @dataProvider getFilterBodyNodesData
  22. */
  23. public function testFilterBodyNodes($input, $expected)
  24. {
  25. $parser = $this->getParserForFilterBodyNodes();
  26. $this->assertEquals($expected, $parser->filterBodyNodes($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. array(
  40. $input = new Twig_Node(array(new Twig_Node_Set(true, new Twig_Node(), new Twig_Node(array(new Twig_Node(array(new Twig_Node_Text('foo', 0))))), 0))),
  41. $input,
  42. ),
  43. );
  44. }
  45. /**
  46. * @dataProvider getFilterBodyNodesDataThrowsException
  47. * @expectedException Twig_Error_Syntax
  48. */
  49. public function testFilterBodyNodesThrowsException($input)
  50. {
  51. $parser = $this->getParserForFilterBodyNodes();
  52. $parser->filterBodyNodes($input);
  53. }
  54. public function getFilterBodyNodesDataThrowsException()
  55. {
  56. return array(
  57. array(new Twig_Node_Text('foo', 0)),
  58. array(new Twig_Node(array(new Twig_Node(array(new Twig_Node_Text('foo', 0)))))),
  59. );
  60. }
  61. /**
  62. * @expectedException Twig_Error_Syntax
  63. * @expectedExceptionMessage A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed at line 0.
  64. */
  65. public function testFilterBodyNodesWithBOM()
  66. {
  67. $parser = $this->getParserForFilterBodyNodes();
  68. $parser->filterBodyNodes(new Twig_Node_Text(chr(0xEF).chr(0xBB).chr(0xBF), 0));
  69. }
  70. protected function getParserForFilterBodyNodes()
  71. {
  72. $parser = new TestParser(new Twig_Environment());
  73. $parser->setParent(new Twig_Node());
  74. $parser->stream = $this->getMockBuilder('Twig_TokenStream')->disableOriginalConstructor()->getMock();
  75. return $parser;
  76. }
  77. }
  78. class TestParser extends Twig_Parser
  79. {
  80. public $stream;
  81. public function filterBodyNodes(Twig_NodeInterface $node)
  82. {
  83. return parent::filterBodyNodes($node);
  84. }
  85. }