AdapterTestCase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. abstract class AdapterTestCase extends CachePoolTest
  15. {
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. if (!array_key_exists('testPrune', $this->skippedTests) && !$this->createCachePool() instanceof PruneableInterface) {
  20. $this->skippedTests['testPrune'] = 'Not a pruneable cache pool.';
  21. }
  22. }
  23. public function testDefaultLifeTime()
  24. {
  25. if (isset($this->skippedTests[__FUNCTION__])) {
  26. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  27. }
  28. $cache = $this->createCachePool(2);
  29. $item = $cache->getItem('key.dlt');
  30. $item->set('value');
  31. $cache->save($item);
  32. sleep(1);
  33. $item = $cache->getItem('key.dlt');
  34. $this->assertTrue($item->isHit());
  35. sleep(2);
  36. $item = $cache->getItem('key.dlt');
  37. $this->assertFalse($item->isHit());
  38. }
  39. public function testExpiration()
  40. {
  41. if (isset($this->skippedTests[__FUNCTION__])) {
  42. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  43. }
  44. $cache = $this->createCachePool();
  45. $cache->save($cache->getItem('k1')->set('v1')->expiresAfter(2));
  46. $cache->save($cache->getItem('k2')->set('v2')->expiresAfter(366 * 86400));
  47. sleep(3);
  48. $item = $cache->getItem('k1');
  49. $this->assertFalse($item->isHit());
  50. $this->assertNull($item->get(), "Item's value must be null when isHit() is false.");
  51. $item = $cache->getItem('k2');
  52. $this->assertTrue($item->isHit());
  53. $this->assertSame('v2', $item->get());
  54. }
  55. public function testNotUnserializable()
  56. {
  57. if (isset($this->skippedTests[__FUNCTION__])) {
  58. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  59. }
  60. $cache = $this->createCachePool();
  61. $item = $cache->getItem('foo');
  62. $cache->save($item->set(new NotUnserializable()));
  63. $item = $cache->getItem('foo');
  64. $this->assertFalse($item->isHit());
  65. foreach ($cache->getItems(['foo']) as $item) {
  66. }
  67. $cache->save($item->set(new NotUnserializable()));
  68. foreach ($cache->getItems(['foo']) as $item) {
  69. }
  70. $this->assertFalse($item->isHit());
  71. }
  72. public function testPrune()
  73. {
  74. if (isset($this->skippedTests[__FUNCTION__])) {
  75. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  76. }
  77. if (!method_exists($this, 'isPruned')) {
  78. $this->fail('Test classes for pruneable caches must implement `isPruned($cache, $name)` method.');
  79. }
  80. /** @var PruneableInterface|CacheItemPoolInterface $cache */
  81. $cache = $this->createCachePool();
  82. $doSet = function ($name, $value, \DateInterval $expiresAfter = null) use ($cache) {
  83. $item = $cache->getItem($name);
  84. $item->set($value);
  85. if ($expiresAfter) {
  86. $item->expiresAfter($expiresAfter);
  87. }
  88. $cache->save($item);
  89. };
  90. $doSet('foo', 'foo-val', new \DateInterval('PT05S'));
  91. $doSet('bar', 'bar-val', new \DateInterval('PT10S'));
  92. $doSet('baz', 'baz-val', new \DateInterval('PT15S'));
  93. $doSet('qux', 'qux-val', new \DateInterval('PT20S'));
  94. sleep(30);
  95. $cache->prune();
  96. $this->assertTrue($this->isPruned($cache, 'foo'));
  97. $this->assertTrue($this->isPruned($cache, 'bar'));
  98. $this->assertTrue($this->isPruned($cache, 'baz'));
  99. $this->assertTrue($this->isPruned($cache, 'qux'));
  100. $doSet('foo', 'foo-val');
  101. $doSet('bar', 'bar-val', new \DateInterval('PT20S'));
  102. $doSet('baz', 'baz-val', new \DateInterval('PT40S'));
  103. $doSet('qux', 'qux-val', new \DateInterval('PT80S'));
  104. $cache->prune();
  105. $this->assertFalse($this->isPruned($cache, 'foo'));
  106. $this->assertFalse($this->isPruned($cache, 'bar'));
  107. $this->assertFalse($this->isPruned($cache, 'baz'));
  108. $this->assertFalse($this->isPruned($cache, 'qux'));
  109. sleep(30);
  110. $cache->prune();
  111. $this->assertFalse($this->isPruned($cache, 'foo'));
  112. $this->assertTrue($this->isPruned($cache, 'bar'));
  113. $this->assertFalse($this->isPruned($cache, 'baz'));
  114. $this->assertFalse($this->isPruned($cache, 'qux'));
  115. sleep(30);
  116. $cache->prune();
  117. $this->assertFalse($this->isPruned($cache, 'foo'));
  118. $this->assertTrue($this->isPruned($cache, 'baz'));
  119. $this->assertFalse($this->isPruned($cache, 'qux'));
  120. sleep(30);
  121. $cache->prune();
  122. $this->assertFalse($this->isPruned($cache, 'foo'));
  123. $this->assertTrue($this->isPruned($cache, 'qux'));
  124. }
  125. }
  126. class NotUnserializable implements \Serializable
  127. {
  128. public function serialize()
  129. {
  130. return serialize(123);
  131. }
  132. public function unserialize($ser)
  133. {
  134. throw new \Exception(__CLASS__);
  135. }
  136. }