Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / tests / Doctrine / Tests / Common / Cache / FilesystemCacheTest.php
1 <?php
2
3 namespace Doctrine\Tests\Common\Cache;
4
5 use Doctrine\Common\Cache\FilesystemCache;
6
7 /**
8  * @group DCOM-101
9  */
10 class FilesystemCacheTest extends CacheTest
11 {
12     /**
13      * @var \Doctrine\Common\Cache\FilesystemCache
14      */
15     private $driver;
16
17     protected function _getCacheDriver()
18     {
19         $dir = sys_get_temp_dir() . "/doctrine_cache_". uniqid();
20         $this->assertFalse(is_dir($dir));
21         
22         $this->driver = new FilesystemCache($dir);
23         $this->assertTrue(is_dir($dir));
24
25         return $this->driver;
26     }
27
28     public function testLifetime()
29     {
30         $cache = $this->_getCacheDriver();
31
32         // Test save
33         $cache->save('test_key', 'testing this out', 10);
34
35         // Test contains to test that save() worked
36         $this->assertTrue($cache->contains('test_key'));
37
38         // Test fetch
39         $this->assertEquals('testing this out', $cache->fetch('test_key'));
40
41         // access private methods
42         $getFilename        = new \ReflectionMethod($cache, 'getFilename');
43         $getNamespacedId    = new \ReflectionMethod($cache, 'getNamespacedId');
44
45         $getFilename->setAccessible(true);
46         $getNamespacedId->setAccessible(true);
47
48         $id         = $getNamespacedId->invoke($cache, 'test_key');
49         $filename   = $getFilename->invoke($cache, $id);
50
51         $data       = '';
52         $lifetime   = 0;
53         $resource   = fopen($filename, "r");
54
55         if (false !== ($line = fgets($resource))) {
56             $lifetime = (integer) $line;
57         }
58
59         while (false !== ($line = fgets($resource))) {
60             $data .= $line;
61         }
62
63         $this->assertNotEquals(0, $lifetime, "previous lifetime could not be loaded");
64
65         // update lifetime
66         $lifetime = $lifetime - 20;
67         file_put_contents($filename, $lifetime . PHP_EOL . $data);
68
69         // test expired data
70         $this->assertFalse($cache->contains('test_key'));
71         $this->assertFalse($cache->fetch('test_key'));
72     }
73
74     public function testGetStats()
75     {
76         $cache = $this->_getCacheDriver();
77         $stats = $cache->getStats();
78
79         $this->assertNull($stats);
80     }
81
82     public function tearDown()
83     {
84         $dir        = $this->driver->getDirectory();
85         $ext        = $this->driver->getExtension();
86         $iterator   = new \RecursiveDirectoryIterator($dir);
87
88         foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
89             if ($file->isFile()) {
90                 @unlink($file->getRealPath());
91             } else {
92                 @rmdir($file->getRealPath());
93             }
94         }
95     }
96
97 }