FilesystemCacheTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\Cache;
  4. use Doctrine\Common\Cache\FilesystemCache;
  5. /**
  6. * @group DCOM-101
  7. */
  8. class FilesystemCacheTest extends BaseFileCacheTest
  9. {
  10. public function testLifetime()
  11. {
  12. $cache = $this->_getCacheDriver();
  13. // Test save
  14. $cache->save('test_key', 'testing this out', 10);
  15. // Test contains to test that save() worked
  16. $this->assertTrue($cache->contains('test_key'));
  17. // Test fetch
  18. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  19. // access private methods
  20. $getFilename = new \ReflectionMethod($cache, 'getFilename');
  21. $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
  22. $getFilename->setAccessible(true);
  23. $getNamespacedId->setAccessible(true);
  24. $id = $getNamespacedId->invoke($cache, 'test_key');
  25. $filename = $getFilename->invoke($cache, $id);
  26. $data = '';
  27. $lifetime = 0;
  28. $resource = fopen($filename, "r");
  29. if (false !== ($line = fgets($resource))) {
  30. $lifetime = (integer) $line;
  31. }
  32. while (false !== ($line = fgets($resource))) {
  33. $data .= $line;
  34. }
  35. $this->assertNotEquals(0, $lifetime, 'previous lifetime could not be loaded');
  36. // update lifetime
  37. $lifetime = $lifetime - 20;
  38. file_put_contents($filename, $lifetime . PHP_EOL . $data);
  39. // test expired data
  40. $this->assertFalse($cache->contains('test_key'));
  41. $this->assertFalse($cache->fetch('test_key'));
  42. }
  43. public function testGetStats()
  44. {
  45. $cache = $this->_getCacheDriver();
  46. $stats = $cache->getStats();
  47. $this->assertNull($stats[Cache::STATS_HITS]);
  48. $this->assertNull($stats[Cache::STATS_MISSES]);
  49. $this->assertNull($stats[Cache::STATS_UPTIME]);
  50. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  51. $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
  52. }
  53. public function testCacheInSharedDirectoryIsPerExtension()
  54. {
  55. $cache1 = new FilesystemCache($this->directory, '.foo');
  56. $cache2 = new FilesystemCache($this->directory, '.bar');
  57. $this->assertTrue($cache1->save('key1', 11));
  58. $this->assertTrue($cache1->save('key2', 12));
  59. $this->assertTrue($cache2->save('key1', 21));
  60. $this->assertTrue($cache2->save('key2', 22));
  61. $this->assertSame(11, $cache1->fetch('key1'), 'Cache value must not be influenced by a different cache in the same directory but different extension');
  62. $this->assertSame(12, $cache1->fetch('key2'));
  63. $this->assertTrue($cache1->flushAll());
  64. $this->assertFalse($cache1->fetch('key1'), 'flushAll() must delete all items with the current extension');
  65. $this->assertFalse($cache1->fetch('key2'));
  66. $this->assertSame(21, $cache2->fetch('key1'), 'flushAll() must not remove items with a different extension in a shared directory');
  67. $this->assertSame(22, $cache2->fetch('key2'));
  68. }
  69. public function testFlushAllWithNoExtension()
  70. {
  71. $cache = new FilesystemCache($this->directory, '');
  72. $this->assertTrue($cache->save('key1', 1));
  73. $this->assertTrue($cache->save('key2', 2));
  74. $this->assertTrue($cache->flushAll());
  75. $this->assertFalse($cache->contains('key1'));
  76. $this->assertFalse($cache->contains('key2'));
  77. }
  78. protected function _getCacheDriver()
  79. {
  80. return new FilesystemCache($this->directory);
  81. }
  82. }