Psr16CacheTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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;
  11. use Cache\IntegrationTests\SimpleCacheTest;
  12. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. use Symfony\Component\Cache\Psr16Cache;
  15. /**
  16. * @group time-sensitive
  17. */
  18. class Psr16CacheTest extends SimpleCacheTest
  19. {
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. if (\array_key_exists('testPrune', $this->skippedTests)) {
  24. return;
  25. }
  26. $pool = $this->createSimpleCache();
  27. if ($pool instanceof Psr16Cache) {
  28. $pool = ((array) $pool)[sprintf("\0%s\0pool", Psr16Cache::class)];
  29. }
  30. if (!$pool instanceof PruneableInterface) {
  31. $this->skippedTests['testPrune'] = 'Not a pruneable cache pool.';
  32. }
  33. }
  34. public function createSimpleCache($defaultLifetime = 0)
  35. {
  36. return new Psr16Cache(new FilesystemAdapter('', $defaultLifetime));
  37. }
  38. public static function validKeys()
  39. {
  40. return array_merge(parent::validKeys(), [["a\0b"]]);
  41. }
  42. public function testDefaultLifeTime()
  43. {
  44. if (isset($this->skippedTests[__FUNCTION__])) {
  45. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  46. }
  47. $cache = $this->createSimpleCache(2);
  48. $cache->clear();
  49. $cache->set('key.dlt', 'value');
  50. sleep(1);
  51. $this->assertSame('value', $cache->get('key.dlt'));
  52. sleep(2);
  53. $this->assertNull($cache->get('key.dlt'));
  54. $cache->clear();
  55. }
  56. public function testNotUnserializable()
  57. {
  58. if (isset($this->skippedTests[__FUNCTION__])) {
  59. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  60. }
  61. $cache = $this->createSimpleCache();
  62. $cache->clear();
  63. $cache->set('foo', new NotUnserializable());
  64. $this->assertNull($cache->get('foo'));
  65. $cache->setMultiple(['foo' => new NotUnserializable()]);
  66. foreach ($cache->getMultiple(['foo']) as $value) {
  67. }
  68. $this->assertNull($value);
  69. $cache->clear();
  70. }
  71. public function testPrune()
  72. {
  73. if (isset($this->skippedTests[__FUNCTION__])) {
  74. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  75. }
  76. /** @var PruneableInterface|CacheInterface $cache */
  77. $cache = $this->createSimpleCache();
  78. $cache->clear();
  79. $cache->set('foo', 'foo-val', new \DateInterval('PT05S'));
  80. $cache->set('bar', 'bar-val', new \DateInterval('PT10S'));
  81. $cache->set('baz', 'baz-val', new \DateInterval('PT15S'));
  82. $cache->set('qux', 'qux-val', new \DateInterval('PT20S'));
  83. sleep(30);
  84. $cache->prune();
  85. $this->assertTrue($this->isPruned($cache, 'foo'));
  86. $this->assertTrue($this->isPruned($cache, 'bar'));
  87. $this->assertTrue($this->isPruned($cache, 'baz'));
  88. $this->assertTrue($this->isPruned($cache, 'qux'));
  89. $cache->set('foo', 'foo-val');
  90. $cache->set('bar', 'bar-val', new \DateInterval('PT20S'));
  91. $cache->set('baz', 'baz-val', new \DateInterval('PT40S'));
  92. $cache->set('qux', 'qux-val', new \DateInterval('PT80S'));
  93. $cache->prune();
  94. $this->assertFalse($this->isPruned($cache, 'foo'));
  95. $this->assertFalse($this->isPruned($cache, 'bar'));
  96. $this->assertFalse($this->isPruned($cache, 'baz'));
  97. $this->assertFalse($this->isPruned($cache, 'qux'));
  98. sleep(30);
  99. $cache->prune();
  100. $this->assertFalse($this->isPruned($cache, 'foo'));
  101. $this->assertTrue($this->isPruned($cache, 'bar'));
  102. $this->assertFalse($this->isPruned($cache, 'baz'));
  103. $this->assertFalse($this->isPruned($cache, 'qux'));
  104. sleep(30);
  105. $cache->prune();
  106. $this->assertFalse($this->isPruned($cache, 'foo'));
  107. $this->assertTrue($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, 'qux'));
  113. $cache->clear();
  114. }
  115. protected function isPruned($cache, $name)
  116. {
  117. if (Psr16Cache::class !== \get_class($cache)) {
  118. $this->fail('Test classes for pruneable caches must implement `isPruned($cache, $name)` method.');
  119. }
  120. $pool = ((array) $cache)[sprintf("\0%s\0pool", Psr16Cache::class)];
  121. $getFileMethod = (new \ReflectionObject($pool))->getMethod('getFile');
  122. $getFileMethod->setAccessible(true);
  123. return !file_exists($getFileMethod->invoke($pool, $name));
  124. }
  125. }
  126. class NotUnserializable
  127. {
  128. public function __wakeup()
  129. {
  130. throw new \Exception(__CLASS__);
  131. }
  132. }