diff options
Diffstat (limited to 'tests/CacheTest.php')
-rw-r--r-- | tests/CacheTest.php | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/CacheTest.php b/tests/CacheTest.php new file mode 100644 index 00000000..aa5395b0 --- /dev/null +++ b/tests/CacheTest.php | |||
@@ -0,0 +1,79 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Cache tests | ||
4 | */ | ||
5 | |||
6 | // required to access $_SESSION array | ||
7 | session_start(); | ||
8 | |||
9 | require_once 'application/Cache.php'; | ||
10 | |||
11 | /** | ||
12 | * Unitary tests for cached pages | ||
13 | */ | ||
14 | class CachedTest extends PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | // test cache directory | ||
17 | protected static $testCacheDir = 'tests/dummycache'; | ||
18 | |||
19 | // dummy cached file names / content | ||
20 | protected static $pages = array('a', 'toto', 'd7b59c'); | ||
21 | |||
22 | |||
23 | /** | ||
24 | * Populate the cache with dummy files | ||
25 | */ | ||
26 | public function setUp() | ||
27 | { | ||
28 | if (! is_dir(self::$testCacheDir)) { | ||
29 | mkdir(self::$testCacheDir); | ||
30 | } else { | ||
31 | array_map('unlink', glob(self::$testCacheDir.'/*')); | ||
32 | } | ||
33 | |||
34 | foreach (self::$pages as $page) { | ||
35 | file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); | ||
36 | } | ||
37 | file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere'); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Purge cached pages | ||
42 | */ | ||
43 | public function testPurgeCachedPages() | ||
44 | { | ||
45 | purgeCachedPages(self::$testCacheDir); | ||
46 | foreach (self::$pages as $page) { | ||
47 | $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); | ||
48 | } | ||
49 | |||
50 | $this->assertFileExists(self::$testCacheDir.'/intru.der'); | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Purge cached pages - missing directory | ||
55 | */ | ||
56 | public function testPurgeCachedPagesMissingDir() | ||
57 | { | ||
58 | $this->assertEquals( | ||
59 | 'Cannot purge tests/dummycache_missing: no directory', | ||
60 | purgeCachedPages(self::$testCacheDir.'_missing') | ||
61 | ); | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * Purge cached pages and session cache | ||
66 | */ | ||
67 | public function testInvalidateCaches() | ||
68 | { | ||
69 | $this->assertArrayNotHasKey('tags', $_SESSION); | ||
70 | $_SESSION['tags'] = array('goodbye', 'cruel', 'world'); | ||
71 | |||
72 | invalidateCaches(self::$testCacheDir); | ||
73 | foreach (self::$pages as $page) { | ||
74 | $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); | ||
75 | } | ||
76 | |||
77 | $this->assertArrayNotHasKey('tags', $_SESSION); | ||
78 | } | ||
79 | } | ||