4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 require_once dirname(dirname(__FILE__)).'/FilesystemHelper.php';
14 class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
20 protected function setUp()
22 $nonce = hash('sha256', uniqid(mt_rand(), true));
23 $this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$nonce;
24 $this->directory = sys_get_temp_dir().'/twig-test';
25 $this->cache = new Twig_Cache_Filesystem($this->directory);
28 protected function tearDown()
30 if (file_exists($this->directory)) {
31 Twig_Tests_FilesystemHelper::removeDir($this->directory);
35 public function testLoad()
37 $key = $this->directory.'/cache/cachefile.php';
40 @mkdir($dir, 0777, true);
41 $this->assertTrue(is_dir($dir));
42 $this->assertFalse(class_exists($this->classname, false));
44 $content = $this->generateSource();
45 file_put_contents($key, $content);
47 $this->cache->load($key);
49 $this->assertTrue(class_exists($this->classname, false));
52 public function testLoadMissing()
54 $key = $this->directory.'/cache/cachefile.php';
56 $this->assertFalse(class_exists($this->classname, false));
58 $this->cache->load($key);
60 $this->assertFalse(class_exists($this->classname, false));
63 public function testWrite()
65 $key = $this->directory.'/cache/cachefile.php';
66 $content = $this->generateSource();
68 $this->assertFileNotExists($key);
69 $this->assertFileNotExists($this->directory);
71 $this->cache->write($key, $content);
73 $this->assertFileExists($this->directory);
74 $this->assertFileExists($key);
75 $this->assertSame(file_get_contents($key), $content);
79 * @expectedException RuntimeException
80 * @expectedExceptionMessage Unable to create the cache directory
82 public function testWriteFailMkdir()
84 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
85 $this->markTestSkipped('Read-only directories not possible on Windows.');
88 $key = $this->directory.'/cache/cachefile.php';
89 $content = $this->generateSource();
91 $this->assertFileNotExists($key);
93 // Create read-only root directory.
94 @mkdir($this->directory, 0555, true);
95 $this->assertTrue(is_dir($this->directory));
97 $this->cache->write($key, $content);
101 * @expectedException RuntimeException
102 * @expectedExceptionMessage Unable to write in the cache directory
104 public function testWriteFailDirWritable()
106 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
107 $this->markTestSkipped('Read-only directories not possible on Windows.');
110 $key = $this->directory.'/cache/cachefile.php';
111 $content = $this->generateSource();
113 $this->assertFileNotExists($key);
115 // Create root directory.
116 @mkdir($this->directory, 0777, true);
117 // Create read-only subdirectory.
118 @mkdir($this->directory.'/cache', 0555);
119 $this->assertTrue(is_dir($this->directory.'/cache'));
121 $this->cache->write($key, $content);
125 * @expectedException RuntimeException
126 * @expectedExceptionMessage Failed to write cache file
128 public function testWriteFailWriteFile()
130 $key = $this->directory.'/cache/cachefile.php';
131 $content = $this->generateSource();
133 $this->assertFileNotExists($key);
135 // Create a directory in the place of the cache file.
136 @mkdir($key, 0777, true);
137 $this->assertTrue(is_dir($key));
139 $this->cache->write($key, $content);
142 public function testGetTimestamp()
144 $key = $this->directory.'/cache/cachefile.php';
146 $dir = dirname($key);
147 @mkdir($dir, 0777, true);
148 $this->assertTrue(is_dir($dir));
150 // Create the file with a specific modification time.
151 touch($key, 1234567890);
153 $this->assertSame(1234567890, $this->cache->getTimestamp($key));
156 public function testGetTimestampMissingFile()
158 $key = $this->directory.'/cache/cachefile.php';
159 $this->assertSame(0, $this->cache->getTimestamp($key));
163 * Test file cache is tolerant towards trailing (back)slashes on the configured cache directory.
165 * @dataProvider provideDirectories
167 public function testGenerateKey($expected, $input)
169 $cache = new Twig_Cache_Filesystem($input);
170 $this->assertRegExp($expected, $cache->generateKey('_test_', get_class($this)));
173 public function provideDirectories()
175 $pattern = '#a/b/[a-zA-Z0-9]+/[a-zA-Z0-9]+.php$#';
178 array($pattern, 'a/b'),
179 array($pattern, 'a/b/'),
180 array($pattern, 'a/b\\'),
181 array($pattern, 'a/b\\/'),
182 array($pattern, 'a/b\\//'),
183 array('#/'.substr($pattern, 1), '/a/b'),
187 private function generateSource()
189 return strtr('<?php class {{classname}} {}', array(
190 '{{classname}}' => $this->classname,