db backup prior to drupal security update
[yaffs-website] / vendor / symfony / expression-language / Tests / ParserTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ExpressionLanguage\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\ExpressionLanguage\Parser;
16 use Symfony\Component\ExpressionLanguage\Lexer;
17 use Symfony\Component\ExpressionLanguage\Node;
18
19 class ParserTest extends TestCase
20 {
21     /**
22      * @expectedException        \Symfony\Component\ExpressionLanguage\SyntaxError
23      * @expectedExceptionMessage Variable "foo" is not valid around position 1 for expression `foo`.
24      */
25     public function testParseWithInvalidName()
26     {
27         $lexer = new Lexer();
28         $parser = new Parser(array());
29         $parser->parse($lexer->tokenize('foo'));
30     }
31
32     /**
33      * @expectedException        \Symfony\Component\ExpressionLanguage\SyntaxError
34      * @expectedExceptionMessage Variable "foo" is not valid around position 1 for expression `foo`.
35      */
36     public function testParseWithZeroInNames()
37     {
38         $lexer = new Lexer();
39         $parser = new Parser(array());
40         $parser->parse($lexer->tokenize('foo'), array(0));
41     }
42
43     /**
44      * @dataProvider getParseData
45      */
46     public function testParse($node, $expression, $names = array())
47     {
48         $lexer = new Lexer();
49         $parser = new Parser(array());
50         $this->assertEquals($node, $parser->parse($lexer->tokenize($expression), $names));
51     }
52
53     public function getParseData()
54     {
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));
59
60         return array(
61             array(
62                 new Node\NameNode('a'),
63                 'a',
64                 array('a'),
65             ),
66             array(
67                 new Node\ConstantNode('a'),
68                 '"a"',
69             ),
70             array(
71                 new Node\ConstantNode(3),
72                 '3',
73             ),
74             array(
75                 new Node\ConstantNode(false),
76                 'false',
77             ),
78             array(
79                 new Node\ConstantNode(true),
80                 'true',
81             ),
82             array(
83                 new Node\ConstantNode(null),
84                 'null',
85             ),
86             array(
87                 new Node\UnaryNode('-', new Node\ConstantNode(3)),
88                 '-3',
89             ),
90             array(
91                 new Node\BinaryNode('-', new Node\ConstantNode(3), new Node\ConstantNode(3)),
92                 '3 - 3',
93             ),
94             array(
95                 new Node\BinaryNode('*',
96                     new Node\BinaryNode('-', new Node\ConstantNode(3), new Node\ConstantNode(3)),
97                     new Node\ConstantNode(2)
98                 ),
99                 '(3 - 3) * 2',
100             ),
101             array(
102                 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::PROPERTY_CALL),
103                 'foo.bar',
104                 array('foo'),
105             ),
106             array(
107                 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('bar'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
108                 'foo.bar()',
109                 array('foo'),
110             ),
111             array(
112                 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode('not'), new Node\ArgumentsNode(), Node\GetAttrNode::METHOD_CALL),
113                 'foo.not()',
114                 array('foo'),
115             ),
116             array(
117                 new Node\GetAttrNode(
118                     new Node\NameNode('foo'),
119                     new Node\ConstantNode('bar'),
120                     $arguments,
121                     Node\GetAttrNode::METHOD_CALL
122                 ),
123                 'foo.bar("arg1", 2, true)',
124                 array('foo'),
125             ),
126             array(
127                 new Node\GetAttrNode(new Node\NameNode('foo'), new Node\ConstantNode(3), new Node\ArgumentsNode(), Node\GetAttrNode::ARRAY_CALL),
128                 'foo[3]',
129                 array('foo'),
130             ),
131             array(
132                 new Node\ConditionalNode(new Node\ConstantNode(true), new Node\ConstantNode(true), new Node\ConstantNode(false)),
133                 'true ? true : false',
134             ),
135             array(
136                 new Node\BinaryNode('matches', new Node\ConstantNode('foo'), new Node\ConstantNode('/foo/')),
137                 '"foo" matches "/foo/"',
138             ),
139
140             // chained calls
141             array(
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]',
150                 array('foo'),
151             ),
152
153             array(
154                 new Node\NameNode('foo'),
155                 'bar',
156                 array('foo' => 'bar'),
157             ),
158         );
159     }
160
161     private function createGetAttrNode($node, $item, $type)
162     {
163         return new Node\GetAttrNode($node, new Node\ConstantNode($item), new Node\ArgumentsNode(), $type);
164     }
165
166     /**
167      * @dataProvider getInvalidPostfixData
168      * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
169      */
170     public function testParseWithInvalidPostfixData($expr, $names = array())
171     {
172         $lexer = new Lexer();
173         $parser = new Parser(array());
174         $parser->parse($lexer->tokenize($expr), $names);
175     }
176
177     public function getInvalidPostfixData()
178     {
179         return array(
180             array(
181                 'foo."#"',
182                 array('foo'),
183             ),
184             array(
185                 'foo."bar"',
186                 array('foo'),
187             ),
188             array(
189                 'foo.**',
190                 array('foo'),
191             ),
192             array(
193                 'foo.123',
194                 array('foo'),
195             ),
196         );
197     }
198 }