ChainCacheTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\ApcCache;
  4. use Doctrine\Common\Cache\ArrayCache;
  5. use Doctrine\Common\Cache\ChainCache;
  6. class ChainCacheTest extends CacheTest
  7. {
  8. protected function _getCacheDriver()
  9. {
  10. return new ChainCache(array(new ArrayCache()));
  11. }
  12. public function testGetStats()
  13. {
  14. $cache = $this->_getCacheDriver();
  15. $stats = $cache->getStats();
  16. $this->assertInternalType('array', $stats);
  17. }
  18. public function testOnlyFetchFirstOne()
  19. {
  20. $cache1 = new ArrayCache();
  21. $cache2 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
  22. $cache2->expects($this->never())->method('doFetch');
  23. $chainCache = new ChainCache(array($cache1, $cache2));
  24. $chainCache->save('id', 'bar');
  25. $this->assertEquals('bar', $chainCache->fetch('id'));
  26. }
  27. public function testFetchPropagateToFastestCache()
  28. {
  29. $cache1 = new ArrayCache();
  30. $cache2 = new ArrayCache();
  31. $cache2->save('bar', 'value');
  32. $chainCache = new ChainCache(array($cache1, $cache2));
  33. $this->assertFalse($cache1->contains('bar'));
  34. $result = $chainCache->fetch('bar');
  35. $this->assertEquals('value', $result);
  36. $this->assertTrue($cache2->contains('bar'));
  37. }
  38. public function testNamespaceIsPropagatedToAllProviders()
  39. {
  40. $cache1 = new ArrayCache();
  41. $cache2 = new ArrayCache();
  42. $chainCache = new ChainCache(array($cache1, $cache2));
  43. $chainCache->setNamespace('bar');
  44. $this->assertEquals('bar', $cache1->getNamespace());
  45. $this->assertEquals('bar', $cache2->getNamespace());
  46. }
  47. public function testDeleteToAllProviders()
  48. {
  49. $cache1 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
  50. $cache2 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
  51. $cache1->expects($this->once())->method('doDelete');
  52. $cache2->expects($this->once())->method('doDelete');
  53. $chainCache = new ChainCache(array($cache1, $cache2));
  54. $chainCache->delete('bar');
  55. }
  56. public function testFlushToAllProviders()
  57. {
  58. $cache1 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
  59. $cache2 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
  60. $cache1->expects($this->once())->method('doFlush');
  61. $cache2->expects($this->once())->method('doFlush');
  62. $chainCache = new ChainCache(array($cache1, $cache2));
  63. $chainCache->flushAll();
  64. }
  65. protected function isSharedStorage()
  66. {
  67. return false;
  68. }
  69. }