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