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