AdapterTestCase.php 5.5 KB

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