ChainAdapterTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 PHPUnit\Framework\MockObject\MockObject;
  12. use Symfony\Component\Cache\Adapter\AdapterInterface;
  13. use Symfony\Component\Cache\Adapter\ArrayAdapter;
  14. use Symfony\Component\Cache\Adapter\ChainAdapter;
  15. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  16. use Symfony\Component\Cache\PruneableInterface;
  17. use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter;
  18. /**
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. * @group time-sensitive
  21. */
  22. class ChainAdapterTest extends AdapterTestCase
  23. {
  24. public function createCachePool($defaultLifetime = 0)
  25. {
  26. return new ChainAdapter([new ArrayAdapter($defaultLifetime), new ExternalAdapter($defaultLifetime), new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
  27. }
  28. public function testEmptyAdaptersException()
  29. {
  30. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  31. $this->expectExceptionMessage('At least one adapter must be specified.');
  32. new ChainAdapter([]);
  33. }
  34. public function testInvalidAdapterException()
  35. {
  36. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  37. $this->expectExceptionMessage('The class "stdClass" does not implement');
  38. new ChainAdapter([new \stdClass()]);
  39. }
  40. public function testPrune()
  41. {
  42. if (isset($this->skippedTests[__FUNCTION__])) {
  43. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  44. }
  45. $cache = new ChainAdapter([
  46. $this->getPruneableMock(),
  47. $this->getNonPruneableMock(),
  48. $this->getPruneableMock(),
  49. ]);
  50. $this->assertTrue($cache->prune());
  51. $cache = new ChainAdapter([
  52. $this->getPruneableMock(),
  53. $this->getFailingPruneableMock(),
  54. $this->getPruneableMock(),
  55. ]);
  56. $this->assertFalse($cache->prune());
  57. }
  58. public function testMultipleCachesExpirationWhenCommonTtlIsNotSet()
  59. {
  60. if (isset($this->skippedTests[__FUNCTION__])) {
  61. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  62. }
  63. $adapter1 = new ArrayAdapter(4);
  64. $adapter2 = new ArrayAdapter(2);
  65. $cache = new ChainAdapter([$adapter1, $adapter2]);
  66. $cache->save($cache->getItem('key')->set('value'));
  67. $item = $adapter1->getItem('key');
  68. $this->assertTrue($item->isHit());
  69. $this->assertEquals('value', $item->get());
  70. $item = $adapter2->getItem('key');
  71. $this->assertTrue($item->isHit());
  72. $this->assertEquals('value', $item->get());
  73. sleep(2);
  74. $item = $adapter1->getItem('key');
  75. $this->assertTrue($item->isHit());
  76. $this->assertEquals('value', $item->get());
  77. $item = $adapter2->getItem('key');
  78. $this->assertFalse($item->isHit());
  79. sleep(2);
  80. $item = $adapter1->getItem('key');
  81. $this->assertFalse($item->isHit());
  82. $adapter2->save($adapter2->getItem('key1')->set('value1'));
  83. $item = $cache->getItem('key1');
  84. $this->assertTrue($item->isHit());
  85. $this->assertEquals('value1', $item->get());
  86. sleep(2);
  87. $item = $adapter1->getItem('key1');
  88. $this->assertTrue($item->isHit());
  89. $this->assertEquals('value1', $item->get());
  90. $item = $adapter2->getItem('key1');
  91. $this->assertFalse($item->isHit());
  92. sleep(2);
  93. $item = $adapter1->getItem('key1');
  94. $this->assertFalse($item->isHit());
  95. }
  96. public function testMultipleCachesExpirationWhenCommonTtlIsSet()
  97. {
  98. if (isset($this->skippedTests[__FUNCTION__])) {
  99. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  100. }
  101. $adapter1 = new ArrayAdapter(4);
  102. $adapter2 = new ArrayAdapter(2);
  103. $cache = new ChainAdapter([$adapter1, $adapter2], 6);
  104. $cache->save($cache->getItem('key')->set('value'));
  105. $item = $adapter1->getItem('key');
  106. $this->assertTrue($item->isHit());
  107. $this->assertEquals('value', $item->get());
  108. $item = $adapter2->getItem('key');
  109. $this->assertTrue($item->isHit());
  110. $this->assertEquals('value', $item->get());
  111. sleep(2);
  112. $item = $adapter1->getItem('key');
  113. $this->assertTrue($item->isHit());
  114. $this->assertEquals('value', $item->get());
  115. $item = $adapter2->getItem('key');
  116. $this->assertFalse($item->isHit());
  117. sleep(2);
  118. $item = $adapter1->getItem('key');
  119. $this->assertFalse($item->isHit());
  120. $adapter2->save($adapter2->getItem('key1')->set('value1'));
  121. $item = $cache->getItem('key1');
  122. $this->assertTrue($item->isHit());
  123. $this->assertEquals('value1', $item->get());
  124. sleep(2);
  125. $item = $adapter1->getItem('key1');
  126. $this->assertTrue($item->isHit());
  127. $this->assertEquals('value1', $item->get());
  128. $item = $adapter2->getItem('key1');
  129. $this->assertFalse($item->isHit());
  130. sleep(2);
  131. $item = $adapter1->getItem('key1');
  132. $this->assertTrue($item->isHit());
  133. $this->assertEquals('value1', $item->get());
  134. sleep(2);
  135. $item = $adapter1->getItem('key1');
  136. $this->assertFalse($item->isHit());
  137. }
  138. /**
  139. * @return MockObject|PruneableCacheInterface
  140. */
  141. private function getPruneableMock()
  142. {
  143. $pruneable = $this
  144. ->getMockBuilder(PruneableCacheInterface::class)
  145. ->getMock();
  146. $pruneable
  147. ->expects($this->atLeastOnce())
  148. ->method('prune')
  149. ->willReturn(true);
  150. return $pruneable;
  151. }
  152. /**
  153. * @return MockObject|PruneableCacheInterface
  154. */
  155. private function getFailingPruneableMock()
  156. {
  157. $pruneable = $this
  158. ->getMockBuilder(PruneableCacheInterface::class)
  159. ->getMock();
  160. $pruneable
  161. ->expects($this->atLeastOnce())
  162. ->method('prune')
  163. ->willReturn(false);
  164. return $pruneable;
  165. }
  166. /**
  167. * @return MockObject|AdapterInterface
  168. */
  169. private function getNonPruneableMock()
  170. {
  171. return $this
  172. ->getMockBuilder(AdapterInterface::class)
  173. ->getMock();
  174. }
  175. }
  176. interface PruneableCacheInterface extends PruneableInterface, AdapterInterface
  177. {
  178. }