CacheTestCase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Simple;
  11. use Cache\IntegrationTests\SimpleCacheTest;
  12. use Psr\SimpleCache\CacheInterface;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. abstract class CacheTestCase extends SimpleCacheTest
  15. {
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. if (!array_key_exists('testPrune', $this->skippedTests) && !$this->createSimpleCache() instanceof PruneableInterface) {
  20. $this->skippedTests['testPrune'] = 'Not a pruneable cache pool.';
  21. }
  22. }
  23. public static function validKeys()
  24. {
  25. return array_merge(parent::validKeys(), [["a\0b"]]);
  26. }
  27. public function testDefaultLifeTime()
  28. {
  29. if (isset($this->skippedTests[__FUNCTION__])) {
  30. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  31. }
  32. $cache = $this->createSimpleCache(2);
  33. $cache->clear();
  34. $cache->set('key.dlt', 'value');
  35. sleep(1);
  36. $this->assertSame('value', $cache->get('key.dlt'));
  37. sleep(2);
  38. $this->assertNull($cache->get('key.dlt'));
  39. $cache->clear();
  40. }
  41. public function testNotUnserializable()
  42. {
  43. if (isset($this->skippedTests[__FUNCTION__])) {
  44. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  45. }
  46. $cache = $this->createSimpleCache();
  47. $cache->clear();
  48. $cache->set('foo', new NotUnserializable());
  49. $this->assertNull($cache->get('foo'));
  50. $cache->setMultiple(['foo' => new NotUnserializable()]);
  51. foreach ($cache->getMultiple(['foo']) as $value) {
  52. }
  53. $this->assertNull($value);
  54. $cache->clear();
  55. }
  56. public function testPrune()
  57. {
  58. if (isset($this->skippedTests[__FUNCTION__])) {
  59. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  60. }
  61. if (!method_exists($this, 'isPruned')) {
  62. $this->fail('Test classes for pruneable caches must implement `isPruned($cache, $name)` method.');
  63. }
  64. /** @var PruneableInterface|CacheInterface $cache */
  65. $cache = $this->createSimpleCache();
  66. $cache->clear();
  67. $cache->set('foo', 'foo-val', new \DateInterval('PT05S'));
  68. $cache->set('bar', 'bar-val', new \DateInterval('PT10S'));
  69. $cache->set('baz', 'baz-val', new \DateInterval('PT15S'));
  70. $cache->set('qux', 'qux-val', new \DateInterval('PT20S'));
  71. sleep(30);
  72. $cache->prune();
  73. $this->assertTrue($this->isPruned($cache, 'foo'));
  74. $this->assertTrue($this->isPruned($cache, 'bar'));
  75. $this->assertTrue($this->isPruned($cache, 'baz'));
  76. $this->assertTrue($this->isPruned($cache, 'qux'));
  77. $cache->set('foo', 'foo-val');
  78. $cache->set('bar', 'bar-val', new \DateInterval('PT20S'));
  79. $cache->set('baz', 'baz-val', new \DateInterval('PT40S'));
  80. $cache->set('qux', 'qux-val', new \DateInterval('PT80S'));
  81. $cache->prune();
  82. $this->assertFalse($this->isPruned($cache, 'foo'));
  83. $this->assertFalse($this->isPruned($cache, 'bar'));
  84. $this->assertFalse($this->isPruned($cache, 'baz'));
  85. $this->assertFalse($this->isPruned($cache, 'qux'));
  86. sleep(30);
  87. $cache->prune();
  88. $this->assertFalse($this->isPruned($cache, 'foo'));
  89. $this->assertTrue($this->isPruned($cache, 'bar'));
  90. $this->assertFalse($this->isPruned($cache, 'baz'));
  91. $this->assertFalse($this->isPruned($cache, 'qux'));
  92. sleep(30);
  93. $cache->prune();
  94. $this->assertFalse($this->isPruned($cache, 'foo'));
  95. $this->assertTrue($this->isPruned($cache, 'baz'));
  96. $this->assertFalse($this->isPruned($cache, 'qux'));
  97. sleep(30);
  98. $cache->prune();
  99. $this->assertFalse($this->isPruned($cache, 'foo'));
  100. $this->assertTrue($this->isPruned($cache, 'qux'));
  101. $cache->clear();
  102. }
  103. }
  104. class NotUnserializable implements \Serializable
  105. {
  106. public function serialize()
  107. {
  108. return serialize(123);
  109. }
  110. public function unserialize($ser)
  111. {
  112. throw new \Exception(__CLASS__);
  113. }
  114. }