]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /** | |
3 | * Cache tests | |
4 | */ | |
5 | namespace Shaarli\Feed; | |
6 | ||
7 | // required to access $_SESSION array | |
8 | session_start(); | |
9 | ||
10 | require_once 'application/feed/Cache.php'; | |
11 | ||
12 | /** | |
13 | * Unitary tests for cached pages | |
14 | */ | |
15 | class CacheTest extends \PHPUnit\Framework\TestCase | |
16 | { | |
17 | // test cache directory | |
18 | protected static $testCacheDir = 'sandbox/dummycache'; | |
19 | ||
20 | // dummy cached file names / content | |
21 | protected static $pages = array('a', 'toto', 'd7b59c'); | |
22 | ||
23 | ||
24 | /** | |
25 | * Populate the cache with dummy files | |
26 | */ | |
27 | public function setUp() | |
28 | { | |
29 | if (!is_dir(self::$testCacheDir)) { | |
30 | mkdir(self::$testCacheDir); | |
31 | } else { | |
32 | array_map('unlink', glob(self::$testCacheDir . '/*')); | |
33 | } | |
34 | ||
35 | foreach (self::$pages as $page) { | |
36 | file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page); | |
37 | } | |
38 | file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere'); | |
39 | } | |
40 | ||
41 | /** | |
42 | * Remove dummycache folder after each tests. | |
43 | */ | |
44 | public function tearDown() | |
45 | { | |
46 | array_map('unlink', glob(self::$testCacheDir . '/*')); | |
47 | rmdir(self::$testCacheDir); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Purge cached pages | |
52 | */ | |
53 | public function testPurgeCachedPages() | |
54 | { | |
55 | purgeCachedPages(self::$testCacheDir); | |
56 | foreach (self::$pages as $page) { | |
57 | $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); | |
58 | } | |
59 | ||
60 | $this->assertFileExists(self::$testCacheDir . '/intru.der'); | |
61 | } | |
62 | ||
63 | /** | |
64 | * Purge cached pages - missing directory | |
65 | */ | |
66 | public function testPurgeCachedPagesMissingDir() | |
67 | { | |
68 | $oldlog = ini_get('error_log'); | |
69 | ini_set('error_log', '/dev/null'); | |
70 | $this->assertEquals( | |
71 | 'Cannot purge sandbox/dummycache_missing: no directory', | |
72 | purgeCachedPages(self::$testCacheDir . '_missing') | |
73 | ); | |
74 | ini_set('error_log', $oldlog); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Purge cached pages and session cache | |
79 | */ | |
80 | public function testInvalidateCaches() | |
81 | { | |
82 | $this->assertArrayNotHasKey('tags', $_SESSION); | |
83 | $_SESSION['tags'] = array('goodbye', 'cruel', 'world'); | |
84 | ||
85 | invalidateCaches(self::$testCacheDir); | |
86 | foreach (self::$pages as $page) { | |
87 | $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); | |
88 | } | |
89 | ||
90 | $this->assertArrayNotHasKey('tags', $_SESSION); | |
91 | } | |
92 | } |