12require_once dirname(dirname(__FILE__)).
'/FilesystemHelper.php';
14class 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';
28 protected function tearDown()
30 if (file_exists($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);
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);
94 @mkdir($this->directory, 0555,
true);
95 $this->assertTrue(is_dir($this->directory));
97 $this->cache->write(
$key, $content);
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);
116 @mkdir($this->directory, 0777,
true);
118 @mkdir($this->directory.
'/cache', 0555);
119 $this->assertTrue(is_dir($this->directory.
'/cache'));
121 $this->cache->write(
$key, $content);
128 public function testWriteFailWriteFile()
130 $key = $this->directory.
'/cache/cachefile.php';
131 $content = $this->generateSource();
133 $this->assertFileNotExists(
$key);
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));
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));
167 public function testGenerateKey($expected, $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,
An exception for terminatinating execution or to throw for unit testing.
Implements a cache on the filesystem.
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'