Extract cache interface
Refactoring to allow for alternative cache implementations. Includes implementations for the default noop (i.e. un-cached) behavior and a filesystem-based cache whose configuration is consistent with the existing Mustache_Engine constructor. Includes updated and new tests.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group functional
|
||||
*/
|
||||
class Mustache_Test_Cache_FilesystemCacheTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
private static $tempDir;
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::$tempDir = sys_get_temp_dir() . '/mustache_test';
|
||||
if (file_exists(self::$tempDir)) {
|
||||
self::rmdir(self::$tempDir);
|
||||
}
|
||||
}
|
||||
|
||||
public function testCacheGetNone()
|
||||
{
|
||||
$key = 'some key';
|
||||
$cache = new Mustache_Cache_FilesystemCache(self::$tempDir);;
|
||||
$cached = $cache->get($key);
|
||||
|
||||
$this->assertNull($cached);
|
||||
}
|
||||
|
||||
public function testCachePut()
|
||||
{
|
||||
$key = 'some key';
|
||||
$value = 'some value';
|
||||
$cache = new Mustache_Cache_FilesystemCache(self::$tempDir);;
|
||||
$cache->put($key, $value);
|
||||
$cached = $cache->get($key);
|
||||
|
||||
$this->assertEquals($cached, $value);
|
||||
}
|
||||
|
||||
private static function rmdir($path)
|
||||
{
|
||||
$path = rtrim($path, '/').'/';
|
||||
$handle = opendir($path);
|
||||
while (($file = readdir($handle)) !== false) {
|
||||
if ($file == '.' || $file == '..') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fullpath = $path.$file;
|
||||
if (is_dir($fullpath)) {
|
||||
self::rmdir($fullpath);
|
||||
} else {
|
||||
unlink($fullpath);
|
||||
}
|
||||
}
|
||||
|
||||
closedir($handle);
|
||||
rmdir($path);
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,7 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($mustache->hasHelper('foo'));
|
||||
$this->assertTrue($mustache->hasHelper('bar'));
|
||||
$this->assertFalse($mustache->hasHelper('baz'));
|
||||
$this->assertInstanceOf('Mustache_Cache_FilesystemCache', $mustache->getCache());
|
||||
}
|
||||
|
||||
public static function getFoo()
|
||||
@@ -95,6 +96,7 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
|
||||
$parser = new Mustache_Parser;
|
||||
$compiler = new Mustache_Compiler;
|
||||
$mustache = new Mustache_Engine;
|
||||
$cache = new Mustache_Cache_FilesystemCache(sys_get_temp_dir());
|
||||
|
||||
$this->assertNotSame($logger, $mustache->getLogger());
|
||||
$mustache->setLogger($logger);
|
||||
@@ -119,6 +121,10 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertNotSame($compiler, $mustache->getCompiler());
|
||||
$mustache->setCompiler($compiler);
|
||||
$this->assertSame($compiler, $mustache->getCompiler());
|
||||
|
||||
$this->assertNotSame($cache, $mustache->getCache());
|
||||
$mustache->setCache($cache);
|
||||
$this->assertSame($cache, $mustache->getCache());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,10 +140,8 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
|
||||
$source = '{{ foo }}';
|
||||
$template = $mustache->loadTemplate($source);
|
||||
$className = $mustache->getTemplateClassName($source);
|
||||
$fileName = self::$tempDir . '/' . $className . '.php';
|
||||
|
||||
$this->assertInstanceOf($className, $template);
|
||||
$this->assertFileExists($fileName);
|
||||
$this->assertContains("\nclass $className extends Mustache_Template", file_get_contents($fileName));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -290,7 +294,7 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
|
||||
$result = $mustache->render('{{ foo }}', array('foo' => 'FOO'));
|
||||
$this->assertEquals('FOO', $result);
|
||||
|
||||
$this->assertContains('WARNING: Template cache disabled, evaluating', file_get_contents($name));
|
||||
$this->assertContains('WARNING: Template cache disabled', file_get_contents($name));
|
||||
}
|
||||
|
||||
public function testLoggingIsNotTooAnnoying()
|
||||
|
||||
Reference in New Issue
Block a user