aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/CachedPageTest.php
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-07-09 22:14:39 +0200
committerVirtualTam <virtualtam@flibidi.net>2015-08-13 23:48:06 +0200
commit01e48f269df59e02798dad4a698c125d76b0ed70 (patch)
tree9341487badd4ed14e0104ae338cf48c4ee0dc994 /tests/CachedPageTest.php
parent5ac5349ac053b1e560b136c62f8c764fd3230039 (diff)
downloadShaarli-01e48f269df59e02798dad4a698c125d76b0ed70.tar.gz
Shaarli-01e48f269df59e02798dad4a698c125d76b0ed70.tar.zst
Shaarli-01e48f269df59e02798dad4a698c125d76b0ed70.zip
CachedPage: move to a proper file, add tests
Modifications - rename `pageCache` to `CachedPage` - move utilities to `Cache` - do not access globals - apply coding rules - update LinkDB and test code - add test coverage Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests/CachedPageTest.php')
-rw-r--r--tests/CachedPageTest.php121
1 files changed, 121 insertions, 0 deletions
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
6require_once 'application/CachedPage.php';
7
8/**
9 * Unitary tests for cached pages
10 */
11class 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}