2015-07-09 22:14:39 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cache tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
// required to access $_SESSION array
|
|
|
|
session_start();
|
|
|
|
|
|
|
|
require_once 'application/Cache.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unitary tests for cached pages
|
|
|
|
*/
|
2015-11-24 02:52:22 +01:00
|
|
|
class CacheTest extends PHPUnit_Framework_TestCase
|
2015-07-09 22:14:39 +02:00
|
|
|
{
|
|
|
|
// test cache directory
|
2015-11-24 02:52:22 +01:00
|
|
|
protected static $testCacheDir = 'sandbox/dummycache';
|
2015-07-09 22:14:39 +02:00
|
|
|
|
|
|
|
// 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);
|
2015-08-13 21:39:51 +02:00
|
|
|
} else {
|
|
|
|
array_map('unlink', glob(self::$testCacheDir.'/*'));
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
2015-12-03 19:27:34 +01:00
|
|
|
|
2015-07-09 22:14:39 +02:00
|
|
|
foreach (self::$pages as $page) {
|
|
|
|
file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
|
|
|
|
}
|
2015-08-13 21:39:51 +02:00
|
|
|
file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere');
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 19:27:34 +01:00
|
|
|
/**
|
|
|
|
* Remove dummycache folder after each tests.
|
|
|
|
*/
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
array_map('unlink', glob(self::$testCacheDir.'/*'));
|
|
|
|
rmdir(self::$testCacheDir);
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:14:39 +02:00
|
|
|
/**
|
|
|
|
* Purge cached pages
|
|
|
|
*/
|
|
|
|
public function testPurgeCachedPages()
|
|
|
|
{
|
|
|
|
purgeCachedPages(self::$testCacheDir);
|
|
|
|
foreach (self::$pages as $page) {
|
|
|
|
$this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
|
2015-08-13 21:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertFileExists(self::$testCacheDir.'/intru.der');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge cached pages - missing directory
|
|
|
|
*/
|
|
|
|
public function testPurgeCachedPagesMissingDir()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2015-11-24 02:52:22 +01:00
|
|
|
'Cannot purge sandbox/dummycache_missing: no directory',
|
2015-08-13 21:39:51 +02:00
|
|
|
purgeCachedPages(self::$testCacheDir.'_missing')
|
|
|
|
);
|
2015-07-09 22:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|