ExpressionLanguageTest.php 10 KB

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