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()
68 'Cannot purge sandbox/dummycache_missing: no directory',
69 purgeCachedPages(self
::$testCacheDir.'_missing')
74 * Purge cached pages and session cache
76 public function testInvalidateCaches()
78 $this->assertArrayNotHasKey('tags', $_SESSION);
79 $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
81 invalidateCaches(self
::$testCacheDir);
82 foreach (self
::$pages as $page) {
83 $this->assertFileNotExists(self
::$testCacheDir.'/'.$page.'.cache');
86 $this->assertArrayNotHasKey('tags', $_SESSION);