View source
<?php
class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase {
public function testCanOnlyAssignToNames($template) {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize($template, 'index'));
}
public function getFailingTestsForAssignment() {
return array(
array(
'{% set false = "foo" %}',
),
array(
'{% set true = "foo" %}',
),
array(
'{% set none = "foo" %}',
),
array(
'{% set 3 = "foo" %}',
),
array(
'{% set 1 + 2 = "foo" %}',
),
array(
'{% set "bar" = "foo" %}',
),
array(
'{% set %}{% endset %}',
),
);
}
public function testArrayExpression($template, $expected) {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$stream = $env
->tokenize($template, 'index');
$parser = new Twig_Parser($env);
$this
->assertEquals($expected, $parser
->parse($stream)
->getNode('body')
->getNode(0)
->getNode('expr'));
}
public function testArraySyntaxError($template) {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize($template, 'index'));
}
public function getFailingTestsForArray() {
return array(
array(
'{{ [1, "a": "b"] }}',
),
array(
'{{ {"a": "b", 2} }}',
),
);
}
public function getTestsForArray() {
return array(
array(
'{{ [1, 2] }}',
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant(0, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(2, 1),
), 1),
),
array(
'{{ [1, 2, ] }}',
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant(0, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(2, 1),
), 1),
),
array(
'{{ {"a": "b", "b": "c"} }}',
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant('a', 1),
new Twig_Node_Expression_Constant('b', 1),
new Twig_Node_Expression_Constant('b', 1),
new Twig_Node_Expression_Constant('c', 1),
), 1),
),
array(
'{{ {"a": "b", "b": "c", } }}',
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant('a', 1),
new Twig_Node_Expression_Constant('b', 1),
new Twig_Node_Expression_Constant('b', 1),
new Twig_Node_Expression_Constant('c', 1),
), 1),
),
array(
'{{ [1, {"a": "b", "b": "c"}] }}',
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant(0, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant('a', 1),
new Twig_Node_Expression_Constant('b', 1),
new Twig_Node_Expression_Constant('b', 1),
new Twig_Node_Expression_Constant('c', 1),
), 1),
), 1),
),
array(
'{{ {"a": [1, 2], "b": "c"} }}',
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant('a', 1),
new Twig_Node_Expression_Array(array(
new Twig_Node_Expression_Constant(0, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(1, 1),
new Twig_Node_Expression_Constant(2, 1),
), 1),
new Twig_Node_Expression_Constant('b', 1),
new Twig_Node_Expression_Constant('c', 1),
), 1),
),
);
}
public function testStringExpressionDoesNotConcatenateTwoConsecutiveStrings() {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
'optimizations' => 0,
));
$stream = $env
->tokenize('{{ "a" "b" }}', 'index');
$parser = new Twig_Parser($env);
$parser
->parse($stream);
}
public function testStringExpression($template, $expected) {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
'optimizations' => 0,
));
$stream = $env
->tokenize($template, 'index');
$parser = new Twig_Parser($env);
$this
->assertEquals($expected, $parser
->parse($stream)
->getNode('body')
->getNode(0)
->getNode('expr'));
}
public function getTestsForString() {
return array(
array(
'{{ "foo" }}',
new Twig_Node_Expression_Constant('foo', 1),
),
array(
'{{ "foo #{bar}" }}',
new Twig_Node_Expression_Binary_Concat(new Twig_Node_Expression_Constant('foo ', 1), new Twig_Node_Expression_Name('bar', 1), 1),
),
array(
'{{ "foo #{bar} baz" }}',
new Twig_Node_Expression_Binary_Concat(new Twig_Node_Expression_Binary_Concat(new Twig_Node_Expression_Constant('foo ', 1), new Twig_Node_Expression_Name('bar', 1), 1), new Twig_Node_Expression_Constant(' baz', 1), 1),
),
array(
'{{ "foo #{"foo #{bar} baz"} baz" }}',
new Twig_Node_Expression_Binary_Concat(new Twig_Node_Expression_Binary_Concat(new Twig_Node_Expression_Constant('foo ', 1), new Twig_Node_Expression_Binary_Concat(new Twig_Node_Expression_Binary_Concat(new Twig_Node_Expression_Constant('foo ', 1), new Twig_Node_Expression_Name('bar', 1), 1), new Twig_Node_Expression_Constant(' baz', 1), 1), 1), new Twig_Node_Expression_Constant(' baz', 1), 1),
),
);
}
public function testAttributeCallDoesNotSupportNamedArguments() {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize('{{ foo.bar(name="Foo") }}', 'index'));
}
public function testMacroCallDoesNotSupportNamedArguments() {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize('{% from _self import foo %}{% macro foo() %}{% endmacro %}{{ foo(name="Foo") }}', 'index'));
}
public function testMacroDefinitionDoesNotSupportNonNameVariableName() {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize('{% macro foo("a") %}{% endmacro %}', 'index'));
}
public function testMacroDefinitionDoesNotSupportNonConstantDefaultValues($template) {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize($template, 'index'));
}
public function getMacroDefinitionDoesNotSupportNonConstantDefaultValues() {
return array(
array(
'{% macro foo(name = "a #{foo} a") %}{% endmacro %}',
),
array(
'{% macro foo(name = [["b", "a #{foo} a"]]) %}{% endmacro %}',
),
);
}
public function testMacroDefinitionSupportsConstantDefaultValues($template) {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize($template, 'index'));
}
public function getMacroDefinitionSupportsConstantDefaultValues() {
return array(
array(
'{% macro foo(name = "aa") %}{% endmacro %}',
),
array(
'{% macro foo(name = 12) %}{% endmacro %}',
),
array(
'{% macro foo(name = true) %}{% endmacro %}',
),
array(
'{% macro foo(name = ["a"]) %}{% endmacro %}',
),
array(
'{% macro foo(name = [["a"]]) %}{% endmacro %}',
),
array(
'{% macro foo(name = {a: "a"}) %}{% endmacro %}',
),
array(
'{% macro foo(name = {a: {b: "a"}}) %}{% endmacro %}',
),
);
}
public function testUnknownFunction() {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize('{{ cycl() }}', 'index'));
}
public function testUnknownFilter() {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize('{{ 1|lowe }}', 'index'));
}
public function testUnknownTest() {
$env = new Twig_Environment($this
->getMock('Twig_LoaderInterface'), array(
'cache' => false,
'autoescape' => false,
));
$parser = new Twig_Parser($env);
$parser
->parse($env
->tokenize('{{ 1 is nul }}', 'index'));
}
}