7 namespace Shaarli\Render
;
9 use Shaarli\Security\SessionManager
;
13 * Unitary tests for cached pages
15 class PageCacheManagerTest
extends TestCase
17 // test cache directory
18 protected static $testCacheDir = 'sandbox/dummycache';
20 // dummy cached file names / content
21 protected static $pages = array('a', 'toto', 'd7b59c');
23 /** @var PageCacheManager */
24 protected $cacheManager;
26 /** @var SessionManager */
27 protected $sessionManager;
30 * Populate the cache with dummy files
32 protected function setUp(): void
34 $this->cacheManager
= new PageCacheManager(static::$testCacheDir, true);
36 if (!is_dir(self
::$testCacheDir)) {
37 mkdir(self
::$testCacheDir);
39 array_map('unlink', glob(self
::$testCacheDir . '/*'));
42 foreach (self
::$pages as $page) {
43 file_put_contents(self
::$testCacheDir . '/' . $page . '.cache', $page);
45 file_put_contents(self
::$testCacheDir . '/intru.der', 'ShouldNotBeThere');
49 * Remove dummycache folder after each tests.
51 protected function tearDown(): void
53 array_map('unlink', glob(self
::$testCacheDir . '/*'));
54 rmdir(self
::$testCacheDir);
60 public function testPurgeCachedPages()
62 $this->cacheManager
->purgeCachedPages();
63 foreach (self
::$pages as $page) {
64 $this->assertFileNotExists(self
::$testCacheDir . '/' . $page . '.cache');
67 $this->assertFileExists(self
::$testCacheDir . '/intru.der');
71 * Purge cached pages - missing directory
73 public function testPurgeCachedPagesMissingDir()
75 $this->cacheManager
= new PageCacheManager(self
::$testCacheDir . '_missing', true);
77 $oldlog = ini_get('error_log');
78 ini_set('error_log', '/dev/null');
80 'Cannot purge sandbox/dummycache_missing: no directory',
81 $this->cacheManager
->purgeCachedPages()
83 ini_set('error_log', $oldlog);