]>
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 | */ | |
a5a9cf23 | 10 | class CachedPageTest extends \Shaarli\TestCase |
01e48f26 V |
11 | { |
12 | // test cache directory | |
4bf35ba5 | 13 | protected static $testCacheDir = 'sandbox/pagecache'; |
9c75f877 | 14 | protected static $url = 'http://shaar.li/feed/atom'; |
01e48f26 V |
15 | protected static $filename; |
16 | ||
17 | /** | |
18 | * Create the cache directory if needed | |
19 | */ | |
8f60e120 | 20 | public static function setUpBeforeClass(): void |
01e48f26 | 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 | */ | |
8f60e120 | 31 | protected function setUp(): void |
01e48f26 V |
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 | { | |
f00600a2 A |
43 | new CachedPage(self::$testCacheDir, '', true, null); |
44 | new CachedPage(self::$testCacheDir, '', false, null); | |
45 | new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/rss', true, null); | |
46 | new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/atom', false, null); | |
def39d0d | 47 | $this->addToAssertionCount(1); |
01e48f26 V |
48 | } |
49 | ||
50 | /** | |
51 | * Cache a page's content | |
52 | */ | |
53 | public function testCache() | |
54 | { | |
f00600a2 | 55 | $page = new CachedPage(self::$testCacheDir, self::$url, true, null); |
01e48f26 V |
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 | { | |
f00600a2 | 71 | $page = new CachedPage(self::$testCacheDir, self::$url, false, null); |
01e48f26 V |
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 | { | |
f00600a2 | 83 | $page = new CachedPage(self::$testCacheDir, self::$url, true, null); |
01e48f26 V |
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 | { | |
f00600a2 | 99 | $page = new CachedPage(self::$testCacheDir, self::$url, true, null); |
01e48f26 V |
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 | { | |
f00600a2 | 113 | $page = new CachedPage(self::$testCacheDir, self::$url, false, null); |
01e48f26 V |
114 | |
115 | $this->assertFileNotExists(self::$filename); | |
116 | $this->assertEquals( | |
117 | null, | |
118 | $page->cachedVersion() | |
119 | ); | |
120 | } | |
f00600a2 A |
121 | |
122 | /** | |
123 | * Return a page's cached content within date period | |
124 | */ | |
125 | public function testCachedVersionInDatePeriod() | |
126 | { | |
127 | $period = new \DatePeriod( | |
128 | new \DateTime('yesterday'), | |
129 | new \DateInterval('P1D'), | |
130 | new \DateTime('tomorrow') | |
131 | ); | |
132 | $page = new CachedPage(self::$testCacheDir, self::$url, true, $period); | |
133 | ||
134 | $this->assertFileNotExists(self::$filename); | |
135 | $page->cache('<p>Some content</p>'); | |
136 | $this->assertFileExists(self::$filename); | |
137 | $this->assertEquals( | |
138 | '<p>Some content</p>', | |
139 | $page->cachedVersion() | |
140 | ); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Return a page's cached content outside of date period | |
145 | */ | |
146 | public function testCachedVersionNotInDatePeriod() | |
147 | { | |
148 | $period = new \DatePeriod( | |
149 | new \DateTime('yesterday noon'), | |
150 | new \DateInterval('P1D'), | |
151 | new \DateTime('yesterday midnight') | |
152 | ); | |
153 | $page = new CachedPage(self::$testCacheDir, self::$url, true, $period); | |
154 | ||
155 | $this->assertFileNotExists(self::$filename); | |
156 | $page->cache('<p>Some content</p>'); | |
157 | $this->assertFileExists(self::$filename); | |
158 | $this->assertNull($page->cachedVersion()); | |
159 | } | |
01e48f26 | 160 | } |