PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php

https://github.com/FabienD/symfony
PHP | 308 lines | 240 code | 39 blank | 29 comment | 0 complexity | 1577ecc979b1f82aaf0ff8a895e255c2 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ExpressionLanguage\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Psr\Cache\CacheItemInterface;
  13. use Psr\Cache\CacheItemPoolInterface;
  14. use Symfony\Component\ExpressionLanguage\ExpressionFunction;
  15. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  16. use Symfony\Component\ExpressionLanguage\ParsedExpression;
  17. use Symfony\Component\ExpressionLanguage\SyntaxError;
  18. use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
  19. class ExpressionLanguageTest extends TestCase
  20. {
  21. public function testCachedParse()
  22. {
  23. $cacheMock = $this->createMock(CacheItemPoolInterface::class);
  24. $cacheItemMock = $this->createMock(CacheItemInterface::class);
  25. $savedParsedExpression = null;
  26. $expressionLanguage = new ExpressionLanguage($cacheMock);
  27. $cacheMock
  28. ->expects($this->exactly(2))
  29. ->method('getItem')
  30. ->with('1%20%2B%201%2F%2F')
  31. ->willReturn($cacheItemMock)
  32. ;
  33. $cacheItemMock
  34. ->expects($this->exactly(2))
  35. ->method('get')
  36. ->willReturnCallback(function () use (&$savedParsedExpression) {
  37. return $savedParsedExpression;
  38. })
  39. ;
  40. $cacheItemMock
  41. ->expects($this->exactly(1))
  42. ->method('set')
  43. ->with($this->isInstanceOf(ParsedExpression::class))
  44. ->willReturnCallback(function ($parsedExpression) use (&$savedParsedExpression, $cacheItemMock) {
  45. $savedParsedExpression = $parsedExpression;
  46. return $cacheItemMock;
  47. })
  48. ;
  49. $cacheMock
  50. ->expects($this->exactly(1))
  51. ->method('save')
  52. ->with($cacheItemMock)
  53. ->willReturn(true)
  54. ;
  55. $parsedExpression = $expressionLanguage->parse('1 + 1', []);
  56. $this->assertSame($savedParsedExpression, $parsedExpression);
  57. $parsedExpression = $expressionLanguage->parse('1 + 1', []);
  58. $this->assertSame($savedParsedExpression, $parsedExpression);
  59. }
  60. public function testConstantFunction()
  61. {
  62. $expressionLanguage = new ExpressionLanguage();
  63. $this->assertEquals(\PHP_VERSION, $expressionLanguage->evaluate('constant("PHP_VERSION")'));
  64. $expressionLanguage = new ExpressionLanguage();
  65. $this->assertEquals('\constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
  66. }
  67. public function testProviders()
  68. {
  69. $expressionLanguage = new ExpressionLanguage(null, [new TestProvider()]);
  70. $this->assertEquals('foo', $expressionLanguage->evaluate('identity("foo")'));
  71. $this->assertEquals('"foo"', $expressionLanguage->compile('identity("foo")'));
  72. $this->assertEquals('FOO', $expressionLanguage->evaluate('strtoupper("foo")'));
  73. $this->assertEquals('\strtoupper("foo")', $expressionLanguage->compile('strtoupper("foo")'));
  74. $this->assertEquals('foo', $expressionLanguage->evaluate('strtolower("FOO")'));
  75. $this->assertEquals('\strtolower("FOO")', $expressionLanguage->compile('strtolower("FOO")'));
  76. $this->assertTrue($expressionLanguage->evaluate('fn_namespaced()'));
  77. $this->assertEquals('\Symfony\Component\ExpressionLanguage\Tests\Fixtures\fn_namespaced()', $expressionLanguage->compile('fn_namespaced()'));
  78. }
  79. /**
  80. * @dataProvider shortCircuitProviderEvaluate
  81. */
  82. public function testShortCircuitOperatorsEvaluate($expression, array $values, $expected)
  83. {
  84. $expressionLanguage = new ExpressionLanguage();
  85. $this->assertEquals($expected, $expressionLanguage->evaluate($expression, $values));
  86. }
  87. /**
  88. * @dataProvider shortCircuitProviderCompile
  89. */
  90. public function testShortCircuitOperatorsCompile($expression, array $names, $expected)
  91. {
  92. $result = null;
  93. $expressionLanguage = new ExpressionLanguage();
  94. eval(sprintf('$result = %s;', $expressionLanguage->compile($expression, $names)));
  95. $this->assertSame($expected, $result);
  96. }
  97. public function testParseThrowsInsteadOfNotice()
  98. {
  99. $this->expectException(SyntaxError::class);
  100. $this->expectExceptionMessage('Unexpected end of expression around position 6 for expression `node.`.');
  101. $expressionLanguage = new ExpressionLanguage();
  102. $expressionLanguage->parse('node.', ['node']);
  103. }
  104. public function shortCircuitProviderEvaluate()
  105. {
  106. $object = $this->getMockBuilder(\stdClass::class)->setMethods(['foo'])->getMock();
  107. $object->expects($this->never())->method('foo');
  108. return [
  109. ['false and object.foo()', ['object' => $object], false],
  110. ['false && object.foo()', ['object' => $object], false],
  111. ['true || object.foo()', ['object' => $object], true],
  112. ['true or object.foo()', ['object' => $object], true],
  113. ];
  114. }
  115. public function shortCircuitProviderCompile()
  116. {
  117. return [
  118. ['false and foo', ['foo' => 'foo'], false],
  119. ['false && foo', ['foo' => 'foo'], false],
  120. ['true || foo', ['foo' => 'foo'], true],
  121. ['true or foo', ['foo' => 'foo'], true],
  122. ];
  123. }
  124. public function testCachingForOverriddenVariableNames()
  125. {
  126. $expressionLanguage = new ExpressionLanguage();
  127. $expression = 'a + b';
  128. $expressionLanguage->evaluate($expression, ['a' => 1, 'b' => 1]);
  129. $result = $expressionLanguage->compile($expression, ['a', 'B' => 'b']);
  130. $this->assertSame('($a + $B)', $result);
  131. }
  132. public function testStrictEquality()
  133. {
  134. $expressionLanguage = new ExpressionLanguage();
  135. $expression = '123 === a';
  136. $result = $expressionLanguage->compile($expression, ['a']);
  137. $this->assertSame('(123 === $a)', $result);
  138. }
  139. public function testCachingWithDifferentNamesOrder()
  140. {
  141. $cacheMock = $this->createMock(CacheItemPoolInterface::class);
  142. $cacheItemMock = $this->createMock(CacheItemInterface::class);
  143. $expressionLanguage = new ExpressionLanguage($cacheMock);
  144. $savedParsedExpression = null;
  145. $cacheMock
  146. ->expects($this->exactly(2))
  147. ->method('getItem')
  148. ->with('a%20%2B%20b%2F%2Fa%7CB%3Ab')
  149. ->willReturn($cacheItemMock)
  150. ;
  151. $cacheItemMock
  152. ->expects($this->exactly(2))
  153. ->method('get')
  154. ->willReturnCallback(function () use (&$savedParsedExpression) {
  155. return $savedParsedExpression;
  156. })
  157. ;
  158. $cacheItemMock
  159. ->expects($this->exactly(1))
  160. ->method('set')
  161. ->with($this->isInstanceOf(ParsedExpression::class))
  162. ->willReturnCallback(function ($parsedExpression) use (&$savedParsedExpression, $cacheItemMock) {
  163. $savedParsedExpression = $parsedExpression;
  164. return $cacheItemMock;
  165. })
  166. ;
  167. $cacheMock
  168. ->expects($this->exactly(1))
  169. ->method('save')
  170. ->with($cacheItemMock)
  171. ->willReturn(true)
  172. ;
  173. $expression = 'a + b';
  174. $expressionLanguage->compile($expression, ['a', 'B' => 'b']);
  175. $expressionLanguage->compile($expression, ['B' => 'b', 'a']);
  176. }
  177. public function testOperatorCollisions()
  178. {
  179. $expressionLanguage = new ExpressionLanguage();
  180. $expression = 'foo.not in [bar]';
  181. $compiled = $expressionLanguage->compile($expression, ['foo', 'bar']);
  182. $this->assertSame('in_array($foo->not, [0 => $bar])', $compiled);
  183. $result = $expressionLanguage->evaluate($expression, ['foo' => (object) ['not' => 'test'], 'bar' => 'test']);
  184. $this->assertTrue($result);
  185. }
  186. /**
  187. * @dataProvider getRegisterCallbacks
  188. */
  189. public function testRegisterAfterParse($registerCallback)
  190. {
  191. $this->expectException(\LogicException::class);
  192. $el = new ExpressionLanguage();
  193. $el->parse('1 + 1', []);
  194. $registerCallback($el);
  195. }
  196. /**
  197. * @dataProvider getRegisterCallbacks
  198. */
  199. public function testRegisterAfterEval($registerCallback)
  200. {
  201. $this->expectException(\LogicException::class);
  202. $el = new ExpressionLanguage();
  203. $el->evaluate('1 + 1');
  204. $registerCallback($el);
  205. }
  206. /**
  207. * @dataProvider provideNullSafe
  208. */
  209. public function testNullSafeEvaluate($expression, $foo)
  210. {
  211. $expressionLanguage = new ExpressionLanguage();
  212. $this->assertNull($expressionLanguage->evaluate($expression, ['foo' => $foo]));
  213. }
  214. /**
  215. * @dataProvider provideNullSafe
  216. */
  217. public function testNullsafeCompile($expression, $foo)
  218. {
  219. $expressionLanguage = new ExpressionLanguage();
  220. $this->assertNull(eval(sprintf('return %s;', $expressionLanguage->compile($expression, ['foo' => 'foo']))));
  221. }
  222. public function provideNullsafe()
  223. {
  224. $foo = new class() extends \stdClass {
  225. public function bar()
  226. {
  227. return null;
  228. }
  229. };
  230. yield ['foo?.bar', null];
  231. yield ['foo?.bar()', null];
  232. yield ['foo.bar?.baz', (object) ['bar' => null]];
  233. yield ['foo.bar?.baz()', (object) ['bar' => null]];
  234. yield ['foo["bar"]?.baz', ['bar' => null]];
  235. yield ['foo["bar"]?.baz()', ['bar' => null]];
  236. yield ['foo.bar()?.baz', $foo];
  237. yield ['foo.bar()?.baz()', $foo];
  238. }
  239. /**
  240. * @dataProvider getRegisterCallbacks
  241. */
  242. public function testRegisterAfterCompile($registerCallback)
  243. {
  244. $this->expectException(\LogicException::class);
  245. $el = new ExpressionLanguage();
  246. $el->compile('1 + 1');
  247. $registerCallback($el);
  248. }
  249. public function getRegisterCallbacks()
  250. {
  251. return [
  252. [
  253. function (ExpressionLanguage $el) {
  254. $el->register('fn', function () {}, function () {});
  255. },
  256. ],
  257. [
  258. function (ExpressionLanguage $el) {
  259. $el->addFunction(new ExpressionFunction('fn', function () {}, function () {}));
  260. },
  261. ],
  262. [
  263. function (ExpressionLanguage $el) {
  264. $el->registerProvider(new TestProvider());
  265. },
  266. ],
  267. ];
  268. }
  269. }