added some cleanup in unit tests
authorFabien Potencier <fabien.potencier@gmail.com>
Sun, 27 Mar 2011 15:50:50 +0000 (17:50 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sun, 27 Mar 2011 15:50:50 +0000 (17:50 +0200)
test/Twig/Tests/FileCachingTest.php

index dd4a892..ee37952 100644 (file)
@@ -8,14 +8,18 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
 
     function setUp()
     {
-        $this->tmpDir = sys_get_temp_dir();
+        $this->tmpDir = sys_get_temp_dir().'/TwigCache';
+        if (!file_exists($this->tmpDir)) {
+            @mkdir($this->tmpDir, 0777, true);;
+        }
+
         if (!is_writable($this->tmpDir)) {
             $this->markTestSkipped(sprintf('Cannot write to %s, cannot test file caching.', $this->tmpDir));
         }
         $this->env = new Twig_Environment(new Twig_Loader_String(), array('cache' => $this->tmpDir));
         parent::setUp();
     }
-    
+
     function testWritingCacheFiles()
     {
         $name = 'This is just text.';
@@ -25,7 +29,7 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
         $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
         $this->fileName = $cacheFileName;
     }
-    
+
     function testClearingCacheFiles()
     {
         $name = 'I will be deleted.';
@@ -39,9 +43,28 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
 
     function tearDown()
     {
-        if($this->fileName) {
+        if ($this->fileName) {
             unlink($this->fileName);
         }
+        $this->removeDir($this->tmpDir);
         parent::tearDown();
     }
-}
\ No newline at end of file
+
+    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);
+    }
+}