2015-07-09 22:14:39 +02:00
|
|
|
<?php
|
2021-04-05 09:39:34 +02:00
|
|
|
|
2015-07-09 22:14:39 +02:00
|
|
|
/**
|
|
|
|
* PageCache tests
|
|
|
|
*/
|
2021-04-05 09:39:34 +02:00
|
|
|
|
2018-12-03 00:08:04 +01:00
|
|
|
namespace Shaarli\Feed;
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unitary tests for cached pages
|
|
|
|
*/
|
2020-09-29 14:41:40 +02:00
|
|
|
class CachedPageTest extends \Shaarli\TestCase
|
2015-07-09 22:14:39 +02:00
|
|
|
{
|
|
|
|
// test cache directory
|
2015-11-24 02:52:22 +01:00
|
|
|
protected static $testCacheDir = 'sandbox/pagecache';
|
2020-06-13 13:08:01 +02:00
|
|
|
protected static $url = 'http://shaar.li/feed/atom';
|
2015-07-09 22:14:39 +02:00
|
|
|
protected static $filename;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the cache directory if needed
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
public static function setUpBeforeClass(): void
|
2015-07-09 22:14:39 +02:00
|
|
|
{
|
2018-12-03 00:08:04 +01:00
|
|
|
if (!is_dir(self::$testCacheDir)) {
|
2015-07-09 22:14:39 +02:00
|
|
|
mkdir(self::$testCacheDir);
|
|
|
|
}
|
2018-12-03 00:08:04 +01:00
|
|
|
self::$filename = self::$testCacheDir . '/' . sha1(self::$url) . '.cache';
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the page cache
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function setUp(): void
|
2015-07-09 22:14:39 +02:00
|
|
|
{
|
|
|
|
if (file_exists(self::$filename)) {
|
|
|
|
unlink(self::$filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new cached page
|
|
|
|
*/
|
|
|
|
public function testConstruct()
|
|
|
|
{
|
2020-12-17 15:43:33 +01:00
|
|
|
new CachedPage(self::$testCacheDir, '', true, null);
|
|
|
|
new CachedPage(self::$testCacheDir, '', false, null);
|
|
|
|
new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/rss', true, null);
|
|
|
|
new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/atom', false, null);
|
2019-08-10 12:31:32 +02:00
|
|
|
$this->addToAssertionCount(1);
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache a page's content
|
|
|
|
*/
|
|
|
|
public function testCache()
|
|
|
|
{
|
2020-12-17 15:43:33 +01:00
|
|
|
$page = new CachedPage(self::$testCacheDir, self::$url, true, null);
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
$page->cache('<p>Some content</p>');
|
|
|
|
$this->assertFileExists(self::$filename);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Some content</p>',
|
|
|
|
file_get_contents(self::$filename)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Cache" a page's content - the page is not to be cached
|
|
|
|
*/
|
|
|
|
public function testShouldNotCache()
|
|
|
|
{
|
2020-12-17 15:43:33 +01:00
|
|
|
$page = new CachedPage(self::$testCacheDir, self::$url, false, null);
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
$page->cache('<p>Some content</p>');
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a page's cached content
|
|
|
|
*/
|
|
|
|
public function testCachedVersion()
|
|
|
|
{
|
2020-12-17 15:43:33 +01:00
|
|
|
$page = new CachedPage(self::$testCacheDir, self::$url, true, null);
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
$page->cache('<p>Some content</p>');
|
|
|
|
$this->assertFileExists(self::$filename);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Some content</p>',
|
|
|
|
$page->cachedVersion()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a page's cached content - the file does not exist
|
|
|
|
*/
|
|
|
|
public function testCachedVersionNoFile()
|
|
|
|
{
|
2020-12-17 15:43:33 +01:00
|
|
|
$page = new CachedPage(self::$testCacheDir, self::$url, true, null);
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
$this->assertEquals(
|
|
|
|
null,
|
|
|
|
$page->cachedVersion()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a page's cached content - the page is not to be cached
|
|
|
|
*/
|
|
|
|
public function testNoCachedVersion()
|
|
|
|
{
|
2020-12-17 15:43:33 +01:00
|
|
|
$page = new CachedPage(self::$testCacheDir, self::$url, false, null);
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
$this->assertEquals(
|
|
|
|
null,
|
|
|
|
$page->cachedVersion()
|
|
|
|
);
|
|
|
|
}
|
2020-12-17 15:43:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a page's cached content within date period
|
|
|
|
*/
|
|
|
|
public function testCachedVersionInDatePeriod()
|
|
|
|
{
|
|
|
|
$period = new \DatePeriod(
|
|
|
|
new \DateTime('yesterday'),
|
|
|
|
new \DateInterval('P1D'),
|
|
|
|
new \DateTime('tomorrow')
|
|
|
|
);
|
|
|
|
$page = new CachedPage(self::$testCacheDir, self::$url, true, $period);
|
|
|
|
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
$page->cache('<p>Some content</p>');
|
|
|
|
$this->assertFileExists(self::$filename);
|
|
|
|
$this->assertEquals(
|
|
|
|
'<p>Some content</p>',
|
|
|
|
$page->cachedVersion()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a page's cached content outside of date period
|
|
|
|
*/
|
|
|
|
public function testCachedVersionNotInDatePeriod()
|
|
|
|
{
|
|
|
|
$period = new \DatePeriod(
|
|
|
|
new \DateTime('yesterday noon'),
|
|
|
|
new \DateInterval('P1D'),
|
|
|
|
new \DateTime('yesterday midnight')
|
|
|
|
);
|
|
|
|
$page = new CachedPage(self::$testCacheDir, self::$url, true, $period);
|
|
|
|
|
|
|
|
$this->assertFileNotExists(self::$filename);
|
|
|
|
$page->cache('<p>Some content</p>');
|
|
|
|
$this->assertFileExists(self::$filename);
|
|
|
|
$this->assertNull($page->cachedVersion());
|
|
|
|
}
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|