ParserCacheAdapterTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Node\Node;
  13. use Symfony\Component\ExpressionLanguage\ParsedExpression;
  14. use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheAdapter;
  15. /**
  16. * @group legacy
  17. */
  18. class ParserCacheAdapterTest extends TestCase
  19. {
  20. public function testGetItem()
  21. {
  22. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  23. $key = 'key';
  24. $value = 'value';
  25. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  26. $poolMock
  27. ->expects($this->once())
  28. ->method('fetch')
  29. ->with($key)
  30. ->willReturn($value)
  31. ;
  32. $cacheItem = $parserCacheAdapter->getItem($key);
  33. $this->assertEquals($cacheItem->get(), $value);
  34. $this->assertEquals($cacheItem->isHit(), true);
  35. }
  36. public function testSave()
  37. {
  38. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  39. $cacheItemMock = $this->getMockBuilder('Psr\Cache\CacheItemInterface')->getMock();
  40. $key = 'key';
  41. $value = new ParsedExpression('1 + 1', new Node([], []));
  42. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  43. $poolMock
  44. ->expects($this->once())
  45. ->method('save')
  46. ->with($key, $value)
  47. ;
  48. $cacheItemMock
  49. ->expects($this->once())
  50. ->method('getKey')
  51. ->willReturn($key)
  52. ;
  53. $cacheItemMock
  54. ->expects($this->once())
  55. ->method('get')
  56. ->willReturn($value)
  57. ;
  58. $cacheItem = $parserCacheAdapter->save($cacheItemMock);
  59. }
  60. public function testGetItems()
  61. {
  62. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  63. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  64. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\BadMethodCallException::class);
  65. $parserCacheAdapter->getItems();
  66. }
  67. public function testHasItem()
  68. {
  69. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  70. $key = 'key';
  71. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  72. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\BadMethodCallException::class);
  73. $parserCacheAdapter->hasItem($key);
  74. }
  75. public function testClear()
  76. {
  77. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  78. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  79. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\BadMethodCallException::class);
  80. $parserCacheAdapter->clear();
  81. }
  82. public function testDeleteItem()
  83. {
  84. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  85. $key = 'key';
  86. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  87. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\BadMethodCallException::class);
  88. $parserCacheAdapter->deleteItem($key);
  89. }
  90. public function testDeleteItems()
  91. {
  92. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  93. $keys = ['key'];
  94. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  95. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\BadMethodCallException::class);
  96. $parserCacheAdapter->deleteItems($keys);
  97. }
  98. public function testSaveDeferred()
  99. {
  100. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  101. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  102. $cacheItemMock = $this->getMockBuilder('Psr\Cache\CacheItemInterface')->getMock();
  103. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\BadMethodCallException::class);
  104. $parserCacheAdapter->saveDeferred($cacheItemMock);
  105. }
  106. public function testCommit()
  107. {
  108. $poolMock = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface')->getMock();
  109. $parserCacheAdapter = new ParserCacheAdapter($poolMock);
  110. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\BadMethodCallException::class);
  111. $parserCacheAdapter->commit();
  112. }
  113. }