]>
Commit | Line | Data |
---|---|---|
01e48f26 V |
1 | <?php |
2 | /** | |
3 | * Cache tests | |
4 | */ | |
dfc650aa | 5 | namespace Shaarli\Feed; |
01e48f26 V |
6 | |
7 | // required to access $_SESSION array | |
8 | session_start(); | |
9 | ||
dfc650aa | 10 | require_once 'application/feed/Cache.php'; |
01e48f26 V |
11 | |
12 | /** | |
13 | * Unitary tests for cached pages | |
14 | */ | |
dfc650aa | 15 | class CacheTest extends \PHPUnit\Framework\TestCase |
01e48f26 V |
16 | { |
17 | // test cache directory | |
4bf35ba5 | 18 | protected static $testCacheDir = 'sandbox/dummycache'; |
01e48f26 V |
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 | { | |
dfc650aa | 29 | if (!is_dir(self::$testCacheDir)) { |
01e48f26 | 30 | mkdir(self::$testCacheDir); |
aedd62e2 | 31 | } else { |
dfc650aa | 32 | array_map('unlink', glob(self::$testCacheDir . '/*')); |
01e48f26 | 33 | } |
b6a54537 | 34 | |
01e48f26 | 35 | foreach (self::$pages as $page) { |
dfc650aa | 36 | file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page); |
01e48f26 | 37 | } |
dfc650aa | 38 | file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere'); |
01e48f26 V |
39 | } |
40 | ||
b6a54537 A |
41 | /** |
42 | * Remove dummycache folder after each tests. | |
43 | */ | |
44 | public function tearDown() | |
45 | { | |
dfc650aa | 46 | array_map('unlink', glob(self::$testCacheDir . '/*')); |
b6a54537 A |
47 | rmdir(self::$testCacheDir); |
48 | } | |
49 | ||
01e48f26 V |
50 | /** |
51 | * Purge cached pages | |
52 | */ | |
53 | public function testPurgeCachedPages() | |
54 | { | |
55 | purgeCachedPages(self::$testCacheDir); | |
56 | foreach (self::$pages as $page) { | |
dfc650aa | 57 | $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); |
aedd62e2 V |
58 | } |
59 | ||
dfc650aa | 60 | $this->assertFileExists(self::$testCacheDir . '/intru.der'); |
aedd62e2 V |
61 | } |
62 | ||
63 | /** | |
64 | * Purge cached pages - missing directory | |
65 | */ | |
66 | public function testPurgeCachedPagesMissingDir() | |
67 | { | |
87f9f4f9 A |
68 | $oldlog = ini_get('error_log'); |
69 | ini_set('error_log', '/dev/null'); | |
aedd62e2 | 70 | $this->assertEquals( |
4bf35ba5 | 71 | 'Cannot purge sandbox/dummycache_missing: no directory', |
dfc650aa | 72 | purgeCachedPages(self::$testCacheDir . '_missing') |
aedd62e2 | 73 | ); |
87f9f4f9 | 74 | ini_set('error_log', $oldlog); |
01e48f26 V |
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) { | |
dfc650aa | 87 | $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); |
067c2dd8 | 88 | } |
01e48f26 V |
89 | |
90 | $this->assertArrayNotHasKey('tags', $_SESSION); | |
91 | } | |
92 | } |