From aedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 13 Aug 2015 21:39:51 +0200 Subject: [PATCH] Cache: simplify cached content cleanup, improve tests Signed-off-by: VirtualTam --- application/Cache.php | 20 ++++++-------------- tests/CacheTest.php | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/application/Cache.php b/application/Cache.php index 9c7e818..5d05016 100644 --- a/application/Cache.php +++ b/application/Cache.php @@ -7,26 +7,18 @@ * Purges all cached pages * * @param string $pageCacheDir page cache directory + * + * @return mixed an error string if the directory is missing */ function purgeCachedPages($pageCacheDir) { if (! is_dir($pageCacheDir)) { - return; + $error = 'Cannot purge '.$pageCacheDir.': no directory'; + error_log($error); + return $error; } - // TODO: check write access to the cache directory - - $handler = opendir($pageCacheDir); - if ($handler == false) { - return; - } - - while (($filename = readdir($handler)) !== false) { - if (endsWith($filename, '.cache')) { - unlink($pageCacheDir.'/'.$filename); - } - } - closedir($handler); + array_map('unlink', glob($pageCacheDir.'/*.cache')); } /** diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 4caf655..aa5395b 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -27,11 +27,14 @@ class CachedTest extends PHPUnit_Framework_TestCase { if (! is_dir(self::$testCacheDir)) { mkdir(self::$testCacheDir); + } else { + array_map('unlink', glob(self::$testCacheDir.'/*')); } foreach (self::$pages as $page) { file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); } + file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere'); } /** @@ -42,7 +45,20 @@ class CachedTest extends PHPUnit_Framework_TestCase purgeCachedPages(self::$testCacheDir); foreach (self::$pages as $page) { $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); - } + } + + $this->assertFileExists(self::$testCacheDir.'/intru.der'); + } + + /** + * Purge cached pages - missing directory + */ + public function testPurgeCachedPagesMissingDir() + { + $this->assertEquals( + 'Cannot purge tests/dummycache_missing: no directory', + purgeCachedPages(self::$testCacheDir.'_missing') + ); } /**