CachedPage: move to a proper file, add tests

Modifications
 - rename `pageCache` to `CachedPage`
 - move utilities to `Cache`
 - do not access globals
 - apply coding rules
 - update LinkDB and test code
 - add test coverage

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2015-07-09 22:14:39 +02:00
parent 5ac5349ac0
commit 01e48f269d
8 changed files with 334 additions and 88 deletions

63
tests/CacheTest.php Normal file
View file

@ -0,0 +1,63 @@
<?php
/**
* Cache tests
*/
// required to access $_SESSION array
session_start();
require_once 'application/Cache.php';
/**
* Unitary tests for cached pages
*/
class CachedTest extends PHPUnit_Framework_TestCase
{
// test cache directory
protected static $testCacheDir = 'tests/dummycache';
// dummy cached file names / content
protected static $pages = array('a', 'toto', 'd7b59c');
/**
* Populate the cache with dummy files
*/
public function setUp()
{
if (! is_dir(self::$testCacheDir)) {
mkdir(self::$testCacheDir);
}
foreach (self::$pages as $page) {
file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
}
}
/**
* Purge cached pages
*/
public function testPurgeCachedPages()
{
purgeCachedPages(self::$testCacheDir);
foreach (self::$pages as $page) {
$this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
}
}
/**
* Purge cached pages and session cache
*/
public function testInvalidateCaches()
{
$this->assertArrayNotHasKey('tags', $_SESSION);
$_SESSION['tags'] = array('goodbye', 'cruel', 'world');
invalidateCaches(self::$testCacheDir);
foreach (self::$pages as $page) {
$this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
}
$this->assertArrayNotHasKey('tags', $_SESSION);
}
}

121
tests/CachedPageTest.php Normal file
View file

@ -0,0 +1,121 @@
<?php
/**
* PageCache tests
*/
require_once 'application/CachedPage.php';
/**
* Unitary tests for cached pages
*/
class CachedPageTest extends PHPUnit_Framework_TestCase
{
// test cache directory
protected static $testCacheDir = 'tests/pagecache';
protected static $url = 'http://shaar.li/?do=atom';
protected static $filename;
/**
* Create the cache directory if needed
*/
public static function setUpBeforeClass()
{
if (! is_dir(self::$testCacheDir)) {
mkdir(self::$testCacheDir);
}
self::$filename = self::$testCacheDir.'/'.sha1(self::$url).'.cache';
}
/**
* Reset the page cache
*/
public function setUp()
{
if (file_exists(self::$filename)) {
unlink(self::$filename);
}
}
/**
* Create a new cached page
*/
public function testConstruct()
{
new CachedPage(self::$testCacheDir, '', true);
new CachedPage(self::$testCacheDir, '', false);
new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=rss', true);
new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=atom', false);
}
/**
* Cache a page's content
*/
public function testCache()
{
$page = new CachedPage(self::$testCacheDir, self::$url, true);
$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()
{
$page = new CachedPage(self::$testCacheDir, self::$url, false);
$this->assertFileNotExists(self::$filename);
$page->cache('<p>Some content</p>');
$this->assertFileNotExists(self::$filename);
}
/**
* Return a page's cached content
*/
public function testCachedVersion()
{
$page = new CachedPage(self::$testCacheDir, self::$url, true);
$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()
{
$page = new CachedPage(self::$testCacheDir, self::$url, true);
$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()
{
$page = new CachedPage(self::$testCacheDir, self::$url, false);
$this->assertFileNotExists(self::$filename);
$this->assertEquals(
null,
$page->cachedVersion()
);
}
}

View file

@ -3,6 +3,7 @@
* Link datastore tests
*/
require_once 'application/Cache.php';
require_once 'application/LinkDB.php';
require_once 'application/Utils.php';
require_once 'tests/utils/ReferenceLinkDB.php';
@ -180,11 +181,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
'tags'=>'unit test'
);
$testDB[$link['linkdate']] = $link;
// TODO: move PageCache to a proper class/file
function invalidateCaches() {}
$testDB->savedb();
$testDB->savedb('tests');
$testDB = new LinkDB(self::$testDatastore, true, false);
$this->assertEquals($dbSize + 1, sizeof($testDB));
@ -514,4 +511,3 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
);
}
}
?>

View file

@ -125,4 +125,3 @@ class ReferenceLinkDB
return $this->_privateCount;
}
}
?>