mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Add tests
This commit is contained in:
@@ -113,6 +113,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
* @param string $result expected result.
|
||||
* @param int $options
|
||||
* @param bool $dump dump source and result code (for debug)
|
||||
* @return \Fenom\Template
|
||||
*/
|
||||
public function exec($code, $vars, $result, $options = 0, $dump = false)
|
||||
{
|
||||
@@ -122,6 +123,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody() . "\n========= DUMP END =============\n";
|
||||
}
|
||||
$this->assertSame(Modifier::strip($result), Modifier::strip($tpl->fetch($vars), true), "Test $code");
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
public function execTpl($name, $code, $vars, $result, $dump = false)
|
||||
@@ -270,37 +272,3 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Fake implements \ArrayAccess
|
||||
{
|
||||
public $vars;
|
||||
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
if ($offset == "object") {
|
||||
return new self();
|
||||
} else {
|
||||
return new self($offset);
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->vars[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->vars[$offset]);
|
||||
}
|
||||
|
||||
public function proxy()
|
||||
{
|
||||
return implode(", ", func_get_args());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ class CustomProviderTest extends TestCase
|
||||
|
||||
public function testCustom()
|
||||
{
|
||||
$this->assertTrue($this->fenom->templateExists('my:include.tpl'));
|
||||
$this->assertFalse($this->fenom->templateExists('my:include-none.tpl'));
|
||||
$this->assertRender("start: {include 'my:include.tpl'}", 'start: include template');
|
||||
//$this->assertRender("start: {import 'my:macros.tpl' as ops} {ops.add a=3 b=6}");
|
||||
}
|
||||
|
||||
@@ -229,6 +229,7 @@ class TemplateTest extends TestCase
|
||||
$result4 = 'Include <b>Welcome, Flame (flame@dev.null)</b> template';
|
||||
return array(
|
||||
array('Include {include "welcome.tpl"} template', $a, $result),
|
||||
array('Include {include "welcome.tpl"} template', $a, $result, Fenom::FORCE_INCLUDE),
|
||||
array('Include {include $tpl} template', $a, $result),
|
||||
array('Include {include "$tpl"} template', $a, $result),
|
||||
array('Include {include "{$tpl}"} template', $a, $result),
|
||||
@@ -238,7 +239,9 @@ class TemplateTest extends TestCase
|
||||
array('Include {include "wel{$fragment}.tpl"} template', $a, $result),
|
||||
array('Include {include "wel{$pr_fragment|lower}.tpl"} template', $a, $result),
|
||||
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2),
|
||||
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2, Fenom::FORCE_INCLUDE),
|
||||
array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3),
|
||||
array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3, Fenom::FORCE_INCLUDE),
|
||||
array('Include {include "welcome.tpl" username="Flame" email="flame@dev.null"} template',
|
||||
$a, $result4),
|
||||
);
|
||||
@@ -252,6 +255,40 @@ class TemplateTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerInsert()
|
||||
{
|
||||
$a = array(
|
||||
"name" => "welcome",
|
||||
"tpl" => "welcome.tpl",
|
||||
"fragment" => "come",
|
||||
"pr_fragment" => "Come",
|
||||
"pr_name" => "Welcome",
|
||||
"username" => "Master",
|
||||
"email" => "dev@null.net"
|
||||
);
|
||||
$result = 'Include <b>Welcome, Master (dev@null.net)</b> template';
|
||||
return array(
|
||||
array('Include {insert "welcome.tpl"} template', $a, $result),
|
||||
array("Include {insert 'welcome.tpl'} template", $a, $result),
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerInsertInvalid()
|
||||
{
|
||||
return array(
|
||||
array('Include {insert} template', 'Fenom\Error\CompileException', "Unexpected end of expression"),
|
||||
array('Include {insert another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Template another not found"),
|
||||
array('Include {insert $tpl} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
|
||||
array('Include {insert "$tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
|
||||
array('Include {insert "{$tpl}"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
|
||||
array('Include {insert "$name.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
|
||||
array('Include {insert "{$name}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
|
||||
array('Include {insert "{$pr_name|lower}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
|
||||
array('Include {insert "wel{$fragment}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
|
||||
array('Include {insert "welcome.tpl" email="flame@dev.null"} template', 'Fenom\Error\CompileException', "Unexpected token 'email'"),
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerIf()
|
||||
{
|
||||
$a = array(
|
||||
@@ -783,9 +820,9 @@ class TemplateTest extends TestCase
|
||||
* @group include
|
||||
* @dataProvider providerInclude
|
||||
*/
|
||||
public function testInclude($code, $vars, $result)
|
||||
public function testInclude($code, $vars, $result, $options = 0)
|
||||
{
|
||||
$this->exec($code, $vars, $result);
|
||||
$this->exec($code, $vars, $result, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -796,6 +833,27 @@ class TemplateTest extends TestCase
|
||||
$this->execError($code, $exception, $message, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group insert
|
||||
* @dataProvider providerInsert
|
||||
*/
|
||||
public function testInsert($code, $vars, $result)
|
||||
{
|
||||
$this->tpl('insert.tpl', $code);
|
||||
$this->assertRender('insert.tpl', $result, $vars);
|
||||
$tpl = $this->exec($code, $vars, $result);
|
||||
$this->assertTrue($tpl->isValid());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group insert
|
||||
* @dataProvider providerInsertInvalid
|
||||
*/
|
||||
public function testInsertInvalid($code, $exception, $message, $options = 0)
|
||||
{
|
||||
$this->execError($code, $exception, $message, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerIf
|
||||
* @group test-if
|
||||
|
||||
@@ -10,7 +10,7 @@ class FenomTest extends \Fenom\TestCase
|
||||
{
|
||||
return array(
|
||||
array("disable_methods", Fenom::DENY_METHODS),
|
||||
array("disable_native_funcs", Fenom::DENY_INLINE_FUNCS),
|
||||
array("disable_native_funcs", Fenom::DENY_NATIVE_FUNCS),
|
||||
array("disable_cache", Fenom::DISABLE_CACHE),
|
||||
array("force_compile", Fenom::FORCE_COMPILE),
|
||||
array("auto_reload", Fenom::AUTO_RELOAD),
|
||||
@@ -20,6 +20,18 @@ class FenomTest extends \Fenom\TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreating() {
|
||||
$time = $this->tpl('temp.tpl', 'Template 1 a');
|
||||
$fenom = new Fenom($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'));
|
||||
$fenom->setCompileDir(FENOM_RESOURCES . '/compile');
|
||||
$this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl'));
|
||||
$this->assertSame($provider, $tpl->getProvider());
|
||||
$this->assertSame('temp.tpl', $tpl->getBaseName());
|
||||
$this->assertSame('temp.tpl', $tpl->getName());
|
||||
$this->assertSame($time, $tpl->getTime());
|
||||
$fenom->clearAllCompiles();
|
||||
}
|
||||
|
||||
public function testCompileFile()
|
||||
{
|
||||
$a = array(
|
||||
@@ -43,14 +55,19 @@ class FenomTest extends \Fenom\TestCase
|
||||
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group testCheckMTime
|
||||
*/
|
||||
public function testCheckMTime()
|
||||
{
|
||||
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
|
||||
$this->tpl('custom.tpl', 'Custom template');
|
||||
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
|
||||
|
||||
sleep(1);
|
||||
$tpl = $this->fenom->getTemplate('custom.tpl');
|
||||
$this->assertTrue($tpl->isValid());
|
||||
usleep(1.5e6);
|
||||
$this->tpl('custom.tpl', 'Custom template (new)');
|
||||
$this->assertFalse($tpl->isValid());
|
||||
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
|
||||
}
|
||||
|
||||
@@ -133,4 +150,45 @@ class FenomTest extends \Fenom\TestCase
|
||||
|
||||
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} god {/var} world')->fetch(array()));
|
||||
}
|
||||
}
|
||||
|
||||
public function testAddInlineCompilerSmart() {
|
||||
$this->fenom->addCompilerSmart('SayA','TestTags');
|
||||
$this->tpl('inline_compiler.tpl', 'I just {SayA}.');
|
||||
$this->assertSame('I just Say A.', $this->fenom->fetch('inline_compiler.tpl', array()));
|
||||
}
|
||||
|
||||
public function testAddBlockCompilerSmart() {
|
||||
$this->fenom->addBlockCompilerSmart('SayBlock', 'TestTags', array('SaySomething'), array('SaySomething'));
|
||||
$this->tpl('block_compiler.tpl', '{SayBlock} and {SaySomething}. It is all, {/SayBlock}');
|
||||
$this->assertSame('Start saying and say blah-blah-blah. It is all, Stop saying',
|
||||
$this->fenom->fetch('block_compiler.tpl', array()));
|
||||
}
|
||||
|
||||
public function testAddFunctions() {
|
||||
$this->fenom->setOptions(Fenom::DENY_NATIVE_FUNCS);
|
||||
$this->assertFalse($this->fenom->isAllowedFunction('substr'));
|
||||
$this->fenom->addAllowedFunctions(array('substr'));
|
||||
$this->assertTrue($this->fenom->isAllowedFunction('substr'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class TestTags {
|
||||
|
||||
public static function tagSayA() {
|
||||
return 'echo "Say A"';
|
||||
}
|
||||
|
||||
public static function SayBlockOpen() {
|
||||
return 'echo "Start saying"';
|
||||
}
|
||||
|
||||
public static function tagSaySomething() {
|
||||
return 'echo "say blah-blah-blah"';
|
||||
}
|
||||
|
||||
public static function SayBlockClose() {
|
||||
return 'echo "Stop saying"';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user