]>
Commit | Line | Data |
---|---|---|
01e48f26 V |
1 | <?php |
2 | /** | |
3 | * PageCache tests | |
4 | */ | |
dfc650aa | 5 | namespace Shaarli\Feed; |
01e48f26 V |
6 | |
7 | /** | |
8 | * Unitary tests for cached pages | |
9 | */ | |
dfc650aa | 10 | class CachedPageTest extends \PHPUnit\Framework\TestCase |
01e48f26 V |
11 | { |
12 | // test cache directory | |
4bf35ba5 | 13 | protected static $testCacheDir = 'sandbox/pagecache'; |
01e48f26 V |
14 | protected static $url = 'http://shaar.li/?do=atom'; |
15 | protected static $filename; | |
16 | ||
17 | /** | |
18 | * Create the cache directory if needed | |
19 | */ | |
20 | public static function setUpBeforeClass() | |
21 | { | |
dfc650aa | 22 | if (!is_dir(self::$testCacheDir)) { |
01e48f26 V |
23 | mkdir(self::$testCacheDir); |
24 | } | |
dfc650aa | 25 | self::$filename = self::$testCacheDir . '/' . sha1(self::$url) . '.cache'; |
01e48f26 V |
26 | } |
27 | ||
28 | /** | |
29 | * Reset the page cache | |
30 | */ | |
31 | public function setUp() | |
32 | { | |
33 | if (file_exists(self::$filename)) { | |
34 | unlink(self::$filename); | |
35 | } | |
36 | } | |
37 | ||
38 | /** | |
39 | * Create a new cached page | |
40 | */ | |
41 | public function testConstruct() | |
42 | { | |
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); | |
47 | } | |
48 | ||
49 | /** | |
50 | * Cache a page's content | |
51 | */ | |
52 | public function testCache() | |
53 | { | |
54 | $page = new CachedPage(self::$testCacheDir, self::$url, true); | |
55 | ||
56 | $this->assertFileNotExists(self::$filename); | |
57 | $page->cache('<p>Some content</p>'); | |
58 | $this->assertFileExists(self::$filename); | |
59 | $this->assertEquals( | |
60 | '<p>Some content</p>', | |
61 | file_get_contents(self::$filename) | |
62 | ); | |
63 | } | |
64 | ||
65 | /** | |
66 | * "Cache" a page's content - the page is not to be cached | |
67 | */ | |
68 | public function testShouldNotCache() | |
69 | { | |
70 | $page = new CachedPage(self::$testCacheDir, self::$url, false); | |
71 | ||
72 | $this->assertFileNotExists(self::$filename); | |
73 | $page->cache('<p>Some content</p>'); | |
74 | $this->assertFileNotExists(self::$filename); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Return a page's cached content | |
79 | */ | |
80 | public function testCachedVersion() | |
81 | { | |
82 | $page = new CachedPage(self::$testCacheDir, self::$url, true); | |
83 | ||
84 | $this->assertFileNotExists(self::$filename); | |
85 | $page->cache('<p>Some content</p>'); | |
86 | $this->assertFileExists(self::$filename); | |
87 | $this->assertEquals( | |
88 | '<p>Some content</p>', | |
89 | $page->cachedVersion() | |
90 | ); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Return a page's cached content - the file does not exist | |
95 | */ | |
96 | public function testCachedVersionNoFile() | |
97 | { | |
98 | $page = new CachedPage(self::$testCacheDir, self::$url, true); | |
99 | ||
100 | $this->assertFileNotExists(self::$filename); | |
101 | $this->assertEquals( | |
102 | null, | |
103 | $page->cachedVersion() | |
104 | ); | |
105 | } | |
106 | ||
107 | /** | |
108 | * Return a page's cached content - the page is not to be cached | |
109 | */ | |
110 | public function testNoCachedVersion() | |
111 | { | |
112 | $page = new CachedPage(self::$testCacheDir, self::$url, false); | |
113 | ||
114 | $this->assertFileNotExists(self::$filename); | |
115 | $this->assertEquals( | |
116 | null, | |
117 | $page->cachedVersion() | |
118 | ); | |
119 | } | |
120 | } |