diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CacheTest.php | 63 | ||||
-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, 186 insertions, 7 deletions
diff --git a/tests/CacheTest.php b/tests/CacheTest.php new file mode 100644 index 00000000..4caf6552 --- /dev/null +++ b/tests/CacheTest.php | |||
@@ -0,0 +1,63 @@ | |||
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 | } | ||
31 | |||
32 | foreach (self::$pages as $page) { | ||
33 | file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | /** | ||
38 | * Purge cached pages | ||
39 | */ | ||
40 | public function testPurgeCachedPages() | ||
41 | { | ||
42 | purgeCachedPages(self::$testCacheDir); | ||
43 | foreach (self::$pages as $page) { | ||
44 | $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * Purge cached pages and session cache | ||
50 | */ | ||
51 | public function testInvalidateCaches() | ||
52 | { | ||
53 | $this->assertArrayNotHasKey('tags', $_SESSION); | ||
54 | $_SESSION['tags'] = array('goodbye', 'cruel', 'world'); | ||
55 | |||
56 | invalidateCaches(self::$testCacheDir); | ||
57 | foreach (self::$pages as $page) { | ||
58 | $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); | ||
59 | } | ||
60 | |||
61 | $this->assertArrayNotHasKey('tags', $_SESSION); | ||
62 | } | ||
63 | } | ||
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 | ?> | ||