ChainCacheTest.php 3.0 KB

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