3 class Twig_Tests_FileCachingTest
extends PHPUnit_Framework_TestCase
9 public function setUp()
11 $this->tmpDir
= sys_get_temp_dir().'/TwigTests';
12 if (!file_exists($this->tmpDir
)) {
13 @mkdir($this->tmpDir
, 0777, true);
16 if (!is_writable($this->tmpDir
)) {
17 $this->markTestSkipped(sprintf('Unable to run the tests as "%s" is not writable.', $this->tmpDir
));
20 $this->env
= new Twig_Environment(new Twig_Loader_String(), array('cache' => $this->tmpDir
));
23 public function tearDown()
25 if ($this->fileName
) {
26 unlink($this->fileName
);
29 $this->removeDir($this->tmpDir
);
32 public function testWritingCacheFiles()
34 $name = 'This is just text.';
35 $template = $this->env
->loadTemplate($name);
36 $cacheFileName = $this->env
->getCacheFilename($name);
38 $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
39 $this->fileName
= $cacheFileName;
42 public function testClearingCacheFiles()
44 $name = 'I will be deleted.';
45 $template = $this->env
->loadTemplate($name);
46 $cacheFileName = $this->env
->getCacheFilename($name);
48 $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
49 $this->env
->clearCacheFiles();
50 $this->assertFalse(file_exists($cacheFileName), 'Cache file was not cleared.');
53 private function removeDir($target)
55 $fp = opendir($target);
56 while (false !== $file = readdir($fp)) {
57 if (in_array($file, array('.', '..'))) {
61 if (is_dir($target.'/'.$file)) {
62 self
::removeDir($target.'/'.$file);
64 unlink($target.'/'.$file);