]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/CacheTest.php
Markdown: inline code background color
[github/shaarli/Shaarli.git] / tests / CacheTest.php
CommitLineData
01e48f26
V
1<?php
2/**
3 * Cache tests
4 */
5
6// required to access $_SESSION array
7session_start();
8
9require_once 'application/Cache.php';
10
11/**
12 * Unitary tests for cached pages
13 */
4bf35ba5 14class CacheTest extends PHPUnit_Framework_TestCase
01e48f26
V
15{
16 // test cache directory
4bf35ba5 17 protected static $testCacheDir = 'sandbox/dummycache';
01e48f26
V
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);
aedd62e2
V
30 } else {
31 array_map('unlink', glob(self::$testCacheDir.'/*'));
01e48f26 32 }
b6a54537 33
01e48f26
V
34 foreach (self::$pages as $page) {
35 file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
36 }
aedd62e2 37 file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere');
01e48f26
V
38 }
39
b6a54537
A
40 /**
41 * Remove dummycache folder after each tests.
42 */
43 public function tearDown()
44 {
45 array_map('unlink', glob(self::$testCacheDir.'/*'));
46 rmdir(self::$testCacheDir);
47 }
48
01e48f26
V
49 /**
50 * Purge cached pages
51 */
52 public function testPurgeCachedPages()
53 {
54 purgeCachedPages(self::$testCacheDir);
55 foreach (self::$pages as $page) {
56 $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
aedd62e2
V
57 }
58
59 $this->assertFileExists(self::$testCacheDir.'/intru.der');
60 }
61
62 /**
63 * Purge cached pages - missing directory
64 */
65 public function testPurgeCachedPagesMissingDir()
66 {
67 $this->assertEquals(
4bf35ba5 68 'Cannot purge sandbox/dummycache_missing: no directory',
aedd62e2
V
69 purgeCachedPages(self::$testCacheDir.'_missing')
70 );
01e48f26
V
71 }
72
73 /**
74 * Purge cached pages and session cache
75 */
76 public function testInvalidateCaches()
77 {
78 $this->assertArrayNotHasKey('tags', $_SESSION);
79 $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
80
81 invalidateCaches(self::$testCacheDir);
82 foreach (self::$pages as $page) {
83 $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
84 }
85
86 $this->assertArrayNotHasKey('tags', $_SESSION);
87 }
88}