Cache: simplify cached content cleanup, improve tests

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2015-08-13 21:39:51 +02:00
parent 01e48f269d
commit aedd62e2b8
2 changed files with 23 additions and 15 deletions

View file

@ -7,26 +7,18 @@
* Purges all cached pages * Purges all cached pages
* *
* @param string $pageCacheDir page cache directory * @param string $pageCacheDir page cache directory
*
* @return mixed an error string if the directory is missing
*/ */
function purgeCachedPages($pageCacheDir) function purgeCachedPages($pageCacheDir)
{ {
if (! is_dir($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 array_map('unlink', glob($pageCacheDir.'/*.cache'));
$handler = opendir($pageCacheDir);
if ($handler == false) {
return;
}
while (($filename = readdir($handler)) !== false) {
if (endsWith($filename, '.cache')) {
unlink($pageCacheDir.'/'.$filename);
}
}
closedir($handler);
} }
/** /**

View file

@ -27,11 +27,14 @@ public function setUp()
{ {
if (! is_dir(self::$testCacheDir)) { if (! is_dir(self::$testCacheDir)) {
mkdir(self::$testCacheDir); mkdir(self::$testCacheDir);
} else {
array_map('unlink', glob(self::$testCacheDir.'/*'));
} }
foreach (self::$pages as $page) { foreach (self::$pages as $page) {
file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
} }
file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere');
} }
/** /**
@ -43,6 +46,19 @@ public function testPurgeCachedPages()
foreach (self::$pages as $page) { foreach (self::$pages as $page) {
$this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); $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')
);
} }
/** /**