AdapterTestCase.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Cache\Tests\Adapter;
  11. use Cache\IntegrationTests\CachePoolTest;
  12. use PHPUnit\Framework\Assert;
  13. use Psr\Cache\CacheItemInterface;
  14. use Psr\Cache\CacheItemPoolInterface;
  15. use Symfony\Component\Cache\CacheItem;
  16. use Symfony\Component\Cache\PruneableInterface;
  17. use Symfony\Contracts\Cache\CallbackInterface;
  18. abstract class AdapterTestCase extends CachePoolTest
  19. {
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. if (!\array_key_exists('testPrune', $this->skippedTests) && !$this->createCachePool() instanceof PruneableInterface) {
  24. $this->skippedTests['testPrune'] = 'Not a pruneable cache pool.';
  25. }
  26. }
  27. public function testGet()
  28. {
  29. if (isset($this->skippedTests[__FUNCTION__])) {
  30. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  31. }
  32. $cache = $this->createCachePool();
  33. $cache->clear();
  34. $value = mt_rand();
  35. $this->assertSame($value, $cache->get('foo', function (CacheItem $item) use ($value) {
  36. $this->assertSame('foo', $item->getKey());
  37. return $value;
  38. }));
  39. $item = $cache->getItem('foo');
  40. $this->assertSame($value, $item->get());
  41. $isHit = true;
  42. $this->assertSame($value, $cache->get('foo', function (CacheItem $item) use (&$isHit) { $isHit = false; }, 0));
  43. $this->assertTrue($isHit);
  44. $this->assertNull($cache->get('foo', function (CacheItem $item) use (&$isHit, $value) {
  45. $isHit = false;
  46. $this->assertTrue($item->isHit());
  47. $this->assertSame($value, $item->get());
  48. }, INF));
  49. $this->assertFalse($isHit);
  50. $this->assertSame($value, $cache->get('bar', new class($value) implements CallbackInterface {
  51. private $value;
  52. public function __construct(int $value)
  53. {
  54. $this->value = $value;
  55. }
  56. public function __invoke(CacheItemInterface $item, bool &$save)
  57. {
  58. Assert::assertSame('bar', $item->getKey());
  59. return $this->value;
  60. }
  61. }));
  62. }
  63. public function testRecursiveGet()
  64. {
  65. if (isset($this->skippedTests[__FUNCTION__])) {
  66. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  67. }
  68. $cache = $this->createCachePool(0, __FUNCTION__);
  69. $v = $cache->get('k1', function () use (&$counter, $cache) {
  70. $cache->get('k2', function () use (&$counter) { return ++$counter; });
  71. $v = $cache->get('k2', function () use (&$counter) { return ++$counter; }); // ensure the callback is called once
  72. return $v;
  73. });
  74. $this->assertSame(1, $counter);
  75. $this->assertSame(1, $v);
  76. $this->assertSame(1, $cache->get('k2', function () { return 2; }));
  77. }
  78. public function testGetMetadata()
  79. {
  80. if (isset($this->skippedTests[__FUNCTION__])) {
  81. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  82. }
  83. $cache = $this->createCachePool(0, __FUNCTION__);
  84. $cache->deleteItem('foo');
  85. $cache->get('foo', function ($item) {
  86. $item->expiresAfter(10);
  87. usleep(999000);
  88. return 'bar';
  89. });
  90. $item = $cache->getItem('foo');
  91. $expected = [
  92. CacheItem::METADATA_EXPIRY => 9.5 + time(),
  93. CacheItem::METADATA_CTIME => 1000,
  94. ];
  95. $this->assertEqualsWithDelta($expected, $item->getMetadata(), .6, 'Item metadata should embed expiry and ctime.');
  96. }
  97. public function testDefaultLifeTime()
  98. {
  99. if (isset($this->skippedTests[__FUNCTION__])) {
  100. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  101. }
  102. $cache = $this->createCachePool(2);
  103. $item = $cache->getItem('key.dlt');
  104. $item->set('value');
  105. $cache->save($item);
  106. sleep(1);
  107. $item = $cache->getItem('key.dlt');
  108. $this->assertTrue($item->isHit());
  109. sleep(2);
  110. $item = $cache->getItem('key.dlt');
  111. $this->assertFalse($item->isHit());
  112. }
  113. public function testExpiration()
  114. {
  115. if (isset($this->skippedTests[__FUNCTION__])) {
  116. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  117. }
  118. $cache = $this->createCachePool();
  119. $cache->save($cache->getItem('k1')->set('v1')->expiresAfter(2));
  120. $cache->save($cache->getItem('k2')->set('v2')->expiresAfter(366 * 86400));
  121. sleep(3);
  122. $item = $cache->getItem('k1');
  123. $this->assertFalse($item->isHit());
  124. $this->assertNull($item->get(), "Item's value must be null when isHit() is false.");
  125. $item = $cache->getItem('k2');
  126. $this->assertTrue($item->isHit());
  127. $this->assertSame('v2', $item->get());
  128. }
  129. public function testNotUnserializable()
  130. {
  131. if (isset($this->skippedTests[__FUNCTION__])) {
  132. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  133. }
  134. $cache = $this->createCachePool();
  135. $item = $cache->getItem('foo');
  136. $cache->save($item->set(new NotUnserializable()));
  137. $item = $cache->getItem('foo');
  138. $this->assertFalse($item->isHit());
  139. foreach ($cache->getItems(['foo']) as $item) {
  140. }
  141. $cache->save($item->set(new NotUnserializable()));
  142. foreach ($cache->getItems(['foo']) as $item) {
  143. }
  144. $this->assertFalse($item->isHit());
  145. }
  146. public function testPrune()
  147. {
  148. if (isset($this->skippedTests[__FUNCTION__])) {
  149. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  150. }
  151. if (!method_exists($this, 'isPruned')) {
  152. $this->fail('Test classes for pruneable caches must implement `isPruned($cache, $name)` method.');
  153. }
  154. /** @var PruneableInterface|CacheItemPoolInterface $cache */
  155. $cache = $this->createCachePool();
  156. $doSet = function ($name, $value, \DateInterval $expiresAfter = null) use ($cache) {
  157. $item = $cache->getItem($name);
  158. $item->set($value);
  159. if ($expiresAfter) {
  160. $item->expiresAfter($expiresAfter);
  161. }
  162. $cache->save($item);
  163. };
  164. $doSet('foo', 'foo-val', new \DateInterval('PT05S'));
  165. $doSet('bar', 'bar-val', new \DateInterval('PT10S'));
  166. $doSet('baz', 'baz-val', new \DateInterval('PT15S'));
  167. $doSet('qux', 'qux-val', new \DateInterval('PT20S'));
  168. sleep(30);
  169. $cache->prune();
  170. $this->assertTrue($this->isPruned($cache, 'foo'));
  171. $this->assertTrue($this->isPruned($cache, 'bar'));
  172. $this->assertTrue($this->isPruned($cache, 'baz'));
  173. $this->assertTrue($this->isPruned($cache, 'qux'));
  174. $doSet('foo', 'foo-val');
  175. $doSet('bar', 'bar-val', new \DateInterval('PT20S'));
  176. $doSet('baz', 'baz-val', new \DateInterval('PT40S'));
  177. $doSet('qux', 'qux-val', new \DateInterval('PT80S'));
  178. $cache->prune();
  179. $this->assertFalse($this->isPruned($cache, 'foo'));
  180. $this->assertFalse($this->isPruned($cache, 'bar'));
  181. $this->assertFalse($this->isPruned($cache, 'baz'));
  182. $this->assertFalse($this->isPruned($cache, 'qux'));
  183. sleep(30);
  184. $cache->prune();
  185. $this->assertFalse($this->isPruned($cache, 'foo'));
  186. $this->assertTrue($this->isPruned($cache, 'bar'));
  187. $this->assertFalse($this->isPruned($cache, 'baz'));
  188. $this->assertFalse($this->isPruned($cache, 'qux'));
  189. sleep(30);
  190. $cache->prune();
  191. $this->assertFalse($this->isPruned($cache, 'foo'));
  192. $this->assertTrue($this->isPruned($cache, 'baz'));
  193. $this->assertFalse($this->isPruned($cache, 'qux'));
  194. sleep(30);
  195. $cache->prune();
  196. $this->assertFalse($this->isPruned($cache, 'foo'));
  197. $this->assertTrue($this->isPruned($cache, 'qux'));
  198. }
  199. }
  200. class NotUnserializable
  201. {
  202. public function __wakeup()
  203. {
  204. throw new \Exception(__CLASS__);
  205. }
  206. }