ExpressionLanguageTest.php 11 KB

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