mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Fix broken test
This commit is contained in:
parent
ee9cd9b746
commit
2acb0feff9
|
@ -1,6 +1,8 @@
|
||||||
Tag {import}
|
Tag {import}
|
||||||
============
|
============
|
||||||
|
|
||||||
|
Import [macro](./macro.md) from another template
|
||||||
|
|
||||||
```smarty
|
```smarty
|
||||||
{import 'page.macros.tpl'}
|
{import 'page.macros.tpl'}
|
||||||
```
|
```
|
||||||
|
|
|
@ -75,10 +75,6 @@ class Aspect {
|
||||||
* @var array Templates storage
|
* @var array Templates storage
|
||||||
*/
|
*/
|
||||||
protected $_storage = array();
|
protected $_storage = array();
|
||||||
/**
|
|
||||||
* @var array template directory
|
|
||||||
*/
|
|
||||||
protected $_tpl_path = array();
|
|
||||||
/**
|
/**
|
||||||
* @var string compile directory
|
* @var string compile directory
|
||||||
*/
|
*/
|
||||||
|
@ -115,7 +111,6 @@ class Aspect {
|
||||||
"truncate" => 'Aspect\Modifier::truncate',
|
"truncate" => 'Aspect\Modifier::truncate',
|
||||||
"escape" => 'Aspect\Modifier::escape',
|
"escape" => 'Aspect\Modifier::escape',
|
||||||
"e" => 'Aspect\Modifier::escape', // alias of escape
|
"e" => 'Aspect\Modifier::escape', // alias of escape
|
||||||
"url" => 'urlencode', // alias of escape:"url"
|
|
||||||
"unescape" => 'Aspect\Modifier::unescape',
|
"unescape" => 'Aspect\Modifier::unescape',
|
||||||
"strip" => 'Aspect\Modifier::strip',
|
"strip" => 'Aspect\Modifier::strip',
|
||||||
"default" => 'Aspect\Modifier::defaultValue'
|
"default" => 'Aspect\Modifier::defaultValue'
|
||||||
|
@ -220,6 +215,15 @@ class Aspect {
|
||||||
'type' => self::BLOCK_COMPILER,
|
'type' => self::BLOCK_COMPILER,
|
||||||
'open' => 'Aspect\Compiler::filterOpen',
|
'open' => 'Aspect\Compiler::filterOpen',
|
||||||
'close' => 'Aspect\Compiler::filterClose'
|
'close' => 'Aspect\Compiler::filterClose'
|
||||||
|
),
|
||||||
|
'macro' => array(
|
||||||
|
'type' => self::BLOCK_COMPILER,
|
||||||
|
'open' => 'Aspect\Compiler::macroOpen',
|
||||||
|
'close' => 'Aspect\Compiler::macroClose'
|
||||||
|
),
|
||||||
|
'import' => array(
|
||||||
|
'type' => self::INLINE_COMPILER,
|
||||||
|
'parser' => 'Aspect\Compiler::tagImport'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -611,23 +615,18 @@ class Aspect {
|
||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove all compiled templates.
|
|
||||||
*
|
|
||||||
* @param string $scm
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function compileAll($scm = null) {
|
|
||||||
//return FS::rm($this->_compile_dir.'/*');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $tpl
|
* @param string $tpl
|
||||||
|
* @param bool $cache
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function clearCompiledTemplate($tpl) {
|
public function clearCompiledTemplate($tpl, $cache = true) {
|
||||||
$file_name = $this->_compile_dir."/".$this->_getHash($tpl);
|
$file_name = $this->_compile_dir."/".$this->_getHash($tpl);
|
||||||
if(file_exists($file_name)) {
|
if(file_exists($file_name)) {
|
||||||
|
if($cache) {
|
||||||
|
unset($this->_storage[$tpl]);
|
||||||
|
}
|
||||||
return unlink($file_name);
|
return unlink($file_name);
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
|
@ -635,10 +634,10 @@ class Aspect {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
*
|
||||||
*/
|
*/
|
||||||
public function clearAllCompiles() {
|
public function clearAllCompiles() {
|
||||||
|
\Aspect\FSProvider::clean($this->_compile_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -688,9 +688,10 @@ class Compiler {
|
||||||
*
|
*
|
||||||
* @param Tokenizer $tokens
|
* @param Tokenizer $tokens
|
||||||
* @param Scope $scope
|
* @param Scope $scope
|
||||||
|
* @throws ImproperUseException
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function macrosOpen(Tokenizer $tokens, Scope $scope) {
|
public static function macroOpen(Tokenizer $tokens, Scope $scope) {
|
||||||
$tokens->get('.');
|
$tokens->get('.');
|
||||||
$name = $tokens->get(Tokenizer::MACRO_STRING);
|
$name = $tokens->get(Tokenizer::MACRO_STRING);
|
||||||
if($tokens->is('(')) {
|
if($tokens->is('(')) {
|
||||||
|
@ -706,7 +707,7 @@ class Compiler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function macrosClose(Tokenizer $tokens, Scope $scope) {
|
public static function macroClose(Tokenizer $tokens, Scope $scope) {
|
||||||
$scope->tpl->_macros[ $scope["name"] ] = $scope->getContent();
|
$scope->tpl->_macros[ $scope["name"] ] = $scope->getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ class FSProvider implements ProviderInterface {
|
||||||
foreach($iterator as $file) {
|
foreach($iterator as $file) {
|
||||||
/* @var \splFileInfo $file*/
|
/* @var \splFileInfo $file*/
|
||||||
if($file->isFile()) {
|
if($file->isFile()) {
|
||||||
if(strpos($file->getBasename(), ",") !== 0) {
|
if(strpos($file->getBasename(), ".") !== 0) {
|
||||||
unlink($file->getRealPath());
|
unlink($file->getRealPath());
|
||||||
}
|
}
|
||||||
} elseif($file->isDir()) {
|
} elseif($file->isDir()) {
|
||||||
|
@ -46,10 +46,6 @@ class FSProvider implements ProviderInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function put($path, $content) {
|
|
||||||
file_put_contents($path, $content);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __construct($template_dir) {
|
public function __construct($template_dir) {
|
||||||
if($_dir = realpath($template_dir)) {
|
if($_dir = realpath($template_dir)) {
|
||||||
$this->_path = $_dir;
|
$this->_path = $_dir;
|
||||||
|
|
|
@ -1,37 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Aspect\Render,
|
use Aspect\Render,
|
||||||
Aspect\Misc;
|
Aspect\FSProvider as FS;
|
||||||
|
|
||||||
class AspectTest extends \PHPUnit_Framework_TestCase {
|
class AspectTest extends \Aspect\TestCase {
|
||||||
/**
|
|
||||||
* @var Aspect
|
|
||||||
*/
|
|
||||||
public $aspect;
|
|
||||||
|
|
||||||
public static function tearDownAfterClass() {
|
|
||||||
Misc::clean(ASPECT_RESOURCES.'/compile');
|
|
||||||
Misc::rm(ASPECT_RESOURCES.'/template/custom.tpl');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setUp() {
|
|
||||||
if(!file_exists(ASPECT_RESOURCES.'/compile')) {
|
|
||||||
mkdir(ASPECT_RESOURCES.'/compile', 0777, true);
|
|
||||||
}
|
|
||||||
self::tearDownAfterClass();
|
|
||||||
$this->aspect = $aspect = Aspect::factory(ASPECT_RESOURCES.'/template', ASPECT_RESOURCES.'/compile');
|
|
||||||
$aspect->setCompileDir(ASPECT_RESOURCES.'/compile');
|
|
||||||
$aspect->setForceCompile(false);
|
|
||||||
$aspect->setCompileCheck(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function tpl($code) {
|
|
||||||
Misc::put(ASPECT_RESOURCES.'/template/custom.tpl', $code);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function rmTpl() {
|
|
||||||
Misc::rm(ASPECT_RESOURCES.'/template/custom.tpl');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testAddRender() {
|
public function testAddRender() {
|
||||||
$test = $this;
|
$test = $this;
|
||||||
|
@ -40,7 +12,8 @@ class AspectTest extends \PHPUnit_Framework_TestCase {
|
||||||
$test->assertInstanceOf('Aspect\Render', $tpl);
|
$test->assertInstanceOf('Aspect\Render', $tpl);
|
||||||
echo "Inline render";
|
echo "Inline render";
|
||||||
}, array(
|
}, array(
|
||||||
"name" => 'render.tpl'
|
"name" => 'render.tpl',
|
||||||
|
"scm" => false
|
||||||
)));
|
)));
|
||||||
|
|
||||||
$this->assertSame("Inline render", $this->aspect->fetch('render.tpl', array()));
|
$this->assertSame("Inline render", $this->aspect->fetch('render.tpl', array()));
|
||||||
|
@ -51,68 +24,69 @@ class AspectTest extends \PHPUnit_Framework_TestCase {
|
||||||
"a" => "a",
|
"a" => "a",
|
||||||
"b" => "b"
|
"b" => "b"
|
||||||
);
|
);
|
||||||
|
$this->tpl('template1.tpl', 'Template 1 a');
|
||||||
|
$this->tpl('template2.tpl', 'Template 2 b');
|
||||||
$this->assertSame("Template 1 a", $this->aspect->fetch('template1.tpl', $a));
|
$this->assertSame("Template 1 a", $this->aspect->fetch('template1.tpl', $a));
|
||||||
$this->assertSame("Template 2 b", $this->aspect->fetch('template2.tpl', $a));
|
$this->assertSame("Template 2 b", $this->aspect->fetch('template2.tpl', $a));
|
||||||
$this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template1.tpl'));
|
$this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template1.tpl'));
|
||||||
$this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template2.tpl'));
|
$this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template2.tpl'));
|
||||||
$this->assertSame(2, iterator_count(new FilesystemIterator(ASPECT_RESOURCES.'/compile')));
|
$this->assertSame(3, iterator_count(new FilesystemIterator(ASPECT_RESOURCES.'/compile')));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testStorage() {
|
public function testStorage() {
|
||||||
$this->tpl('Custom template');
|
$this->tpl('custom.tpl', 'Custom template');
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
$this->rmTpl();
|
$this->aspect->clearCompiledTemplate('custom.tpl', false);
|
||||||
|
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
|
|
||||||
$this->tpl('Custom template 2');
|
$this->tpl('custom.tpl', 'Custom template 2');
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCheckMTime() {
|
public function testCheckMTime() {
|
||||||
$this->aspect->setCompileCheck(true);
|
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
|
||||||
$this->tpl('Custom template');
|
$this->tpl('custom.tpl', 'Custom template');
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$this->tpl('Custom template (new)');
|
$this->tpl('custom.tpl', 'Custom template (new)');
|
||||||
$this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testForceCompile() {
|
public function testForceCompile() {
|
||||||
$this->aspect->setForceCompile(true);
|
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
|
||||||
$this->tpl('Custom template');
|
$this->tpl('custom.tpl', 'Custom template');
|
||||||
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
|
||||||
$this->tpl('Custom template (new)');
|
$this->tpl('custom.tpl', 'Custom template (new)');
|
||||||
$this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetModifier() {
|
public function testSetModifier() {
|
||||||
$this->aspect->addModifier("mymod", "myMod");
|
$this->aspect->addModifier("mymod", "myMod");
|
||||||
$this->tpl('Custom modifier {$a|mymod}');
|
$this->tpl('custom.tpl', 'Custom modifier {$a|mymod}');
|
||||||
$this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->aspect->fetch('custom.tpl', array("a" => "Custom")));
|
$this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->aspect->fetch('custom.tpl', array("a" => "Custom")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetFunctions() {
|
public function testSetFunctions() {
|
||||||
$this->aspect->setForceCompile(true);
|
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
|
||||||
$this->aspect->addFunction("myfunc", "myFunc");
|
$this->aspect->addFunction("myfunc", "myFunc");
|
||||||
$this->aspect->addBlockFunction("myblockfunc", "myBlockFunc");
|
$this->aspect->addBlockFunction("myblockfunc", "myBlockFunc");
|
||||||
$this->tpl('Custom function {myfunc name="foo"}');
|
$this->tpl('custom.tpl', 'Custom function {myfunc name="foo"}');
|
||||||
$this->assertSame("Custom function MyFunc:foo", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom function MyFunc:foo", $this->aspect->fetch('custom.tpl', array()));
|
||||||
$this->tpl('Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}');
|
$this->tpl('custom.tpl', 'Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}');
|
||||||
$this->assertSame("Custom function Block:foo:this block1:Block", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom function Block:foo:this block1:Block", $this->aspect->fetch('custom.tpl', array()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetCompilers() {
|
public function testSetCompilers() {
|
||||||
$this->aspect->setForceCompile(true);
|
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
|
||||||
$this->aspect->addCompiler("mycompiler", 'myCompiler');
|
$this->aspect->addCompiler("mycompiler", 'myCompiler');
|
||||||
$this->aspect->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
|
$this->aspect->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
|
||||||
'tag' => 'myBlockCompilerTag'
|
'tag' => 'myBlockCompilerTag'
|
||||||
));
|
));
|
||||||
$this->tpl('Custom compiler {mycompiler name="bar"}');
|
$this->tpl('custom.tpl', 'Custom compiler {mycompiler name="bar"}');
|
||||||
$this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar)", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar)", $this->aspect->fetch('custom.tpl', array()));
|
||||||
$this->tpl('Custom compiler {myblockcompiler name="bar"} block1 {tag name="baz"} block2 {/myblockcompiler}');
|
$this->tpl('custom.tpl', 'Custom compiler {myblockcompiler name="bar"} block1 {tag name="baz"} block2 {/myblockcompiler}');
|
||||||
$this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar) block1 Tag baz of compiler block2 End of compiler", $this->aspect->fetch('custom.tpl', array()));
|
$this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar) block1 Tag baz of compiler block2 End of compiler", $this->aspect->fetch('custom.tpl', array()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user