5 namespace Shaarli\Render
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Security\SessionManager
;
10 // required to access $_SESSION array
14 * Unitary tests for cached pages
16 class PageCacheManagerTest
extends TestCase
18 // test cache directory
19 protected static $testCacheDir = 'sandbox/dummycache';
21 // dummy cached file names / content
22 protected static $pages = array('a', 'toto', 'd7b59c');
24 /** @var PageCacheManager */
25 protected $cacheManager;
27 /** @var SessionManager */
28 protected $sessionManager;
31 * Populate the cache with dummy files
33 public function setUp()
35 $this->cacheManager
= new PageCacheManager(static::$testCacheDir, true);
37 if (!is_dir(self
::$testCacheDir)) {
38 mkdir(self
::$testCacheDir);
40 array_map('unlink', glob(self
::$testCacheDir . '/*'));
43 foreach (self
::$pages as $page) {
44 file_put_contents(self
::$testCacheDir . '/' . $page . '.cache', $page);
46 file_put_contents(self
::$testCacheDir . '/intru.der', 'ShouldNotBeThere');
50 * Remove dummycache folder after each tests.
52 public function tearDown()
54 array_map('unlink', glob(self
::$testCacheDir . '/*'));
55 rmdir(self
::$testCacheDir);
61 public function testPurgeCachedPages()
63 $this->cacheManager
->purgeCachedPages();
64 foreach (self
::$pages as $page) {
65 $this->assertFileNotExists(self
::$testCacheDir . '/' . $page . '.cache');
68 $this->assertFileExists(self
::$testCacheDir . '/intru.der');
72 * Purge cached pages - missing directory
74 public function testPurgeCachedPagesMissingDir()
76 $this->cacheManager
= new PageCacheManager(self
::$testCacheDir . '_missing', true);
78 $oldlog = ini_get('error_log');
79 ini_set('error_log', '/dev/null');
81 'Cannot purge sandbox/dummycache_missing: no directory',
82 $this->cacheManager
->purgeCachedPages()
84 ini_set('error_log', $oldlog);