CacheTraitTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Contracts\Tests\Cache;
  11. use PHPUnit\Framework\TestCase;
  12. use Psr\Cache\CacheItemInterface;
  13. use Psr\Cache\CacheItemPoolInterface;
  14. use Symfony\Contracts\Cache\CacheTrait;
  15. /**
  16. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  17. */
  18. class CacheTraitTest extends TestCase
  19. {
  20. public function testSave()
  21. {
  22. $item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
  23. $item->method('set')
  24. ->willReturn($item);
  25. $item->method('isHit')
  26. ->willReturn(false);
  27. $item->expects($this->once())
  28. ->method('set')
  29. ->with('computed data');
  30. $cache = $this->getMockBuilder(TestPool::class)
  31. ->setMethods(array('getItem', 'save'))
  32. ->getMock();
  33. $cache->expects($this->once())
  34. ->method('getItem')
  35. ->with('key')
  36. ->willReturn($item);
  37. $cache->expects($this->once())
  38. ->method('save');
  39. $callback = function (CacheItemInterface $item) {
  40. return 'computed data';
  41. };
  42. $cache->get('key', $callback);
  43. }
  44. public function testNoCallbackCallOnHit()
  45. {
  46. $item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
  47. $item->method('isHit')
  48. ->willReturn(true);
  49. $item->expects($this->never())
  50. ->method('set');
  51. $cache = $this->getMockBuilder(TestPool::class)
  52. ->setMethods(array('getItem', 'save'))
  53. ->getMock();
  54. $cache->expects($this->once())
  55. ->method('getItem')
  56. ->with('key')
  57. ->willReturn($item);
  58. $cache->expects($this->never())
  59. ->method('save');
  60. $callback = function (CacheItemInterface $item) {
  61. $this->assertTrue(false, 'This code should never be reached');
  62. };
  63. $cache->get('key', $callback);
  64. }
  65. public function testRecomputeOnBetaInf()
  66. {
  67. $item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
  68. $item->method('set')
  69. ->willReturn($item);
  70. $item->method('isHit')
  71. // We want to recompute even if it is a hit
  72. ->willReturn(true);
  73. $item->expects($this->once())
  74. ->method('set')
  75. ->with('computed data');
  76. $cache = $this->getMockBuilder(TestPool::class)
  77. ->setMethods(array('getItem', 'save'))
  78. ->getMock();
  79. $cache->expects($this->once())
  80. ->method('getItem')
  81. ->with('key')
  82. ->willReturn($item);
  83. $cache->expects($this->once())
  84. ->method('save');
  85. $callback = function (CacheItemInterface $item) {
  86. return 'computed data';
  87. };
  88. $cache->get('key', $callback, INF);
  89. }
  90. public function testExceptionOnNegativeBeta()
  91. {
  92. $cache = $this->getMockBuilder(TestPool::class)
  93. ->setMethods(array('getItem', 'save'))
  94. ->getMock();
  95. $callback = function (CacheItemInterface $item) {
  96. return 'computed data';
  97. };
  98. $this->expectException(\InvalidArgumentException::class);
  99. $cache->get('key', $callback, -2);
  100. }
  101. }
  102. class TestPool implements CacheItemPoolInterface
  103. {
  104. use CacheTrait;
  105. public function hasItem($key)
  106. {
  107. }
  108. public function deleteItem($key)
  109. {
  110. }
  111. public function deleteItems(array $keys = array())
  112. {
  113. }
  114. public function getItem($key)
  115. {
  116. }
  117. public function getItems(array $key = array())
  118. {
  119. }
  120. public function saveDeferred(CacheItemInterface $item)
  121. {
  122. }
  123. public function save(CacheItemInterface $item)
  124. {
  125. }
  126. public function commit()
  127. {
  128. }
  129. public function clear()
  130. {
  131. }
  132. }