ChainCacheTest.php 3.1 KB

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