ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
FilesystemTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 require_once dirname(dirname(__FILE__)).'/FilesystemHelper.php';
13 
14 class Twig_Tests_Cache_FilesystemTest extends \PHPUnit\Framework\TestCase
15 {
16  private $classname;
17  private $directory;
18  private $cache;
19 
20  protected function setUp()
21  {
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);
26  }
27 
28  protected function tearDown()
29  {
30  if (file_exists($this->directory)) {
32  }
33  }
34 
35  public function testLoad()
36  {
37  $key = $this->directory.'/cache/cachefile.php';
38 
39  $dir = dirname($key);
40  @mkdir($dir, 0777, true);
41  $this->assertTrue(is_dir($dir));
42  $this->assertFalse(class_exists($this->classname, false));
43 
44  $content = $this->generateSource();
45  file_put_contents($key, $content);
46 
47  $this->cache->load($key);
48 
49  $this->assertTrue(class_exists($this->classname, false));
50  }
51 
52  public function testLoadMissing()
53  {
54  $key = $this->directory.'/cache/cachefile.php';
55 
56  $this->assertFalse(class_exists($this->classname, false));
57 
58  $this->cache->load($key);
59 
60  $this->assertFalse(class_exists($this->classname, false));
61  }
62 
63  public function testWrite()
64  {
65  $key = $this->directory.'/cache/cachefile.php';
66  $content = $this->generateSource();
67 
68  $this->assertFileNotExists($key);
69  $this->assertFileNotExists($this->directory);
70 
71  $this->cache->write($key, $content);
72 
73  $this->assertFileExists($this->directory);
74  $this->assertFileExists($key);
75  $this->assertSame(file_get_contents($key), $content);
76  }
77 
82  public function testWriteFailMkdir()
83  {
84  if (defined('PHP_WINDOWS_VERSION_BUILD')) {
85  $this->markTestSkipped('Read-only directories not possible on Windows.');
86  }
87 
88  $key = $this->directory.'/cache/cachefile.php';
89  $content = $this->generateSource();
90 
91  $this->assertFileNotExists($key);
92 
93  // Create read-only root directory.
94  @mkdir($this->directory, 0555, true);
95  $this->assertTrue(is_dir($this->directory));
96 
97  $this->cache->write($key, $content);
98  }
99 
104  public function testWriteFailDirWritable()
105  {
106  if (defined('PHP_WINDOWS_VERSION_BUILD')) {
107  $this->markTestSkipped('Read-only directories not possible on Windows.');
108  }
109 
110  $key = $this->directory.'/cache/cachefile.php';
111  $content = $this->generateSource();
112 
113  $this->assertFileNotExists($key);
114 
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'));
120 
121  $this->cache->write($key, $content);
122  }
123 
128  public function testWriteFailWriteFile()
129  {
130  $key = $this->directory.'/cache/cachefile.php';
131  $content = $this->generateSource();
132 
133  $this->assertFileNotExists($key);
134 
135  // Create a directory in the place of the cache file.
136  @mkdir($key, 0777, true);
137  $this->assertTrue(is_dir($key));
138 
139  $this->cache->write($key, $content);
140  }
141 
142  public function testGetTimestamp()
143  {
144  $key = $this->directory.'/cache/cachefile.php';
145 
146  $dir = dirname($key);
147  @mkdir($dir, 0777, true);
148  $this->assertTrue(is_dir($dir));
149 
150  // Create the file with a specific modification time.
151  touch($key, 1234567890);
152 
153  $this->assertSame(1234567890, $this->cache->getTimestamp($key));
154  }
155 
156  public function testGetTimestampMissingFile()
157  {
158  $key = $this->directory.'/cache/cachefile.php';
159  $this->assertSame(0, $this->cache->getTimestamp($key));
160  }
161 
167  public function testGenerateKey($expected, $input)
168  {
170  $this->assertRegExp($expected, $cache->generateKey('_test_', get_class($this)));
171  }
172 
173  public function provideDirectories()
174  {
175  $pattern = '#a/b/[a-zA-Z0-9]+/[a-zA-Z0-9]+.php$#';
176 
177  return array(
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'),
184  );
185  }
186 
187  private function generateSource()
188  {
189  return strtr('<?php class {{classname}} {}', array(
190  '{{classname}}' => $this->classname,
191  ));
192  }
193 }
testWriteFailWriteFile()
RuntimeException Failed to write cache file
testGenerateKey($expected, $input)
Test file cache is tolerant towards trailing (back)slashes on the configured cache directory...
testWriteFailDirWritable()
RuntimeException Unable to write in the cache directory
Implements a cache on the filesystem.
Definition: Filesystem.php:17
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
$key
Definition: croninfo.php:18
testWriteFailMkdir()
RuntimeException Unable to create the cache directory