3 namespace Doctrine\Tests\Common\Cache;
5 use Doctrine\Common\Cache\PhpFileCache;
10 class PhpFileCacheTest extends CacheTest
13 * @var \Doctrine\Common\Cache\PhpFileCache
17 protected function _getCacheDriver()
19 $dir = sys_get_temp_dir() . "/doctrine_cache_". uniqid();
20 $this->assertFalse(is_dir($dir));
22 $this->driver = new PhpFileCache($dir);
23 $this->assertTrue(is_dir($dir));
28 public function testObjects()
30 $this->markTestSkipped('PhpFileCache does not support saving objects that dont implement __set_state()');
33 public function testLifetime()
35 $cache = $this->_getCacheDriver();
38 $cache->save('test_key', 'testing this out', 10);
40 // Test contains to test that save() worked
41 $this->assertTrue($cache->contains('test_key'));
44 $this->assertEquals('testing this out', $cache->fetch('test_key'));
46 // access private methods
47 $getFilename = new \ReflectionMethod($cache, 'getFilename');
48 $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId');
50 $getFilename->setAccessible(true);
51 $getNamespacedId->setAccessible(true);
53 $id = $getNamespacedId->invoke($cache, 'test_key');
54 $path = $getFilename->invoke($cache, $id);
55 $value = include $path;
58 $value['lifetime'] = $value['lifetime'] - 20;
59 file_put_contents($path, '<?php return unserialize(' . var_export(serialize($value), true) . ');');
62 $this->assertFalse($cache->contains('test_key'));
63 $this->assertFalse($cache->fetch('test_key'));
66 public function testImplementsSetState()
68 $cache = $this->_getCacheDriver();
71 $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
73 //Test __set_state call
74 $this->assertCount(0, SetStateClass::$values);
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());
81 //Test __set_state call
82 $this->assertCount(1, SetStateClass::$values);
85 $this->assertTrue($cache->contains('test_set_state'));
88 public function testNotImplementsSetState()
90 $cache = $this->_getCacheDriver();
92 $this->setExpectedException('InvalidArgumentException');
93 $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
96 public function testGetStats()
98 $cache = $this->_getCacheDriver();
99 $stats = $cache->getStats();
101 $this->assertNull($stats);
104 public function tearDown()
106 if (!$this->driver) {
110 $dir = $this->driver->getDirectory();
111 $ext = $this->driver->getExtension();
112 $iterator = new \RecursiveDirectoryIterator($dir);
114 foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
115 if ($file->isFile()) {
116 @unlink($file->getRealPath());
118 @rmdir($file->getRealPath());
125 class NotSetStateClass
129 public function __construct($value)
131 $this->value = $value;
134 public function getValue()
140 class SetStateClass extends NotSetStateClass
142 public static $values = array();
144 public static function __set_state($data)
146 self::$values = $data;
147 return new self($data['value']);