PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/l3l0/symfony
PHP | 214 lines | 162 code | 31 blank | 21 comment | 0 complexity | 66aa892d0b67a7e8f722511b140fd9ac 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 Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  12. use Symfony\Component\ExpressionLanguage\ParsedExpression;
  13. use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;
  14. class ExpressionLanguageTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testCachedParse()
  17. {
  18. $cacheMock = $this->getMock('Psr\Cache\CacheItemPoolInterface');
  19. $cacheItemMock = $this->getMock('Psr\Cache\CacheItemInterface');
  20. $savedParsedExpression = null;
  21. $expressionLanguage = new ExpressionLanguage($cacheMock);
  22. $cacheMock
  23. ->expects($this->exactly(2))
  24. ->method('getItem')
  25. ->with('1%20%2B%201%2F%2F')
  26. ->willReturn($cacheItemMock)
  27. ;
  28. $cacheItemMock
  29. ->expects($this->exactly(2))
  30. ->method('get')
  31. ->will($this->returnCallback(function () use (&$savedParsedExpression) {
  32. return $savedParsedExpression;
  33. }))
  34. ;
  35. $cacheItemMock
  36. ->expects($this->exactly(1))
  37. ->method('set')
  38. ->with($this->isInstanceOf(ParsedExpression::class))
  39. ->will($this->returnCallback(function ($parsedExpression) use (&$savedParsedExpression) {
  40. $savedParsedExpression = $parsedExpression;
  41. }))
  42. ;
  43. $cacheMock
  44. ->expects($this->exactly(1))
  45. ->method('save')
  46. ->with($cacheItemMock)
  47. ;
  48. $parsedExpression = $expressionLanguage->parse('1 + 1', array());
  49. $this->assertSame($savedParsedExpression, $parsedExpression);
  50. $parsedExpression = $expressionLanguage->parse('1 + 1', array());
  51. $this->assertSame($savedParsedExpression, $parsedExpression);
  52. }
  53. /**
  54. * @group legacy
  55. */
  56. public function testCachedParseWithDeprecatedParserCacheInterface()
  57. {
  58. $cacheMock = $this->getMock('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface');
  59. $cacheItemMock = $this->getMock('Psr\Cache\CacheItemInterface');
  60. $savedParsedExpression = null;
  61. $expressionLanguage = new ExpressionLanguage($cacheMock);
  62. $cacheMock
  63. ->expects($this->exactly(1))
  64. ->method('fetch')
  65. ->with('1%20%2B%201%2F%2F')
  66. ->willReturn($savedParsedExpression)
  67. ;
  68. $cacheMock
  69. ->expects($this->exactly(1))
  70. ->method('save')
  71. ->with('1%20%2B%201%2F%2F', $this->isInstanceOf(ParsedExpression::class))
  72. ->will($this->returnCallback(function ($key, $expression) use (&$savedParsedExpression) {
  73. $savedParsedExpression = $expression;
  74. }))
  75. ;
  76. $parsedExpression = $expressionLanguage->parse('1 + 1', array());
  77. $this->assertSame($savedParsedExpression, $parsedExpression);
  78. }
  79. /**
  80. * @expectedException \InvalidArgumentException
  81. * @expectedExceptionMessage Cache argument has to implement Psr\Cache\CacheItemPoolInterface.
  82. */
  83. public function testWrongCacheImplementation()
  84. {
  85. $cacheMock = $this->getMock('Psr\Cache\CacheItemSpoolInterface');
  86. $expressionLanguage = new ExpressionLanguage($cacheMock);
  87. }
  88. public function testConstantFunction()
  89. {
  90. $expressionLanguage = new ExpressionLanguage();
  91. $this->assertEquals(PHP_VERSION, $expressionLanguage->evaluate('constant("PHP_VERSION")'));
  92. $expressionLanguage = new ExpressionLanguage();
  93. $this->assertEquals('constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
  94. }
  95. public function testProviders()
  96. {
  97. $expressionLanguage = new ExpressionLanguage(null, array(new TestProvider()));
  98. $this->assertEquals('foo', $expressionLanguage->evaluate('identity("foo")'));
  99. $this->assertEquals('"foo"', $expressionLanguage->compile('identity("foo")'));
  100. }
  101. /**
  102. * @dataProvider shortCircuitProviderEvaluate
  103. */
  104. public function testShortCircuitOperatorsEvaluate($expression, array $values, $expected)
  105. {
  106. $expressionLanguage = new ExpressionLanguage();
  107. $this->assertEquals($expected, $expressionLanguage->evaluate($expression, $values));
  108. }
  109. /**
  110. * @dataProvider shortCircuitProviderCompile
  111. */
  112. public function testShortCircuitOperatorsCompile($expression, array $names, $expected)
  113. {
  114. $result = null;
  115. $expressionLanguage = new ExpressionLanguage();
  116. eval(sprintf('$result = %s;', $expressionLanguage->compile($expression, $names)));
  117. $this->assertSame($expected, $result);
  118. }
  119. public function shortCircuitProviderEvaluate()
  120. {
  121. $object = $this->getMockBuilder('stdClass')->setMethods(array('foo'))->getMock();
  122. $object->expects($this->never())->method('foo');
  123. return array(
  124. array('false and object.foo()', array('object' => $object), false),
  125. array('false && object.foo()', array('object' => $object), false),
  126. array('true || object.foo()', array('object' => $object), true),
  127. array('true or object.foo()', array('object' => $object), true),
  128. );
  129. }
  130. public function shortCircuitProviderCompile()
  131. {
  132. return array(
  133. array('false and foo', array('foo' => 'foo'), false),
  134. array('false && foo', array('foo' => 'foo'), false),
  135. array('true || foo', array('foo' => 'foo'), true),
  136. array('true or foo', array('foo' => 'foo'), true),
  137. );
  138. }
  139. public function testCachingForOverriddenVariableNames()
  140. {
  141. $expressionLanguage = new ExpressionLanguage();
  142. $expression = 'a + b';
  143. $expressionLanguage->evaluate($expression, array('a' => 1, 'b' => 1));
  144. $result = $expressionLanguage->compile($expression, array('a', 'B' => 'b'));
  145. $this->assertSame('($a + $B)', $result);
  146. }
  147. public function testCachingWithDifferentNamesOrder()
  148. {
  149. $cacheMock = $this->getMock('Psr\Cache\CacheItemPoolInterface');
  150. $cacheItemMock = $this->getMock('Psr\Cache\CacheItemInterface');
  151. $expressionLanguage = new ExpressionLanguage($cacheMock);
  152. $savedParsedExpressions = array();
  153. $cacheMock
  154. ->expects($this->exactly(2))
  155. ->method('getItem')
  156. ->with('a%20%2B%20b%2F%2Fa%7CB%3Ab')
  157. ->willReturn($cacheItemMock)
  158. ;
  159. $cacheItemMock
  160. ->expects($this->exactly(2))
  161. ->method('get')
  162. ->will($this->returnCallback(function () use (&$savedParsedExpression) {
  163. return $savedParsedExpression;
  164. }))
  165. ;
  166. $cacheItemMock
  167. ->expects($this->exactly(1))
  168. ->method('set')
  169. ->with($this->isInstanceOf(ParsedExpression::class))
  170. ->will($this->returnCallback(function ($parsedExpression) use (&$savedParsedExpression) {
  171. $savedParsedExpression = $parsedExpression;
  172. }))
  173. ;
  174. $cacheMock
  175. ->expects($this->exactly(1))
  176. ->method('save')
  177. ->with($cacheItemMock)
  178. ;
  179. $expression = 'a + b';
  180. $expressionLanguage->compile($expression, array('a', 'B' => 'b'));
  181. $expressionLanguage->compile($expression, array('B' => 'b', 'a'));
  182. }
  183. }