diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-23 21:13:41 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | b0428aa9b02b058b72c40b6e8dc2298d55bf692f (patch) | |
tree | c57176b24d76836a73608fb273b1094fcb8de785 /tests/render | |
parent | 485b168a9677d160b0c0426e4f282b9bd0c632c1 (diff) | |
download | Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.tar.gz Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.tar.zst Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.zip |
Migrate cache purge function to a proper class
And update dependencies and tests.
Note that SESSION['tags'] has been removed a log ago
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 | } | ||