4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ExpressionLanguage\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\ExpressionLanguage\Parser;
16 use Symfony\Component\ExpressionLanguage\Lexer;
17 use Symfony\Component\ExpressionLanguage\Node;
19 class ParserTest extends TestCase
22 * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
23 * @expectedExceptionMessage Variable "foo" is not valid around position 1 for expression `foo`.
25 public function testParseWithInvalidName()
28 $parser = new Parser(array());
29 $parser->parse($lexer->tokenize('foo'));
33 * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
34 * @expectedExceptionMessage Variable "foo" is not valid around position 1 for expression `foo`.
36 public function testParseWithZeroInNames()
39 $parser = new Parser(array());
40 $parser->parse($lexer->tokenize('foo'), array(0));
44 * @dataProvider getParseData
46 public function testParse($node, $expression, $names = array())
49 $parser = new Parser(array());
50 $this->assertEquals($node, $parser->parse($lexer->tokenize($expression), $names));
53 public function getParseData()
55 $arguments = new Node\ArgumentsNode();
56 $arguments->addElement(new Node\ConstantNode('arg1'));
57 $arguments->addElement(new Node\ConstantNode(2));
58 $arguments->addElement(new Node\ConstantNode(true));
62 new Node\NameNode('a'),
67 new Node\ConstantNode('a'),
71 new Node\ConstantNode(3),
75 new Node\ConstantNode(false),
79 new Node\ConstantNode(true),
83 new Node\ConstantNode(null),
87 new Node\UnaryNode('-', new Node\ConstantNode(3)),
91 new Node\BinaryNode('-', new Node\ConstantNode(3), new Node\ConstantNode(3)),
95 new Node\BinaryNode('*',
96 new Node\BinaryNode('-', new Node\ConstantNode(3), new Node\ConstantNode(3)),
97 new Node\ConstantNode(2)
102 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::PROPERTY_CALL),
107 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
112 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('not'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
117 new Node\GetAttrNode(
118 new Node\NameNode('foo'),
119 new Node\ConstantNode('bar'),
121 Node\GetAttrNode::METHOD_CALL
123 'foo.bar("arg1", 2, true)',
127 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode(3), new Node\ArgumentsNode(), Node\GetAttrNode::ARRAY_CALL),
132 new Node\ConditionalNode(new Node\ConstantNode(true), new Node\ConstantNode(true), new Node\ConstantNode(false)),
133 'true ? true : false',
136 new Node\BinaryNode('matches', new Node\ConstantNode('foo'), new Node\ConstantNode('/foo/')),
137 '"foo" matches "/foo/"',
142 $this->createGetAttrNode(
143 $this->createGetAttrNode(
144 $this->createGetAttrNode(
145 $this->createGetAttrNode(new Node\NameNode('foo'), 'bar', Node\GetAttrNode::METHOD_CALL),
146 'foo', Node\GetAttrNode::METHOD_CALL),
147 'baz', Node\GetAttrNode::PROPERTY_CALL),
148 '3', Node\GetAttrNode::ARRAY_CALL),
149 'foo.bar().foo().baz[3]',
154 new Node\NameNode('foo'),
156 array('foo' => 'bar'),
161 private function createGetAttrNode($node, $item, $type)
163 return new Node\GetAttrNode($node, new Node\ConstantNode($item), new Node\ArgumentsNode(), $type);
167 * @dataProvider getInvalidPostfixData
168 * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
170 public function testParseWithInvalidPostfixData($expr, $names = array())
172 $lexer = new Lexer();
173 $parser = new Parser(array());
174 $parser->parse($lexer->tokenize($expr), $names);
177 public function getInvalidPostfixData()