Remove need for wrapper class in unit tests

This commit is contained in:
Kijin Sung 2023-10-15 20:12:46 +09:00
parent ccc67d54e3
commit 936663f804
2 changed files with 9 additions and 28 deletions

View file

@ -269,14 +269,14 @@ class Template
// If a cached result does not exist, or if it is stale, compile again. // If a cached result does not exist, or if it is stale, compile again.
if (!Storage::exists($this->cache_path) || filemtime($this->cache_path) < $latest_mtime || !$this->cache_enabled) if (!Storage::exists($this->cache_path) || filemtime($this->cache_path) < $latest_mtime || !$this->cache_enabled)
{ {
$content = $this->_convert(); $content = $this->parse();
if (!Storage::write($this->cache_path, $content)) if (!Storage::write($this->cache_path, $content))
{ {
throw new Exception('Cannot write template cache file: ' . $this->cache_path); throw new Exception('Cannot write template cache file: ' . $this->cache_path);
} }
} }
$output = $this->_execute(); $output = $this->execute();
// Record the time elapsed. // Record the time elapsed.
$elapsed_time = microtime(true) - $start; $elapsed_time = microtime(true) - $start;
@ -307,8 +307,8 @@ class Template
return escape($error_message); return escape($error_message);
} }
// Convert, but don't actually execute it. // Parse the template, but don't actually execute it.
return $this->_convert(); return $this->parse();
} }
/** /**
@ -319,7 +319,7 @@ class Template
* *
* @return string * @return string
*/ */
protected function _convert(?string $content = null): string public function parse(?string $content = null): string
{ {
// Read the source, or use the provided content. // Read the source, or use the provided content.
if ($content === null && $this->exists) if ($content === null && $this->exists)
@ -361,7 +361,7 @@ class Template
* *
* @return string * @return string
*/ */
protected function _execute(): string public function execute(): string
{ {
// Import Context and lang as local variables. // Import Context and lang as local variables.
$__Context = $this->vars ?: \Context::getAll(); $__Context = $this->vars ?: \Context::getAll();

View file

@ -504,8 +504,7 @@ class TemplateParserV1Test extends \Codeception\TestCase\Test
foreach ($tests as $test) foreach ($tests as $test)
{ {
$tmpl = new TemplateHandlerWrapper; $tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'no_file.html');
$tmpl->init('./tests/_data/template', 'no_file.html');
$result = $tmpl->parse($test[0]); $result = $tmpl->parse($test[0]);
$between = str_starts_with($test[1], '?>') ? '' : ' '; $between = str_starts_with($test[1], '?>') ? '' : ' ';
$this->assertEquals($this->prefix . $between . $test[1], $result); $this->assertEquals($this->prefix . $between . $test[1], $result);
@ -514,35 +513,17 @@ class TemplateParserV1Test extends \Codeception\TestCase\Test
public function testParseNoContent() public function testParseNoContent()
{ {
$tmpl = new TemplateHandlerWrapper; $tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'no_file.html');
$tmpl->init('./tests/_data/template', 'no_file.html');
$result = $tmpl->parse(null); $result = $tmpl->parse(null);
$this->assertEquals('', $result); $this->assertEquals('', $result);
} }
public function testCompileDirect() public function testCompileDirect()
{ {
$tmpl = TemplateHandler::getInstance(); $tmpl = new \Rhymix\Framework\Template();
$result = $tmpl->compileDirect('./tests/_data/template', 'sample.html'); $result = $tmpl->compileDirect('./tests/_data/template', 'sample.html');
$result = trim($result); $result = trim($result);
$this->assertEquals($this->prefix . ' if($__Context->has_blog ?? false){ ?><a href="http://mygony.com">Taggon\'s blog</a><?php } ?>'.PHP_EOL.'<!--#Meta://external.host/js.js--><?php Context::loadFile([\'//external.host/js.js\', \'\', \'tests\', \'\']); ?>', $result); $this->assertEquals($this->prefix . ' if($__Context->has_blog ?? false){ ?><a href="http://mygony.com">Taggon\'s blog</a><?php } ?>'.PHP_EOL.'<!--#Meta://external.host/js.js--><?php Context::loadFile([\'//external.host/js.js\', \'\', \'tests\', \'\']); ?>', $result);
} }
} }
class TemplateHandlerWrapper extends \TemplateHandler {
private $inst;
function __construct() {
$this->inst = parent::getInstance();
}
public function init($tpl_path, $tpl_filename, $tpl_file = '') {
$this->inst->_setSourcePath($tpl_path, $tpl_filename, $tpl_file);
}
public function parse($buff = null) {
return $this->inst->_convert($buff);
}
}