Cache.php
<?php
namespace Liaison\Test\Compo;
class Cache extends \Taeluf\Tester {
public function testDeletingStaleCacheFiles(){
//setup
$lia = new \Liaison(['bare'=>true]);
$package = new \Liaison\Test\Mock\Package($lia);
$cache = new \Lia\Compo\Cache($package);
$lia->set('lia:cache.dir',$tDir = $this->cacheDir);
$lia->set('lia:cache.clearAfterXMinutes', 0);
// create cache files
$files = [];
$i=0;
while ($i++ < 20){
$name = uniqid();
$files['file-'.$name] = $name;
$files['meta-'.$name] = $name;
$lia->cacheFile($name, 'happy', -5);//it expired 5 seconds ago LOL
}
// verify cache files were created
$filesPresent = $files;
$dh = opendir($tDir);
while (($f=readdir($dh)) !== false){
unset($filesPresent[$f]);
}
closedir($dh);
if (count($filesPresent)>0){
throw new \Exception("Cache files were not created...");
}
// remove stale cache files
$cache->onEmitResponseSent();
// verify none of the uniqid() cache files remain
$filesDeleted = $files;
$dh = opendir($tDir);
while (($f=readdir($dh)) !== false){
unset($filesDeleted[$f]);
}
closedir($dh);
return true
&& $this->compare($files, $filesDeleted)
;
}
public function testExpiredCache(){
$lia = new \Liaison(['bare'=>true]);
$package = new \Liaison\Test\Mock\Package($lia);
$cache = new \Lia\Compo\Cache($package);
$lia->set('lia:cache.dir',$tDir = $this->cacheDir);
$tContent = "The \"justice\" system in America is punish-for-profit.";
$lia->cacheFile($tKey='the-justice.system',$tContent, -1);
return true
&& $this->compare($tContent, file_get_contents($tDir.'file-'.$tKey))
&& $this->compare(false, $lia->getCacheFile($tKey))
;
}
public function testCaching(){
$lia = new \Liaison(['bare'=>true]);
$package = new \Liaison\Test\Mock\Package($lia);
$cache = new \Lia\Compo\Cache($package);
$lia->set('lia:cache.dir',$tDir = $this->cacheDir);
$tContent = "The war on drugs disproportionately affected(affects) black people.";
$lia->cacheFile($tKey='war.on.drugs',$tContent);
return true
&& $this->compare($tContent, file_get_contents($tDir.'file-'.$tKey))
&& $this->compare($tContent, file_get_contents($lia->getCacheFile($tKey)))
&& $this->compare($tContent, file_get_contents($lia->api('lia:cache.get', $tKey)))
;
}
protected $cacheDir = __DIR__.'/../extra/Cache/';
}
Cache::runAll();