Yaffs site version 1.1
[yaffs-website] / vendor / symfony / expression-language / Tests / ExpressionLanguageTest.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 Symfony\Component\ExpressionLanguage\ExpressionFunction;
15 use PHPUnit\Framework\TestCase;
16 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
17 use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
18
19 class ExpressionLanguageTest extends TestCase
20 {
21     public function testCachedParse()
22     {
23         $cacheMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
24         $savedParsedExpression = null;
25         $expressionLanguage = new ExpressionLanguage($cacheMock);
26
27         $cacheMock
28             ->expects($this->exactly(2))
29             ->method('fetch')
30             ->with('1 + 1//')
31             ->will($this->returnCallback(function () use (&$savedParsedExpression) {
32                 return $savedParsedExpression;
33             }))
34         ;
35         $cacheMock
36             ->expects($this->exactly(1))
37             ->method('save')
38             ->with('1 + 1//', $this->isInstanceOf('Symfony\Component\ExpressionLanguage\ParsedExpression'))
39             ->will($this->returnCallback(function ($key, $expression) use (&$savedParsedExpression) {
40                 $savedParsedExpression = $expression;
41             }))
42         ;
43
44         $parsedExpression = $expressionLanguage->parse('1 + 1', array());
45         $this->assertSame($savedParsedExpression, $parsedExpression);
46
47         $parsedExpression = $expressionLanguage->parse('1 + 1', array());
48         $this->assertSame($savedParsedExpression, $parsedExpression);
49     }
50
51     public function testConstantFunction()
52     {
53         $expressionLanguage = new ExpressionLanguage();
54         $this->assertEquals(PHP_VERSION, $expressionLanguage->evaluate('constant("PHP_VERSION")'));
55
56         $expressionLanguage = new ExpressionLanguage();
57         $this->assertEquals('constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
58     }
59
60     public function testProviders()
61     {
62         $expressionLanguage = new ExpressionLanguage(null, array(new TestProvider()));
63         $this->assertEquals('foo', $expressionLanguage->evaluate('identity("foo")'));
64         $this->assertEquals('"foo"', $expressionLanguage->compile('identity("foo")'));
65     }
66
67     /**
68      * @dataProvider shortCircuitProviderEvaluate
69      */
70     public function testShortCircuitOperatorsEvaluate($expression, array $values, $expected)
71     {
72         $expressionLanguage = new ExpressionLanguage();
73         $this->assertEquals($expected, $expressionLanguage->evaluate($expression, $values));
74     }
75
76     /**
77      * @dataProvider shortCircuitProviderCompile
78      */
79     public function testShortCircuitOperatorsCompile($expression, array $names, $expected)
80     {
81         $result = null;
82         $expressionLanguage = new ExpressionLanguage();
83         eval(sprintf('$result = %s;', $expressionLanguage->compile($expression, $names)));
84         $this->assertSame($expected, $result);
85     }
86
87     public function shortCircuitProviderEvaluate()
88     {
89         $object = $this->getMockBuilder('stdClass')->setMethods(array('foo'))->getMock();
90         $object->expects($this->never())->method('foo');
91
92         return array(
93             array('false and object.foo()', array('object' => $object), false),
94             array('false && object.foo()', array('object' => $object), false),
95             array('true || object.foo()', array('object' => $object), true),
96             array('true or object.foo()', array('object' => $object), true),
97         );
98     }
99
100     public function shortCircuitProviderCompile()
101     {
102         return array(
103             array('false and foo', array('foo' => 'foo'), false),
104             array('false && foo', array('foo' => 'foo'), false),
105             array('true || foo', array('foo' => 'foo'), true),
106             array('true or foo', array('foo' => 'foo'), true),
107         );
108     }
109
110     public function testCachingForOverriddenVariableNames()
111     {
112         $expressionLanguage = new ExpressionLanguage();
113         $expression = 'a + b';
114         $expressionLanguage->evaluate($expression, array('a' => 1, 'b' => 1));
115         $result = $expressionLanguage->compile($expression, array('a', 'B' => 'b'));
116         $this->assertSame('($a + $B)', $result);
117     }
118
119     public function testCachingWithDifferentNamesOrder()
120     {
121         $cacheMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
122         $expressionLanguage = new ExpressionLanguage($cacheMock);
123         $savedParsedExpressions = array();
124         $cacheMock
125             ->expects($this->exactly(2))
126             ->method('fetch')
127             ->will($this->returnCallback(function ($key) use (&$savedParsedExpressions) {
128                 return isset($savedParsedExpressions[$key]) ? $savedParsedExpressions[$key] : null;
129             }))
130         ;
131         $cacheMock
132             ->expects($this->exactly(1))
133             ->method('save')
134             ->will($this->returnCallback(function ($key, $expression) use (&$savedParsedExpressions) {
135                 $savedParsedExpressions[$key] = $expression;
136             }))
137         ;
138
139         $expression = 'a + b';
140         $expressionLanguage->compile($expression, array('a', 'B' => 'b'));
141         $expressionLanguage->compile($expression, array('B' => 'b', 'a'));
142     }
143
144     /**
145      * @dataProvider getRegisterCallbacks
146      * @expectedException \LogicException
147      */
148     public function testRegisterAfterParse($registerCallback)
149     {
150         $el = new ExpressionLanguage();
151         $el->parse('1 + 1', array());
152         $registerCallback($el);
153     }
154
155     /**
156      * @dataProvider getRegisterCallbacks
157      * @expectedException \LogicException
158      */
159     public function testRegisterAfterEval($registerCallback)
160     {
161         $el = new ExpressionLanguage();
162         $el->evaluate('1 + 1');
163         $registerCallback($el);
164     }
165
166     /**
167      * @dataProvider getRegisterCallbacks
168      * @expectedException \LogicException
169      */
170     public function testRegisterAfterCompile($registerCallback)
171     {
172         $el = new ExpressionLanguage();
173         $el->compile('1 + 1');
174         $registerCallback($el);
175     }
176
177     public function getRegisterCallbacks()
178     {
179         return array(
180             array(
181                 function (ExpressionLanguage $el) {
182                     $el->register('fn', function () {}, function () {});
183                 },
184             ),
185             array(
186                 function (ExpressionLanguage $el) {
187                     $el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
188                 },
189             ),
190             array(
191                 function (ExpressionLanguage $el) {
192                     $el->registerProvider(new TestProvider());
193                 },
194             ),
195         );
196     }
197 }