5 namespace Shaarli\Feed
;
8 * Unitary tests for cached pages
10 class CachedPageTest
extends \PHPUnit\Framework\TestCase
12 // test cache directory
13 protected static $testCacheDir = 'sandbox/pagecache';
14 protected static $url = 'http://shaar.li/?do=atom';
15 protected static $filename;
18 * Create the cache directory if needed
20 public static function setUpBeforeClass()
22 if (!is_dir(self
::$testCacheDir)) {
23 mkdir(self
::$testCacheDir);
25 self
::$filename = self
::$testCacheDir . '/' . sha1(self
::$url) . '.cache';
29 * Reset the page cache
31 public function setUp()
33 if (file_exists(self
::$filename)) {
34 unlink(self
::$filename);
39 * Create a new cached page
41 public function testConstruct()
43 new CachedPage(self
::$testCacheDir, '', true);
44 new CachedPage(self
::$testCacheDir, '', false);
45 new CachedPage(self
::$testCacheDir, 'http://shaar.li/?do=rss', true);
46 new CachedPage(self
::$testCacheDir, 'http://shaar.li/?do=atom', false);
50 * Cache a page's content
52 public function testCache()
54 $page = new CachedPage(self
::$testCacheDir, self
::$url, true);
56 $this->assertFileNotExists(self
::$filename);
57 $page->cache('<p>Some content</p>');
58 $this->assertFileExists(self
::$filename);
60 '<p>Some content</p>',
61 file_get_contents(self
::$filename)
66 * "Cache" a page's content - the page is not to be cached
68 public function testShouldNotCache()
70 $page = new CachedPage(self
::$testCacheDir, self
::$url, false);
72 $this->assertFileNotExists(self
::$filename);
73 $page->cache('<p>Some content</p>');
74 $this->assertFileNotExists(self
::$filename);
78 * Return a page's cached content
80 public function testCachedVersion()
82 $page = new CachedPage(self
::$testCacheDir, self
::$url, true);
84 $this->assertFileNotExists(self
::$filename);
85 $page->cache('<p>Some content</p>');
86 $this->assertFileExists(self
::$filename);
88 '<p>Some content</p>',
89 $page->cachedVersion()
94 * Return a page's cached content - the file does not exist
96 public function testCachedVersionNoFile()
98 $page = new CachedPage(self
::$testCacheDir, self
::$url, true);
100 $this->assertFileNotExists(self
::$filename);
103 $page->cachedVersion()
108 * Return a page's cached content - the page is not to be cached
110 public function testNoCachedVersion()
112 $page = new CachedPage(self
::$testCacheDir, self
::$url, false);
114 $this->assertFileNotExists(self
::$filename);
117 $page->cachedVersion()