fenom/tests/cases/FenomTest.php

138 lines
5.7 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
2013-06-28 11:53:53 +04:00
use Fenom\Render,
Fenom\Provider as FS;
2013-01-25 18:36:16 +04:00
2013-07-29 14:58:14 +04:00
class FenomTest extends \Fenom\TestCase
{
2013-01-25 18:36:16 +04:00
2013-07-29 14:58:14 +04:00
public static function providerOptions()
{
2013-07-23 11:32:31 +04:00
return array(
2013-07-29 14:58:14 +04:00
array("disable_methods", Fenom::DENY_METHODS),
array("disable_native_funcs", Fenom::DENY_INLINE_FUNCS),
array("disable_cache", Fenom::DISABLE_CACHE),
array("force_compile", Fenom::FORCE_COMPILE),
array("auto_reload", Fenom::AUTO_RELOAD),
array("force_include", Fenom::FORCE_INCLUDE),
array("auto_escape", Fenom::AUTO_ESCAPE),
array("force_verify", Fenom::FORCE_VERIFY)
2013-07-23 11:32:31 +04:00
);
}
2013-07-29 14:58:14 +04:00
public function testCompileFile()
{
2013-01-25 18:36:16 +04:00
$a = array(
"a" => "a",
"b" => "b"
);
2013-02-22 00:05:20 +04:00
$this->tpl('template1.tpl', 'Template 1 a');
$this->tpl('template2.tpl', 'Template 2 b');
2013-06-28 11:53:53 +04:00
$this->assertSame("Template 1 a", $this->fenom->fetch('template1.tpl', $a));
$this->assertSame("Template 2 b", $this->fenom->fetch('template2.tpl', $a));
$this->assertInstanceOf('Fenom\Render', $this->fenom->getTemplate('template1.tpl'));
$this->assertInstanceOf('Fenom\Render', $this->fenom->getTemplate('template2.tpl'));
2013-07-29 14:58:14 +04:00
$this->assertSame(3, iterator_count(new FilesystemIterator(FENOM_RESOURCES . '/compile')));
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
public function testStorage()
{
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom template');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom template 2');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
public function testCheckMTime()
{
2013-06-28 11:53:53 +04:00
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom template');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
2013-01-25 18:36:16 +04:00
sleep(1);
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom template (new)');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
public function testForceCompile()
{
2013-06-28 11:53:53 +04:00
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom template');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom template (new)');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
public function testSetModifier()
{
2013-06-28 11:53:53 +04:00
$this->fenom->addModifier("mymod", "myMod");
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom modifier {$a|mymod}');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->fenom->fetch('custom.tpl', array("a" => "Custom")));
2013-01-25 18:36:16 +04:00
}
/**
* @group add_functions
*/
2013-07-29 14:58:14 +04:00
public function testSetFunctions()
{
2013-06-28 11:53:53 +04:00
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->fenom->addFunction("myfunc", "myFunc");
$this->fenom->addBlockFunction("myblockfunc", "myBlockFunc");
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom function {myfunc name="foo"}');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom function MyFunc:foo", $this->fenom->fetch('custom.tpl', array()));
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}');
2013-06-28 11:53:53 +04:00
$this->assertSame("Custom function Block:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array()));
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
public function testSetCompilers()
{
2013-06-28 11:53:53 +04:00
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->fenom->addCompiler("mycompiler", 'myCompiler');
$this->fenom->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
2013-01-25 18:36:16 +04:00
'tag' => 'myBlockCompilerTag'
));
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom compiler {mycompiler name="bar"}');
2013-07-29 14:58:14 +04:00
$this->assertSame("Custom compiler PHP_VERSION: " . PHP_VERSION . " (for bar)", $this->fenom->fetch('custom.tpl', array()));
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom compiler {myblockcompiler name="bar"} block1 {tag name="baz"} block2 {/myblockcompiler}');
2013-07-29 14:58:14 +04:00
$this->assertSame("Custom compiler PHP_VERSION: " . PHP_VERSION . " (for bar) block1 Tag baz of compiler block2 End of compiler", $this->fenom->fetch('custom.tpl', array()));
2013-01-25 18:36:16 +04:00
}
2013-07-23 11:32:31 +04:00
/**
* @dataProvider providerOptions
*/
2013-07-29 14:58:14 +04:00
public function testOptions($code, $option)
{
2013-07-23 11:32:31 +04:00
static $options = array();
static $flags = 0;
$options[$code] = true;
$flags |= $option;
$this->fenom->setOptions($options);
$this->assertSame($this->fenom->getOptions(), $flags);
// printf("from %010b, flags %010b\n", $this->fenom->getOptions(), $flags);
// $this->fenom->setOptions(array($code => false));
// printf("remove %010b from option %010b, flags %010b\n", $option, $this->fenom->getOptions(), $flags & ~$option);
// $this->assertSame($this->fenom->getOptions(), $flags & ~$option);
}
2013-08-01 01:05:19 +04:00
2013-08-02 21:50:04 +04:00
public function testFilter()
{
2013-08-01 01:05:19 +04:00
$punit = $this;
$this->fenom->addPreFilter(function ($src, $tpl) use ($punit) {
2013-08-05 19:29:11 +04:00
$punit->assertInstanceOf('Fenom\Template', $tpl);
2013-08-01 01:05:19 +04:00
return "== $src ==";
});
$this->fenom->addPostFilter(function ($code, $tpl) use ($punit) {
2013-08-05 19:29:11 +04:00
$punit->assertInstanceOf('Fenom\Template', $tpl);
2013-08-01 01:05:19 +04:00
return "+++ $code +++";
});
$this->fenom->addFilter(function ($text, $tpl) use ($punit) {
2013-08-05 19:29:11 +04:00
$punit->assertInstanceOf('Fenom\Template', $tpl);
2013-08-01 01:05:19 +04:00
return "|--- $text ---|";
});
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} god {/var} world')->fetch(array()));
}
2013-07-23 11:32:31 +04:00
}