ChainCacheTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 PHPUnit\Framework\MockObject\MockObject;
  12. use Psr\SimpleCache\CacheInterface;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. use Symfony\Component\Cache\Simple\ArrayCache;
  15. use Symfony\Component\Cache\Simple\ChainCache;
  16. use Symfony\Component\Cache\Simple\FilesystemCache;
  17. /**
  18. * @group time-sensitive
  19. */
  20. class ChainCacheTest extends CacheTestCase
  21. {
  22. public function createSimpleCache($defaultLifetime = 0)
  23. {
  24. return new ChainCache([new ArrayCache($defaultLifetime), new FilesystemCache('', $defaultLifetime)], $defaultLifetime);
  25. }
  26. public function testEmptyCachesException()
  27. {
  28. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  29. $this->expectExceptionMessage('At least one cache must be specified.');
  30. new ChainCache([]);
  31. }
  32. public function testInvalidCacheException()
  33. {
  34. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  35. $this->expectExceptionMessage('The class "stdClass" does not implement');
  36. new ChainCache([new \stdClass()]);
  37. }
  38. public function testPrune()
  39. {
  40. if (isset($this->skippedTests[__FUNCTION__])) {
  41. $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
  42. }
  43. $cache = new ChainCache([
  44. $this->getPruneableMock(),
  45. $this->getNonPruneableMock(),
  46. $this->getPruneableMock(),
  47. ]);
  48. $this->assertTrue($cache->prune());
  49. $cache = new ChainCache([
  50. $this->getPruneableMock(),
  51. $this->getFailingPruneableMock(),
  52. $this->getPruneableMock(),
  53. ]);
  54. $this->assertFalse($cache->prune());
  55. }
  56. /**
  57. * @return MockObject|PruneableCacheInterface
  58. */
  59. private function getPruneableMock()
  60. {
  61. $pruneable = $this
  62. ->getMockBuilder(PruneableCacheInterface::class)
  63. ->getMock();
  64. $pruneable
  65. ->expects($this->atLeastOnce())
  66. ->method('prune')
  67. ->willReturn(true);
  68. return $pruneable;
  69. }
  70. /**
  71. * @return MockObject|PruneableCacheInterface
  72. */
  73. private function getFailingPruneableMock()
  74. {
  75. $pruneable = $this
  76. ->getMockBuilder(PruneableCacheInterface::class)
  77. ->getMock();
  78. $pruneable
  79. ->expects($this->atLeastOnce())
  80. ->method('prune')
  81. ->willReturn(false);
  82. return $pruneable;
  83. }
  84. /**
  85. * @return MockObject|CacheInterface
  86. */
  87. private function getNonPruneableMock()
  88. {
  89. return $this
  90. ->getMockBuilder(CacheInterface::class)
  91. ->getMock();
  92. }
  93. }
  94. interface PruneableCacheInterface extends PruneableInterface, CacheInterface
  95. {
  96. }