skip read-only dir tests on windows and cleanup tmp dirs correctly

This commit is contained in:
Tobias Schultze
2015-12-21 04:25:29 +01:00
committed by Fabien Potencier
parent db93842e80
commit c6601a15c0
7 changed files with 61 additions and 53 deletions
+17 -18
View File
@@ -9,35 +9,26 @@
* file that was distributed with this source code.
*/
require_once dirname(dirname(__FILE__)).'/FilesystemHelper.php';
class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
{
private $nonce;
private $classname;
private $directory;
private $cache;
protected function setUp()
{
$this->nonce = hash('sha256', uniqid(mt_rand(), true));
$this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$this->nonce;
$this->directory = sys_get_temp_dir().'/twig-test-'.$this->nonce;
$nonce = hash('sha256', uniqid(mt_rand(), true));
$this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$nonce;
$this->directory = sys_get_temp_dir().'/twig-test';
$this->cache = new Twig_Cache_Filesystem($this->directory);
}
protected function tearDown()
{
if (file_exists($this->directory)) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $filename => $fileInfo) {
if (!$iterator->isDot()) {
if ($fileInfo->isDir()) {
rmdir($filename);
} else {
unlink($filename);
}
}
}
rmdir($this->directory);
Twig_Tests_FilesystemHelper::removeDir($this->directory);
}
}
@@ -86,10 +77,14 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
/**
* @expectedException RuntimeException
* @expectedExceptionMessageRegExp #^Unable to create the cache directory #
* @expectedExceptionMessage Unable to create the cache directory
*/
public function testWriteFailMkdir()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->markTestSkipped('Read-only directories not possible on Windows.');
}
$key = $this->directory.'/cache/cachefile.php';
$content = $this->generateSource();
@@ -104,10 +99,14 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
/**
* @expectedException RuntimeException
* @expectedExceptionMessageRegExp #^Unable to write in the cache directory #
* @expectedExceptionMessage Unable to write in the cache directory
*/
public function testWriteFailDirWritable()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->markTestSkipped('Read-only directories not possible on Windows.');
}
$key = $this->directory.'/cache/cachefile.php';
$content = $this->generateSource();
@@ -124,7 +123,7 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
/**
* @expectedException RuntimeException
* @expectedExceptionMessageRegExp #^Failed to write cache file #
* @expectedExceptionMessage Failed to write cache file
*/
public function testWriteFailWriteFile()
{
+4 -3
View File
@@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/
require_once dirname(__FILE__).'/FilesystemHelper.php';
class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
{
private $deprecations = array();
@@ -154,8 +156,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
{
$uid = function_exists('posix_getuid') ? posix_getuid() : '';
$cache = new Twig_Cache_Filesystem($dir = sys_get_temp_dir().'/twig'.$uid);
$cache = new Twig_Cache_Filesystem($dir = sys_get_temp_dir().'/twig');
$options = array('cache' => $cache, 'auto_reload' => false, 'debug' => false);
// force compilation
@@ -178,7 +179,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
$output = $twig->render('index', array('foo' => 'bar'));
$this->assertEquals('bar', $output);
unlink($key);
Twig_Tests_FilesystemHelper::removeDir($dir);
}
public function testAutoReloadCacheMiss()
+1 -1
View File
@@ -13,7 +13,7 @@ class Twig_Tests_Extension_SandboxTest extends PHPUnit_Framework_TestCase
{
protected static $params, $templates;
public function setUp()
protected function setUp()
{
self::$params = array(
'name' => 'Fabien',
+7 -29
View File
@@ -9,13 +9,14 @@
* file that was distributed with this source code.
*/
require_once dirname(__FILE__).'/FilesystemHelper.php';
class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
{
protected $fileName;
protected $env;
protected $tmpDir;
private $env;
private $tmpDir;
public function setUp()
protected function setUp()
{
$this->tmpDir = sys_get_temp_dir().'/TwigTests';
if (!file_exists($this->tmpDir)) {
@@ -29,13 +30,9 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
$this->env = new Twig_Environment(new Twig_Loader_Array(array('index' => 'index', 'index2' => 'index2')), array('cache' => $this->tmpDir));
}
public function tearDown()
protected function tearDown()
{
if ($this->fileName) {
unlink($this->fileName);
}
$this->removeDir($this->tmpDir);
Twig_Tests_FilesystemHelper::removeDir($this->tmpDir);
}
/**
@@ -48,7 +45,6 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
$cacheFileName = $this->env->getCacheFilename($name);
$this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
$this->fileName = $cacheFileName;
}
/**
@@ -64,22 +60,4 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
$this->env->clearCacheFiles();
$this->assertFalse(file_exists($cacheFileName), 'Cache file was not cleared.');
}
private function removeDir($target)
{
$fp = opendir($target);
while (false !== $file = readdir($fp)) {
if (in_array($file, array('.', '..'))) {
continue;
}
if (is_dir($target.'/'.$file)) {
self::removeDir($target.'/'.$file);
} else {
unlink($target.'/'.$file);
}
}
closedir($fp);
rmdir($target);
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Tests_FilesystemHelper
{
public static function removeDir($dir)
{
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, PHP_VERSION_ID < 50300 ? 0 : FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $filename => $fileInfo) {
if ($iterator->isDot()) {
continue;
}
if ($fileInfo->isDir()) {
rmdir($filename);
} else {
unlink($filename);
}
}
rmdir($dir);
}
}
+1 -1
View File
@@ -13,7 +13,7 @@ class Twig_Tests_TokenStreamTest extends PHPUnit_Framework_TestCase
{
protected static $tokens;
public function setUp()
protected function setUp()
{
self::$tokens = array(
new Twig_Token(Twig_Token::TEXT_TYPE, 1, 1),
+1 -1
View File
@@ -144,7 +144,7 @@ class Twig_Test_EscapingTest extends PHPUnit_Framework_TestCase
protected $env;
public function setUp()
protected function setUp()
{
$this->env = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
}