Migrate cache purge function to a proper class

And update dependencies and tests.

Note that SESSION['tags'] has been removed a log ago
This commit is contained in:
ArthurHoaro 2020-01-23 21:13:41 +01:00
parent 485b168a96
commit b0428aa9b0
9 changed files with 76 additions and 68 deletions

View file

@ -18,7 +18,6 @@ require_once 'application/bookmark/LinkUtils.php';
require_once 'application/Utils.php';
require_once 'application/http/UrlUtils.php';
require_once 'application/http/HttpUtils.php';
require_once 'application/feed/Cache.php';
require_once 'tests/utils/ReferenceLinkDB.php';
require_once 'tests/utils/ReferenceHistory.php';
require_once 'tests/utils/FakeBookmarkService.php';

View file

@ -11,7 +11,6 @@ use ReflectionClass;
use Shaarli;
use Shaarli\Bookmark\Bookmark;
require_once 'application/feed/Cache.php';
require_once 'application/Utils.php';
require_once 'tests/utils/ReferenceLinkDB.php';

View file

@ -2,17 +2,18 @@
/**
* Cache tests
*/
namespace Shaarli\Feed;
namespace Shaarli\Render;
use PHPUnit\Framework\TestCase;
use Shaarli\Security\SessionManager;
// required to access $_SESSION array
session_start();
require_once 'application/feed/Cache.php';
/**
* Unitary tests for cached pages
*/
class CacheTest extends \PHPUnit\Framework\TestCase
class PageCacheManagerTest extends TestCase
{
// test cache directory
protected static $testCacheDir = 'sandbox/dummycache';
@ -20,12 +21,19 @@ class CacheTest extends \PHPUnit\Framework\TestCase
// dummy cached file names / content
protected static $pages = array('a', 'toto', 'd7b59c');
/** @var PageCacheManager */
protected $cacheManager;
/** @var SessionManager */
protected $sessionManager;
/**
* Populate the cache with dummy files
*/
public function setUp()
{
$this->cacheManager = new PageCacheManager(static::$testCacheDir);
if (!is_dir(self::$testCacheDir)) {
mkdir(self::$testCacheDir);
} else {
@ -52,7 +60,7 @@ class CacheTest extends \PHPUnit\Framework\TestCase
*/
public function testPurgeCachedPages()
{
purgeCachedPages(self::$testCacheDir);
$this->cacheManager->purgeCachedPages();
foreach (self::$pages as $page) {
$this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
}
@ -65,28 +73,14 @@ class CacheTest extends \PHPUnit\Framework\TestCase
*/
public function testPurgeCachedPagesMissingDir()
{
$this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing');
$oldlog = ini_get('error_log');
ini_set('error_log', '/dev/null');
$this->assertEquals(
'Cannot purge sandbox/dummycache_missing: no directory',
purgeCachedPages(self::$testCacheDir . '_missing')
$this->cacheManager->purgeCachedPages()
);
ini_set('error_log', $oldlog);
}
/**
* Purge cached pages and session cache
*/
public function testInvalidateCaches()
{
$this->assertArrayNotHasKey('tags', $_SESSION);
$_SESSION['tags'] = array('goodbye', 'cruel', 'world');
invalidateCaches(self::$testCacheDir);
foreach (self::$pages as $page) {
$this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
}
$this->assertArrayNotHasKey('tags', $_SESSION);
}
}