aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/feed/CacheTest.php
diff options
context:
space:
mode:
authorAurélien Tamisier <virtualtam+github@flibidi.net>2019-01-18 21:26:03 +0100
committerGitHub <noreply@github.com>2019-01-18 21:26:03 +0100
commitff3b5dc5542ec150f0d9b447394364a15e9156d0 (patch)
tree5e926e36816d510e3b3a10e20b94c23f43b55092 /tests/feed/CacheTest.php
parent1826e383ecf501302974132fd443cf1ca06e10f6 (diff)
parentdea72c711ff740b3b829d238fcf85648465143a0 (diff)
downloadShaarli-ff3b5dc5542ec150f0d9b447394364a15e9156d0.tar.gz
Shaarli-ff3b5dc5542ec150f0d9b447394364a15e9156d0.tar.zst
Shaarli-ff3b5dc5542ec150f0d9b447394364a15e9156d0.zip
Merge pull request #1248 from virtualtam/refactor/namespacing
Ensure all PHP classes are properly namespaced
Diffstat (limited to 'tests/feed/CacheTest.php')
-rw-r--r--tests/feed/CacheTest.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/feed/CacheTest.php b/tests/feed/CacheTest.php
new file mode 100644
index 00000000..c0a9f26f
--- /dev/null
+++ b/tests/feed/CacheTest.php
@@ -0,0 +1,92 @@
1<?php
2/**
3 * Cache tests
4 */
5namespace Shaarli\Feed;
6
7// required to access $_SESSION array
8session_start();
9
10require_once 'application/feed/Cache.php';
11
12/**
13 * Unitary tests for cached pages
14 */
15class CacheTest extends \PHPUnit\Framework\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
24 /**
25 * Populate the cache with dummy files
26 */
27 public function setUp()
28 {
29 if (!is_dir(self::$testCacheDir)) {
30 mkdir(self::$testCacheDir);
31 } else {
32 array_map('unlink', glob(self::$testCacheDir . '/*'));
33 }
34
35 foreach (self::$pages as $page) {
36 file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page);
37 }
38 file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere');
39 }
40
41 /**
42 * Remove dummycache folder after each tests.
43 */
44 public function tearDown()
45 {
46 array_map('unlink', glob(self::$testCacheDir . '/*'));
47 rmdir(self::$testCacheDir);
48 }
49
50 /**
51 * Purge cached pages
52 */
53 public function testPurgeCachedPages()
54 {
55 purgeCachedPages(self::$testCacheDir);
56 foreach (self::$pages as $page) {
57 $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
58 }
59
60 $this->assertFileExists(self::$testCacheDir . '/intru.der');
61 }
62
63 /**
64 * Purge cached pages - missing directory
65 */
66 public function testPurgeCachedPagesMissingDir()
67 {
68 $oldlog = ini_get('error_log');
69 ini_set('error_log', '/dev/null');
70 $this->assertEquals(
71 'Cannot purge sandbox/dummycache_missing: no directory',
72 purgeCachedPages(self::$testCacheDir . '_missing')
73 );
74 ini_set('error_log', $oldlog);
75 }
76
77 /**
78 * Purge cached pages and session cache
79 */
80 public function testInvalidateCaches()
81 {
82 $this->assertArrayNotHasKey('tags', $_SESSION);
83 $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
84
85 invalidateCaches(self::$testCacheDir);
86 foreach (self::$pages as $page) {
87 $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
88 }
89
90 $this->assertArrayNotHasKey('tags', $_SESSION);
91 }
92}