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