diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /tests/render/PageCacheManagerTest.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/render/PageCacheManagerTest.php')
-rw-r--r-- | tests/render/PageCacheManagerTest.php | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/render/PageCacheManagerTest.php b/tests/render/PageCacheManagerTest.php new file mode 100644 index 00000000..08d4e5ea --- /dev/null +++ b/tests/render/PageCacheManagerTest.php | |||
@@ -0,0 +1,85 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Cache tests | ||
5 | */ | ||
6 | |||
7 | namespace Shaarli\Render; | ||
8 | |||
9 | use Shaarli\Security\SessionManager; | ||
10 | use Shaarli\TestCase; | ||
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 | protected function setUp(): void | ||
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 | protected function tearDown(): void | ||
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 | } | ||