ChainAdapterTest.php 3.2 KB

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