123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\ExpressionLanguage\Tests;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\ExpressionLanguage\ExpressionFunction;
- use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
- use Symfony\Component\ExpressionLanguage\ParsedExpression;
- use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
- class ExpressionLanguageTest extends TestCase
- {
- public function testCachedParse()
- {
- $cacheMock = $this->getMockBuilder('Psr\Cache\CacheItemPoolInterface')->getMock();
- $cacheItemMock = $this->getMockBuilder('Psr\Cache\CacheItemInterface')->getMock();
- $savedParsedExpression = null;
- $expressionLanguage = new ExpressionLanguage($cacheMock);
- $cacheMock
- ->expects($this->exactly(2))
- ->method('getItem')
- ->with('1%20%2B%201%2F%2F')
- ->willReturn($cacheItemMock)
- ;
- $cacheItemMock
- ->expects($this->exactly(2))
- ->method('get')
- ->will($this->returnCallback(function () use (&$savedParsedExpression) {
- return $savedParsedExpression;
- }))
- ;
- $cacheItemMock
- ->expects($this->exactly(1))
- ->method('set')
- ->with($this->isInstanceOf(ParsedExpression::class))
- ->will($this->returnCallback(function ($parsedExpression) use (&$savedParsedExpression) {
- $savedParsedExpression = $parsedExpression;
- }))
- ;
- $cacheMock
- ->expects($this->exactly(1))
- ->method('save')
- ->with($cacheItemMock)
- ;
- $parsedExpression = $expressionLanguage->parse('1 + 1', []);
- $this->assertSame($savedParsedExpression, $parsedExpression);
- $parsedExpression = $expressionLanguage->parse('1 + 1', []);
- $this->assertSame($savedParsedExpression, $parsedExpression);
- }
- /**
- * @group legacy
- */
- public function testCachedParseWithDeprecatedParserCacheInterface()
- {
- $cacheMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
- $cacheItemMock = $this->getMockBuilder('Psr\Cache\CacheItemInterface')->getMock();
- $savedParsedExpression = null;
- $expressionLanguage = new ExpressionLanguage($cacheMock);
- $cacheMock
- ->expects($this->exactly(1))
- ->method('fetch')
- ->with('1%20%2B%201%2F%2F')
- ->willReturn($savedParsedExpression)
- ;
- $cacheMock
- ->expects($this->exactly(1))
- ->method('save')
- ->with('1%20%2B%201%2F%2F', $this->isInstanceOf(ParsedExpression::class))
- ->will($this->returnCallback(function ($key, $expression) use (&$savedParsedExpression) {
- $savedParsedExpression = $expression;
- }))
- ;
- $parsedExpression = $expressionLanguage->parse('1 + 1', []);
- $this->assertSame($savedParsedExpression, $parsedExpression);
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Cache argument has to implement Psr\Cache\CacheItemPoolInterface.
- */
- public function testWrongCacheImplementation()
- {
- $cacheMock = $this->getMockBuilder('Psr\Cache\CacheItemSpoolInterface')->getMock();
- $expressionLanguage = new ExpressionLanguage($cacheMock);
- }
- public function testConstantFunction()
- {
- $expressionLanguage = new ExpressionLanguage();
- $this->assertEquals(PHP_VERSION, $expressionLanguage->evaluate('constant("PHP_VERSION")'));
- $expressionLanguage = new ExpressionLanguage();
- $this->assertEquals('\constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
- }
- public function testProviders()
- {
- $expressionLanguage = new ExpressionLanguage(null, [new TestProvider()]);
- $this->assertEquals('foo', $expressionLanguage->evaluate('identity("foo")'));
- $this->assertEquals('"foo"', $expressionLanguage->compile('identity("foo")'));
- $this->assertEquals('FOO', $expressionLanguage->evaluate('strtoupper("foo")'));
- $this->assertEquals('\strtoupper("foo")', $expressionLanguage->compile('strtoupper("foo")'));
- $this->assertEquals('foo', $expressionLanguage->evaluate('strtolower("FOO")'));
- $this->assertEquals('\strtolower("FOO")', $expressionLanguage->compile('strtolower("FOO")'));
- $this->assertTrue($expressionLanguage->evaluate('fn_namespaced()'));
- $this->assertEquals('\Symfony\Component\ExpressionLanguage\Tests\Fixtures\fn_namespaced()', $expressionLanguage->compile('fn_namespaced()'));
- }
- /**
- * @dataProvider shortCircuitProviderEvaluate
- */
- public function testShortCircuitOperatorsEvaluate($expression, array $values, $expected)
- {
- $expressionLanguage = new ExpressionLanguage();
- $this->assertEquals($expected, $expressionLanguage->evaluate($expression, $values));
- }
- /**
- * @dataProvider shortCircuitProviderCompile
- */
- public function testShortCircuitOperatorsCompile($expression, array $names, $expected)
- {
- $result = null;
- $expressionLanguage = new ExpressionLanguage();
- eval(sprintf('$result = %s;', $expressionLanguage->compile($expression, $names)));
- $this->assertSame($expected, $result);
- }
- /**
- * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
- * @expectedExceptionMessage Unexpected end of expression around position 6 for expression `node.`.
- */
- public function testParseThrowsInsteadOfNotice()
- {
- $expressionLanguage = new ExpressionLanguage();
- $expressionLanguage->parse('node.', ['node']);
- }
- public function shortCircuitProviderEvaluate()
- {
- $object = $this->getMockBuilder('stdClass')->setMethods(['foo'])->getMock();
- $object->expects($this->never())->method('foo');
- return [
- ['false and object.foo()', ['object' => $object], false],
- ['false && object.foo()', ['object' => $object], false],
- ['true || object.foo()', ['object' => $object], true],
- ['true or object.foo()', ['object' => $object], true],
- ];
- }
- public function shortCircuitProviderCompile()
- {
- return [
- ['false and foo', ['foo' => 'foo'], false],
- ['false && foo', ['foo' => 'foo'], false],
- ['true || foo', ['foo' => 'foo'], true],
- ['true or foo', ['foo' => 'foo'], true],
- ];
- }
- public function testCachingForOverriddenVariableNames()
- {
- $expressionLanguage = new ExpressionLanguage();
- $expression = 'a + b';
- $expressionLanguage->evaluate($expression, ['a' => 1, 'b' => 1]);
- $result = $expressionLanguage->compile($expression, ['a', 'B' => 'b']);
- $this->assertSame('($a + $B)', $result);
- }
- public function testStrictEquality()
- {
- $expressionLanguage = new ExpressionLanguage();
- $expression = '123 === a';
- $result = $expressionLanguage->compile($expression, ['a']);
- $this->assertSame('(123 === $a)', $result);
- }
- public function testCachingWithDifferentNamesOrder()
- {
- $cacheMock = $this->getMockBuilder('Psr\Cache\CacheItemPoolInterface')->getMock();
- $cacheItemMock = $this->getMockBuilder('Psr\Cache\CacheItemInterface')->getMock();
- $expressionLanguage = new ExpressionLanguage($cacheMock);
- $savedParsedExpressions = [];
- $cacheMock
- ->expects($this->exactly(2))
- ->method('getItem')
- ->with('a%20%2B%20b%2F%2Fa%7CB%3Ab')
- ->willReturn($cacheItemMock)
- ;
- $cacheItemMock
- ->expects($this->exactly(2))
- ->method('get')
- ->will($this->returnCallback(function () use (&$savedParsedExpression) {
- return $savedParsedExpression;
- }))
- ;
- $cacheItemMock
- ->expects($this->exactly(1))
- ->method('set')
- ->with($this->isInstanceOf(ParsedExpression::class))
- ->will($this->returnCallback(function ($parsedExpression) use (&$savedParsedExpression) {
- $savedParsedExpression = $parsedExpression;
- }))
- ;
- $cacheMock
- ->expects($this->exactly(1))
- ->method('save')
- ->with($cacheItemMock)
- ;
- $expression = 'a + b';
- $expressionLanguage->compile($expression, ['a', 'B' => 'b']);
- $expressionLanguage->compile($expression, ['B' => 'b', 'a']);
- }
- /**
- * @dataProvider getRegisterCallbacks
- * @expectedException \LogicException
- */
- public function testRegisterAfterParse($registerCallback)
- {
- $el = new ExpressionLanguage();
- $el->parse('1 + 1', []);
- $registerCallback($el);
- }
- /**
- * @dataProvider getRegisterCallbacks
- * @expectedException \LogicException
- */
- public function testRegisterAfterEval($registerCallback)
- {
- $el = new ExpressionLanguage();
- $el->evaluate('1 + 1');
- $registerCallback($el);
- }
- /**
- * @expectedException \RuntimeException
- * @expectedExceptionMessageRegExp /Unable to call method "\w+" of object "\w+"./
- */
- public function testCallBadCallable()
- {
- $el = new ExpressionLanguage();
- $el->evaluate('foo.myfunction()', ['foo' => new \stdClass()]);
- }
- /**
- * @dataProvider getRegisterCallbacks
- * @expectedException \LogicException
- */
- public function testRegisterAfterCompile($registerCallback)
- {
- $el = new ExpressionLanguage();
- $el->compile('1 + 1');
- $registerCallback($el);
- }
- public function getRegisterCallbacks()
- {
- return [
- [
- function (ExpressionLanguage $el) {
- $el->register('fn', function () {}, function () {});
- },
- ],
- [
- function (ExpressionLanguage $el) {
- $el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
- },
- ],
- [
- function (ExpressionLanguage $el) {
- $el->registerProvider(new TestProvider());
- },
- ],
- ];
- }
- }
|