FilesystemCacheTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 CacheTest
  9. {
  10. /**
  11. * @var \Doctrine\Common\Cache\FilesystemCache
  12. */
  13. private $driver;
  14. protected function _getCacheDriver()
  15. {
  16. $dir = sys_get_temp_dir() . "/doctrine_cache_". uniqid();
  17. $this->assertFalse(is_dir($dir));
  18. $this->driver = new FilesystemCache($dir);
  19. $this->assertTrue(is_dir($dir));
  20. return $this->driver;
  21. }
  22. public function testLifetime()
  23. {
  24. $cache = $this->_getCacheDriver();
  25. // Test save
  26. $cache->save('test_key', 'testing this out', 10);
  27. // Test contains to test that save() worked
  28. $this->assertTrue($cache->contains('test_key'));
  29. // Test fetch
  30. $this->assertEquals('testing this out', $cache->fetch('test_key'));
  31. // access private methods
  32. $getFilename = new \ReflectionMethod($cache, 'getFilename');
  33. $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
  34. $getFilename->setAccessible(true);
  35. $getNamespacedId->setAccessible(true);
  36. $id = $getNamespacedId->invoke($cache, 'test_key');
  37. $filename = $getFilename->invoke($cache, $id);
  38. $data = '';
  39. $lifetime = 0;
  40. $resource = fopen($filename, "r");
  41. if (false !== ($line = fgets($resource))) {
  42. $lifetime = (integer) $line;
  43. }
  44. while (false !== ($line = fgets($resource))) {
  45. $data .= $line;
  46. }
  47. $this->assertNotEquals(0, $lifetime, "previous lifetime could not be loaded");
  48. // update lifetime
  49. $lifetime = $lifetime - 20;
  50. file_put_contents($filename, $lifetime . PHP_EOL . $data);
  51. // test expired data
  52. $this->assertFalse($cache->contains('test_key'));
  53. $this->assertFalse($cache->fetch('test_key'));
  54. }
  55. public function testGetStats()
  56. {
  57. $cache = $this->_getCacheDriver();
  58. $stats = $cache->getStats();
  59. $this->assertNull($stats[Cache::STATS_HITS]);
  60. $this->assertNull($stats[Cache::STATS_MISSES]);
  61. $this->assertNull($stats[Cache::STATS_UPTIME]);
  62. $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
  63. $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
  64. }
  65. public function tearDown()
  66. {
  67. $dir = $this->driver->getDirectory();
  68. $ext = $this->driver->getExtension();
  69. $iterator = new \RecursiveDirectoryIterator($dir);
  70. foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
  71. if ($file->isFile()) {
  72. @unlink($file->getRealPath());
  73. } else {
  74. @rmdir($file->getRealPath());
  75. }
  76. }
  77. }
  78. }