Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / tests / Doctrine / Tests / Common / Cache / PhpFileCacheTest.php
1 <?php
2
3 namespace Doctrine\Tests\Common\Cache;
4
5 use Doctrine\Common\Cache\PhpFileCache;
6
7 /**
8  * @group DCOM-101
9  */
10 class PhpFileCacheTest extends CacheTest
11 {
12     /**
13      * @var \Doctrine\Common\Cache\PhpFileCache
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 PhpFileCache($dir);
23         $this->assertTrue(is_dir($dir));
24
25         return $this->driver;
26     }
27
28     public function testObjects()
29     {
30         $this->markTestSkipped('PhpFileCache does not support saving objects that dont implement __set_state()');
31     }
32
33     public function testLifetime()
34     {
35         $cache = $this->_getCacheDriver();
36
37         // Test save
38         $cache->save('test_key', 'testing this out', 10);
39
40         // Test contains to test that save() worked
41         $this->assertTrue($cache->contains('test_key'));
42
43         // Test fetch
44         $this->assertEquals('testing this out', $cache->fetch('test_key'));
45
46         // access private methods
47         $getFilename        = new \ReflectionMethod($cache, 'getFilename');
48         $getNamespacedId    = new \ReflectionMethod($cache, 'getNamespacedId');
49
50         $getFilename->setAccessible(true);
51         $getNamespacedId->setAccessible(true);
52
53         $id     = $getNamespacedId->invoke($cache, 'test_key');
54         $path   = $getFilename->invoke($cache, $id);
55         $value  = include $path;
56
57         // update lifetime
58         $value['lifetime'] = $value['lifetime'] - 20;
59         file_put_contents($path, '<?php return unserialize(' . var_export(serialize($value), true) . ');');
60
61         // test expired data
62         $this->assertFalse($cache->contains('test_key'));
63         $this->assertFalse($cache->fetch('test_key'));
64     }
65
66     public function testImplementsSetState()
67     {
68         $cache = $this->_getCacheDriver();
69
70         // Test save
71         $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
72
73         //Test __set_state call
74         $this->assertCount(0, SetStateClass::$values);
75
76         // Test fetch
77         $value = $cache->fetch('test_set_state');
78         $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value);
79         $this->assertEquals(array(1,2,3), $value->getValue());
80
81         //Test __set_state call
82         $this->assertCount(1, SetStateClass::$values);
83
84         // Test contains
85         $this->assertTrue($cache->contains('test_set_state'));
86     }
87
88     public function testNotImplementsSetState()
89     {
90         $cache = $this->_getCacheDriver();
91
92         $this->setExpectedException('InvalidArgumentException');
93         $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
94     }
95
96     public function testGetStats()
97     {
98         $cache = $this->_getCacheDriver();
99         $stats = $cache->getStats();
100
101         $this->assertNull($stats);
102     }
103
104     public function tearDown()
105     {
106         if (!$this->driver) {
107             return;
108         }
109
110         $dir        = $this->driver->getDirectory();
111         $ext        = $this->driver->getExtension();
112         $iterator   = new \RecursiveDirectoryIterator($dir);
113
114         foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
115             if ($file->isFile()) {
116                 @unlink($file->getRealPath());
117             } else {
118                 @rmdir($file->getRealPath());
119             }
120         }
121     }
122
123 }
124
125 class NotSetStateClass
126 {
127     private $value;
128
129     public function __construct($value)
130     {
131         $this->value = $value;
132     }
133
134     public function getValue()
135     {
136         return $this->value;
137     }
138 }
139
140 class SetStateClass extends NotSetStateClass
141 {
142     public static $values = array();
143
144     public static function __set_state($data)
145     {
146         self::$values = $data;
147         return new self($data['value']);
148     }
149 }