diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:07:13 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:07:13 +0200 |
commit | d9f6275ebca035fec8331652c677981056793ccc (patch) | |
tree | 37a64baf4f0eba6b781040605965383d8aded2cc /tests/feed/CachedPageTest.php | |
parent | 38672ba0d1c722e5d6d33a58255ceb55e9410e46 (diff) | |
parent | d63ff87a009313141ae684ec447b902562ff6ee7 (diff) | |
download | Shaarli-d9f6275ebca035fec8331652c677981056793ccc.tar.gz Shaarli-d9f6275ebca035fec8331652c677981056793ccc.tar.zst Shaarli-d9f6275ebca035fec8331652c677981056793ccc.zip |
Merge branch 'v0.11' into stablestable
Diffstat (limited to 'tests/feed/CachedPageTest.php')
-rw-r--r-- | tests/feed/CachedPageTest.php | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/feed/CachedPageTest.php b/tests/feed/CachedPageTest.php new file mode 100644 index 00000000..0bcc1442 --- /dev/null +++ b/tests/feed/CachedPageTest.php | |||
@@ -0,0 +1,120 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * PageCache tests | ||
4 | */ | ||
5 | namespace Shaarli\Feed; | ||
6 | |||
7 | /** | ||
8 | * Unitary tests for cached pages | ||
9 | */ | ||
10 | class CachedPageTest extends \PHPUnit\Framework\TestCase | ||
11 | { | ||
12 | // test cache directory | ||
13 | protected static $testCacheDir = 'sandbox/pagecache'; | ||
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 | { | ||
22 | if (!is_dir(self::$testCacheDir)) { | ||
23 | mkdir(self::$testCacheDir); | ||
24 | } | ||
25 | self::$filename = self::$testCacheDir . '/' . sha1(self::$url) . '.cache'; | ||
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 | } | ||