6 require_once 'application/CachedPage.php';
9 * Unitary tests for cached pages
11 class CachedPageTest
extends PHPUnit_Framework_TestCase
13 // test cache directory
14 protected static $testCacheDir = 'tests/pagecache';
15 protected static $url = 'http://shaar.li/?do=atom';
16 protected static $filename;
19 * Create the cache directory if needed
21 public static function setUpBeforeClass()
23 if (! is_dir(self
::$testCacheDir)) {
24 mkdir(self
::$testCacheDir);
26 self
::$filename = self
::$testCacheDir.'/'.sha1(self
::$url).'.cache';
30 * Reset the page cache
32 public function setUp()
34 if (file_exists(self
::$filename)) {
35 unlink(self
::$filename);
40 * Create a new cached page
42 public function testConstruct()
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);
51 * Cache a page's content
53 public function testCache()
55 $page = new CachedPage(self
::$testCacheDir, self
::$url, true);
57 $this->assertFileNotExists(self
::$filename);
58 $page->cache('<p>Some content</p>');
59 $this->assertFileExists(self
::$filename);
61 '<p>Some content</p>',
62 file_get_contents(self
::$filename)
67 * "Cache" a page's content - the page is not to be cached
69 public function testShouldNotCache()
71 $page = new CachedPage(self
::$testCacheDir, self
::$url, false);
73 $this->assertFileNotExists(self
::$filename);
74 $page->cache('<p>Some content</p>');
75 $this->assertFileNotExists(self
::$filename);
79 * Return a page's cached content
81 public function testCachedVersion()
83 $page = new CachedPage(self
::$testCacheDir, self
::$url, true);
85 $this->assertFileNotExists(self
::$filename);
86 $page->cache('<p>Some content</p>');
87 $this->assertFileExists(self
::$filename);
89 '<p>Some content</p>',
90 $page->cachedVersion()
95 * Return a page's cached content - the file does not exist
97 public function testCachedVersionNoFile()
99 $page = new CachedPage(self
::$testCacheDir, self
::$url, true);
101 $this->assertFileNotExists(self
::$filename);
104 $page->cachedVersion()
109 * Return a page's cached content - the page is not to be cached
111 public function testNoCachedVersion()
113 $page = new CachedPage(self
::$testCacheDir, self
::$url, false);
115 $this->assertFileNotExists(self
::$filename);
118 $page->cachedVersion()