diff --git a/test/Twig/Tests/Cache/FilesystemTest.php b/test/Twig/Tests/Cache/FilesystemTest.php index 20e83efd5..541328dd6 100644 --- a/test/Twig/Tests/Cache/FilesystemTest.php +++ b/test/Twig/Tests/Cache/FilesystemTest.php @@ -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() { diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 76d528df3..e5ea8249a 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -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() diff --git a/test/Twig/Tests/Extension/SandboxTest.php b/test/Twig/Tests/Extension/SandboxTest.php index d21fb231b..bfaa62b04 100644 --- a/test/Twig/Tests/Extension/SandboxTest.php +++ b/test/Twig/Tests/Extension/SandboxTest.php @@ -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', diff --git a/test/Twig/Tests/FileCachingTest.php b/test/Twig/Tests/FileCachingTest.php index 14e0144c6..d9c02c077 100644 --- a/test/Twig/Tests/FileCachingTest.php +++ b/test/Twig/Tests/FileCachingTest.php @@ -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); - } } diff --git a/test/Twig/Tests/FilesystemHelper.php b/test/Twig/Tests/FilesystemHelper.php new file mode 100644 index 000000000..0e315e8f7 --- /dev/null +++ b/test/Twig/Tests/FilesystemHelper.php @@ -0,0 +1,30 @@ + $fileInfo) { + if ($iterator->isDot()) { + continue; + } + + if ($fileInfo->isDir()) { + rmdir($filename); + } else { + unlink($filename); + } + } + rmdir($dir); + } +} diff --git a/test/Twig/Tests/TokenStreamTest.php b/test/Twig/Tests/TokenStreamTest.php index fd4ec6337..5ac3a286f 100644 --- a/test/Twig/Tests/TokenStreamTest.php +++ b/test/Twig/Tests/TokenStreamTest.php @@ -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), diff --git a/test/Twig/Tests/escapingTest.php b/test/Twig/Tests/escapingTest.php index 7b765caee..abf623643 100644 --- a/test/Twig/Tests/escapingTest.php +++ b/test/Twig/Tests/escapingTest.php @@ -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')); }