5 namespace Shaarli\Feed
;
7 // required to access $_SESSION array
10 require_once 'application/feed/Cache.php';
13 * Unitary tests for cached pages
15 class CacheTest
extends \PHPUnit\Framework\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');
25 * Populate the cache with dummy files
27 public function setUp()
29 if (!is_dir(self
::$testCacheDir)) {
30 mkdir(self
::$testCacheDir);
32 array_map('unlink', glob(self
::$testCacheDir . '/*'));
35 foreach (self
::$pages as $page) {
36 file_put_contents(self
::$testCacheDir . '/' . $page . '.cache', $page);
38 file_put_contents(self
::$testCacheDir . '/intru.der', 'ShouldNotBeThere');
42 * Remove dummycache folder after each tests.
44 public function tearDown()
46 array_map('unlink', glob(self
::$testCacheDir . '/*'));
47 rmdir(self
::$testCacheDir);
53 public function testPurgeCachedPages()
55 purgeCachedPages(self
::$testCacheDir);
56 foreach (self
::$pages as $page) {
57 $this->assertFileNotExists(self
::$testCacheDir . '/' . $page . '.cache');
60 $this->assertFileExists(self
::$testCacheDir . '/intru.der');
64 * Purge cached pages - missing directory
66 public function testPurgeCachedPagesMissingDir()
68 $oldlog = ini_get('error_log');
69 ini_set('error_log', '/dev/null');
71 'Cannot purge sandbox/dummycache_missing: no directory',
72 purgeCachedPages(self
::$testCacheDir . '_missing')
74 ini_set('error_log', $oldlog);
78 * Purge cached pages and session cache
80 public function testInvalidateCaches()
82 $this->assertArrayNotHasKey('tags', $_SESSION);
83 $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
85 invalidateCaches(self
::$testCacheDir);
86 foreach (self
::$pages as $page) {
87 $this->assertFileNotExists(self
::$testCacheDir . '/' . $page . '.cache');
90 $this->assertArrayNotHasKey('tags', $_SESSION);