4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 class Twig_Tests_ParserTest
extends PHPUnit_Framework_TestCase
14 * @expectedException Twig_Error_Syntax
16 public function testSetMacroThrowsExceptionOnReservedMethods()
18 $parser = $this->getParser();
19 $parser->setMacro('display', $this->getMock('Twig_Node_Macro', array(), array(), '', null));
23 * @expectedException Twig_Error_Syntax
24 * @expectedExceptionMessage Unknown tag name "foo". Did you mean "for" at line 1
26 public function testUnknownTag()
28 $stream = new Twig_TokenStream(array(
29 new Twig_Token(Twig_Token
::BLOCK_START_TYPE
, '', 1),
30 new Twig_Token(Twig_Token
::NAME_TYPE
, 'foo', 1),
31 new Twig_Token(Twig_Token
::BLOCK_END_TYPE
, '', 1),
32 new Twig_Token(Twig_Token
::EOF_TYPE
, '', 1),
34 $parser = new Twig_Parser(new Twig_Environment());
35 $parser->parse($stream);
39 * @dataProvider getFilterBodyNodesData
41 public function testFilterBodyNodes($input, $expected)
43 $parser = $this->getParser();
45 $this->assertEquals($expected, $parser->filterBodyNodes($input));
48 public function getFilterBodyNodesData()
52 new Twig_Node(array(new Twig_Node_Text(' ', 1))),
53 new Twig_Node(array()),
56 $input = new Twig_Node(array(new Twig_Node_Set(false, new Twig_Node(), new Twig_Node(), 1))),
60 $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', 1))))), 1))),
67 * @dataProvider getFilterBodyNodesDataThrowsException
68 * @expectedException Twig_Error_Syntax
70 public function testFilterBodyNodesThrowsException($input)
72 $parser = $this->getParser();
74 $parser->filterBodyNodes($input);
77 public function getFilterBodyNodesDataThrowsException()
80 array(new Twig_Node_Text('foo', 1)),
81 array(new Twig_Node(array(new Twig_Node(array(new Twig_Node_Text('foo', 1)))))),
86 * @expectedException Twig_Error_Syntax
87 * @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 1.
89 public function testFilterBodyNodesWithBOM()
91 $parser = $this->getParser();
92 $parser->filterBodyNodes(new Twig_Node_Text(chr(0xEF).chr(0xBB).chr(0xBF), 1));
95 public function testParseIsReentrant()
97 $twig = new Twig_Environment(null, array(
98 'autoescape' => false,
101 $twig->addTokenParser(new TestTokenParser());
103 $parser = new Twig_Parser($twig);
105 $parser->parse(new Twig_TokenStream(array(
106 new Twig_Token(Twig_Token
::BLOCK_START_TYPE
, '', 1),
107 new Twig_Token(Twig_Token
::NAME_TYPE
, 'test', 1),
108 new Twig_Token(Twig_Token
::BLOCK_END_TYPE
, '', 1),
109 new Twig_Token(Twig_Token
::VAR_START_TYPE
, '', 1),
110 new Twig_Token(Twig_Token
::NAME_TYPE
, 'foo', 1),
111 new Twig_Token(Twig_Token
::VAR_END_TYPE
, '', 1),
112 new Twig_Token(Twig_Token
::EOF_TYPE
, '', 1),
115 $this->assertEquals(null, $parser->getParent());
118 // The getVarName() must not depend on the template loaders,
119 // If this test does not throw any exception, that's good.
120 // see https://github.com/symfony/symfony/issues/4218
121 public function testGetVarName()
123 $twig = new Twig_Environment(null, array(
124 'autoescape' => false,
125 'optimizations' => 0,
128 $twig->parse($twig->tokenize(<<<EOF
129 {% from _self import foo %}
138 protected function getParser()
140 $parser = new TestParser(new Twig_Environment());
141 $parser->setParent(new Twig_Node());
142 $parser->stream
= $this->getMockBuilder('Twig_TokenStream')->disableOriginalConstructor()->getMock();
148 class TestParser
extends Twig_Parser
152 public function filterBodyNodes(Twig_NodeInterface
$node)
154 return parent
::filterBodyNodes($node);
158 class TestTokenParser
extends Twig_TokenParser
160 public function parse(Twig_Token
$token)
162 // simulate the parsing of another template right in the middle of the parsing of the current template
163 $this->parser
->parse(new Twig_TokenStream(array(
164 new Twig_Token(Twig_Token
::BLOCK_START_TYPE
, '', 1),
165 new Twig_Token(Twig_Token
::NAME_TYPE
, 'extends', 1),
166 new Twig_Token(Twig_Token
::STRING_TYPE
, 'base', 1),
167 new Twig_Token(Twig_Token
::BLOCK_END_TYPE
, '', 1),
168 new Twig_Token(Twig_Token
::EOF_TYPE
, '', 1),
171 $this->parser
->getStream()->expect(Twig_Token
::BLOCK_END_TYPE
);
173 return new Twig_Node(array());
176 public function getTag()