6 // required to access $_SESSION array
9 require_once 'application/Cache.php';
12 * Unitary tests for cached pages
14 class CacheTest
extends PHPUnit_Framework_TestCase
16 // test cache directory
17 protected static $testCacheDir = 'sandbox/dummycache';
19 // dummy cached file names / content
20 protected static $pages = array('a', 'toto', 'd7b59c');
24 * Populate the cache with dummy files
26 public function setUp()
28 if (! is_dir(self
::$testCacheDir)) {
29 mkdir(self
::$testCacheDir);
31 array_map('unlink', glob(self
::$testCacheDir.'/*'));
34 foreach (self
::$pages as $page) {
35 file_put_contents(self
::$testCacheDir.'/'.$page.'.cache', $page);
37 file_put_contents(self
::$testCacheDir.'/intru.der', 'ShouldNotBeThere');
41 * Remove dummycache folder after each tests.
43 public function tearDown()
45 array_map('unlink', glob(self
::$testCacheDir.'/*'));
46 rmdir(self
::$testCacheDir);
52 public function testPurgeCachedPages()
54 purgeCachedPages(self
::$testCacheDir);
55 foreach (self
::$pages as $page) {
56 $this->assertFileNotExists(self
::$testCacheDir.'/'.$page.'.cache');
59 $this->assertFileExists(self
::$testCacheDir.'/intru.der');
63 * Purge cached pages - missing directory
65 public function testPurgeCachedPagesMissingDir()
67 $oldlog = ini_get('error_log');
68 ini_set('error_log', '/dev/null');
70 'Cannot purge sandbox/dummycache_missing: no directory',
71 purgeCachedPages(self
::$testCacheDir.'_missing')
73 ini_set('error_log', $oldlog);
77 * Purge cached pages and session cache
79 public function testInvalidateCaches()
81 $this->assertArrayNotHasKey('tags', $_SESSION);
82 $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
84 invalidateCaches(self
::$testCacheDir);
85 foreach (self
::$pages as $page) {
86 $this->assertFileNotExists(self
::$testCacheDir.'/'.$page.'.cache');
89 $this->assertArrayNotHasKey('tags', $_SESSION);