diff options
author | VirtualTam <virtualtam@flibidi.net> | 2015-08-13 23:54:26 +0200 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-08-13 23:54:26 +0200 |
commit | a3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5 (patch) | |
tree | 003ac85fc10d2b9b5051f299fda083a9ced69972 /tests | |
parent | 5ac5349ac053b1e560b136c62f8c764fd3230039 (diff) | |
parent | aedd62e2b84b4ea0d3c03f5c23ec594f4ebb1c17 (diff) | |
download | Shaarli-a3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5.tar.gz Shaarli-a3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5.tar.zst Shaarli-a3b1b4ae709bd3c5a8ec6fe29aa36fd01c52bca5.zip |
Merge pull request #309 from virtualtam/refactor/PageCache
CachedPage: move to a proper file, add tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CacheTest.php | 79 | ||||
-rw-r--r-- | tests/CachedPageTest.php | 121 | ||||
-rw-r--r-- | tests/LinkDBTest.php | 8 | ||||
-rw-r--r-- | tests/utils/ReferenceLinkDB.php | 1 |
4 files changed, 202 insertions, 7 deletions
diff --git a/tests/CacheTest.php b/tests/CacheTest.php new file mode 100644 index 00000000..aa5395b0 --- /dev/null +++ b/tests/CacheTest.php | |||
@@ -0,0 +1,79 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Cache tests | ||
4 | */ | ||
5 | |||
6 | // required to access $_SESSION array | ||
7 | session_start(); | ||
8 | |||
9 | require_once 'application/Cache.php'; | ||
10 | |||
11 | /** | ||
12 | * Unitary tests for cached pages | ||
13 | */ | ||
14 | class CachedTest extends PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | // test cache directory | ||
17 | protected static $testCacheDir = 'tests/dummycache'; | ||
18 | |||
19 | // dummy cached file names / content | ||
20 | protected static $pages = array('a', 'toto', 'd7b59c'); | ||
21 | |||
22 | |||
23 | /** | ||
24 | * Populate the cache with dummy files | ||
25 | */ | ||
26 | public function setUp() | ||
27 | { | ||
28 | if (! is_dir(self::$testCacheDir)) { | ||
29 | mkdir(self::$testCacheDir); | ||
30 | } else { | ||
31 | array_map('unlink', glob(self::$testCacheDir.'/*')); | ||
32 | } | ||
33 | |||
34 | foreach (self::$pages as $page) { | ||
35 | file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); | ||
36 | } | ||
37 | file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere'); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Purge cached pages | ||
42 | */ | ||
43 | public function testPurgeCachedPages() | ||
44 | { | ||
45 | purgeCachedPages(self::$testCacheDir); | ||
46 | foreach (self::$pages as $page) { | ||
47 | $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); | ||
48 | } | ||
49 | |||
50 | $this->assertFileExists(self::$testCacheDir.'/intru.der'); | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Purge cached pages - missing directory | ||
55 | */ | ||
56 | public function testPurgeCachedPagesMissingDir() | ||
57 | { | ||
58 | $this->assertEquals( | ||
59 | 'Cannot purge tests/dummycache_missing: no directory', | ||
60 | purgeCachedPages(self::$testCacheDir.'_missing') | ||
61 | ); | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * Purge cached pages and session cache | ||
66 | */ | ||
67 | public function testInvalidateCaches() | ||
68 | { | ||
69 | $this->assertArrayNotHasKey('tags', $_SESSION); | ||
70 | $_SESSION['tags'] = array('goodbye', 'cruel', 'world'); | ||
71 | |||
72 | invalidateCaches(self::$testCacheDir); | ||
73 | foreach (self::$pages as $page) { | ||
74 | $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); | ||
75 | } | ||
76 | |||
77 | $this->assertArrayNotHasKey('tags', $_SESSION); | ||
78 | } | ||
79 | } | ||
diff --git a/tests/CachedPageTest.php b/tests/CachedPageTest.php new file mode 100644 index 00000000..e97af030 --- /dev/null +++ b/tests/CachedPageTest.php | |||
@@ -0,0 +1,121 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * PageCache tests | ||
4 | */ | ||
5 | |||
6 | require_once 'application/CachedPage.php'; | ||
7 | |||
8 | /** | ||
9 | * Unitary tests for cached pages | ||
10 | */ | ||
11 | class CachedPageTest extends PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | // test cache directory | ||
14 | protected static $testCacheDir = 'tests/pagecache'; | ||
15 | protected static $url = 'http://shaar.li/?do=atom'; | ||
16 | protected static $filename; | ||
17 | |||
18 | /** | ||
19 | * Create the cache directory if needed | ||
20 | */ | ||
21 | public static function setUpBeforeClass() | ||
22 | { | ||
23 | if (! is_dir(self::$testCacheDir)) { | ||
24 | mkdir(self::$testCacheDir); | ||
25 | } | ||
26 | self::$filename = self::$testCacheDir.'/'.sha1(self::$url).'.cache'; | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Reset the page cache | ||
31 | */ | ||
32 | public function setUp() | ||
33 | { | ||
34 | if (file_exists(self::$filename)) { | ||
35 | unlink(self::$filename); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Create a new cached page | ||
41 | */ | ||
42 | public function testConstruct() | ||
43 | { | ||
44 | new CachedPage(self::$testCacheDir, '', true); | ||
45 | new CachedPage(self::$testCacheDir, '', false); | ||
46 | new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=rss', true); | ||
47 | new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=atom', false); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Cache a page's content | ||
52 | */ | ||
53 | public function testCache() | ||
54 | { | ||
55 | $page = new CachedPage(self::$testCacheDir, self::$url, true); | ||
56 | |||
57 | $this->assertFileNotExists(self::$filename); | ||
58 | $page->cache('<p>Some content</p>'); | ||
59 | $this->assertFileExists(self::$filename); | ||
60 | $this->assertEquals( | ||
61 | '<p>Some content</p>', | ||
62 | file_get_contents(self::$filename) | ||
63 | ); | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * "Cache" a page's content - the page is not to be cached | ||
68 | */ | ||
69 | public function testShouldNotCache() | ||
70 | { | ||
71 | $page = new CachedPage(self::$testCacheDir, self::$url, false); | ||
72 | |||
73 | $this->assertFileNotExists(self::$filename); | ||
74 | $page->cache('<p>Some content</p>'); | ||
75 | $this->assertFileNotExists(self::$filename); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Return a page's cached content | ||
80 | */ | ||
81 | public function testCachedVersion() | ||
82 | { | ||
83 | $page = new CachedPage(self::$testCacheDir, self::$url, true); | ||
84 | |||
85 | $this->assertFileNotExists(self::$filename); | ||
86 | $page->cache('<p>Some content</p>'); | ||
87 | $this->assertFileExists(self::$filename); | ||
88 | $this->assertEquals( | ||
89 | '<p>Some content</p>', | ||
90 | $page->cachedVersion() | ||
91 | ); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Return a page's cached content - the file does not exist | ||
96 | */ | ||
97 | public function testCachedVersionNoFile() | ||
98 | { | ||
99 | $page = new CachedPage(self::$testCacheDir, self::$url, true); | ||
100 | |||
101 | $this->assertFileNotExists(self::$filename); | ||
102 | $this->assertEquals( | ||
103 | null, | ||
104 | $page->cachedVersion() | ||
105 | ); | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * Return a page's cached content - the page is not to be cached | ||
110 | */ | ||
111 | public function testNoCachedVersion() | ||
112 | { | ||
113 | $page = new CachedPage(self::$testCacheDir, self::$url, false); | ||
114 | |||
115 | $this->assertFileNotExists(self::$filename); | ||
116 | $this->assertEquals( | ||
117 | null, | ||
118 | $page->cachedVersion() | ||
119 | ); | ||
120 | } | ||
121 | } | ||
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 504c8190..451f1d6f 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php | |||
@@ -3,6 +3,7 @@ | |||
3 | * Link datastore tests | 3 | * Link datastore tests |
4 | */ | 4 | */ |
5 | 5 | ||
6 | require_once 'application/Cache.php'; | ||
6 | require_once 'application/LinkDB.php'; | 7 | require_once 'application/LinkDB.php'; |
7 | require_once 'application/Utils.php'; | 8 | require_once 'application/Utils.php'; |
8 | require_once 'tests/utils/ReferenceLinkDB.php'; | 9 | require_once 'tests/utils/ReferenceLinkDB.php'; |
@@ -180,11 +181,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
180 | 'tags'=>'unit test' | 181 | 'tags'=>'unit test' |
181 | ); | 182 | ); |
182 | $testDB[$link['linkdate']] = $link; | 183 | $testDB[$link['linkdate']] = $link; |
183 | 184 | $testDB->savedb('tests'); | |
184 | // TODO: move PageCache to a proper class/file | ||
185 | function invalidateCaches() {} | ||
186 | |||
187 | $testDB->savedb(); | ||
188 | 185 | ||
189 | $testDB = new LinkDB(self::$testDatastore, true, false); | 186 | $testDB = new LinkDB(self::$testDatastore, true, false); |
190 | $this->assertEquals($dbSize + 1, sizeof($testDB)); | 187 | $this->assertEquals($dbSize + 1, sizeof($testDB)); |
@@ -514,4 +511,3 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
514 | ); | 511 | ); |
515 | } | 512 | } |
516 | } | 513 | } |
517 | ?> | ||
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 0b225720..47b51829 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php | |||
@@ -125,4 +125,3 @@ class ReferenceLinkDB | |||
125 | return $this->_privateCount; | 125 | return $this->_privateCount; |
126 | } | 126 | } |
127 | } | 127 | } |
128 | ?> | ||