3 namespace Doctrine\Tests\Common\Cache;
5 use Doctrine\Common\Cache\FilesystemCache;
10 class FilesystemCacheTest extends CacheTest
13 * @var \Doctrine\Common\Cache\FilesystemCache
17 protected function _getCacheDriver()
19 $dir = sys_get_temp_dir() . "/doctrine_cache_". uniqid();
20 $this->assertFalse(is_dir($dir));
22 $this->driver = new FilesystemCache($dir);
23 $this->assertTrue(is_dir($dir));
28 public function testLifetime()
30 $cache = $this->_getCacheDriver();
33 $cache->save('test_key', 'testing this out', 10);
35 // Test contains to test that save() worked
36 $this->assertTrue($cache->contains('test_key'));
39 $this->assertEquals('testing this out', $cache->fetch('test_key'));
41 // access private methods
42 $getFilename = new \ReflectionMethod($cache, 'getFilename');
43 $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
45 $getFilename->setAccessible(true);
46 $getNamespacedId->setAccessible(true);
48 $id = $getNamespacedId->invoke($cache, 'test_key');
49 $filename = $getFilename->invoke($cache, $id);
53 $resource = fopen($filename, "r");
55 if (false !== ($line = fgets($resource))) {
56 $lifetime = (integer) $line;
59 while (false !== ($line = fgets($resource))) {
63 $this->assertNotEquals(0, $lifetime, "previous lifetime could not be loaded");
66 $lifetime = $lifetime - 20;
67 file_put_contents($filename, $lifetime . PHP_EOL . $data);
70 $this->assertFalse($cache->contains('test_key'));
71 $this->assertFalse($cache->fetch('test_key'));
74 public function testGetStats()
76 $cache = $this->_getCacheDriver();
77 $stats = $cache->getStats();
79 $this->assertNull($stats);
82 public function tearDown()
84 $dir = $this->driver->getDirectory();
85 $ext = $this->driver->getExtension();
86 $iterator = new \RecursiveDirectoryIterator($dir);
88 foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
89 if ($file->isFile()) {
90 @unlink($file->getRealPath());
92 @rmdir($file->getRealPath());