]> git.immae.eu Git - github/wallabag/wallabag.git/blame - vendor/twig/twig/test/Twig/Tests/FileCachingTest.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / twig / twig / test / Twig / Tests / FileCachingTest.php
CommitLineData
4f5b44bd
NL
1<?php
2
3class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
4{
5 protected $fileName;
6 protected $env;
7 protected $tmpDir;
8
9 public function setUp()
10 {
11 $this->tmpDir = sys_get_temp_dir().'/TwigTests';
12 if (!file_exists($this->tmpDir)) {
13 @mkdir($this->tmpDir, 0777, true);
14 }
15
16 if (!is_writable($this->tmpDir)) {
17 $this->markTestSkipped(sprintf('Unable to run the tests as "%s" is not writable.', $this->tmpDir));
18 }
19
20 $this->env = new Twig_Environment(new Twig_Loader_String(), array('cache' => $this->tmpDir));
21 }
22
23 public function tearDown()
24 {
25 if ($this->fileName) {
26 unlink($this->fileName);
27 }
28
29 $this->removeDir($this->tmpDir);
30 }
31
32 public function testWritingCacheFiles()
33 {
34 $name = 'This is just text.';
35 $template = $this->env->loadTemplate($name);
36 $cacheFileName = $this->env->getCacheFilename($name);
37
38 $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
39 $this->fileName = $cacheFileName;
40 }
41
42 public function testClearingCacheFiles()
43 {
44 $name = 'I will be deleted.';
45 $template = $this->env->loadTemplate($name);
46 $cacheFileName = $this->env->getCacheFilename($name);
47
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.');
51 }
52
53 private function removeDir($target)
54 {
55 $fp = opendir($target);
56 while (false !== $file = readdir($fp)) {
57 if (in_array($file, array('.', '..'))) {
58 continue;
59 }
60
61 if (is_dir($target.'/'.$file)) {
62 self::removeDir($target.'/'.$file);
63 } else {
64 unlink($target.'/'.$file);
65 }
66 }
67 closedir($fp);
68 rmdir($target);
69 }
70}