FilesystemCacheTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. protected function _getCacheDriver()
  54. {
  55. return new FilesystemCache($this->directory);
  56. }
  57. }