fenom/tests/cases/FenomTest.php

332 lines
11 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
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),
2013-09-14 11:24:23 +04:00
array("disable_native_funcs", Fenom::DENY_NATIVE_FUNCS),
2013-07-29 14:58:14 +04:00
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),
2014-05-08 12:56:37 +04:00
array("force_verify", Fenom::FORCE_VERIFY),
array("strip", Fenom::AUTO_STRIP),
2013-07-23 11:32:31 +04:00
);
}
2014-05-06 14:22:58 +04:00
2014-02-27 16:30:44 +04:00
public function testCreating()
{
2014-05-06 14:22:58 +04:00
$time = $this->tpl('temp.tpl', 'Template 1 a');
2013-09-14 11:24:23 +04:00
$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();
}
2014-02-27 16:30:44 +04:00
public function testFactory()
{
2014-05-06 14:22:58 +04:00
$time = $this->tpl('temp.tpl', 'Template 1 a');
$fenom = Fenom::factory(
$provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'),
FENOM_RESOURCES . '/compile',
Fenom::AUTO_ESCAPE
);
2013-09-15 16:05:18 +04:00
$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();
}
2014-05-06 14:22:58 +04:00
/**
* @expectedException LogicException
* @expectedExceptionMessage Cache directory /invalid/path is not writable
*/
public function testFactoryInvalid()
{
Fenom::factory(FENOM_RESOURCES . '/template', '/invalid/path');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Source must be a valid path or provider object
*/
public function testFactoryInvalid2()
{
Fenom::factory(new StdClass);
}
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-09-14 11:24:23 +04:00
/**
* @group testCheckMTime
*/
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-09-14 11:24:23 +04:00
$tpl = $this->fenom->getTemplate('custom.tpl');
$this->assertTrue($tpl->isValid());
usleep(1.5e6);
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom template (new)');
2013-09-14 11:24:23 +04:00
$this->assertFalse($tpl->isValid());
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}');
2014-05-06 14:22:58 +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');
2014-05-06 14:22:58 +04:00
$this->fenom->addBlockCompiler(
"myblockcompiler",
'myBlockCompilerOpen',
'myBlockCompilerClose',
array(
'tag' => 'myBlockCompilerTag'
)
);
2013-02-22 00:05:20 +04:00
$this->tpl('custom.tpl', 'Custom compiler {mycompiler name="bar"}');
2014-05-06 14:22:58 +04:00
$this->assertSame(
"Custom compiler PHP_VERSION: " . PHP_VERSION . " (for bar)",
$this->fenom->fetch('custom.tpl', array())
);
$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->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);
2013-08-07 10:27:20 +04:00
$this->fenom->setOptions(array($code => false));
$this->assertSame($this->fenom->getOptions(), $flags & ~$option);
2013-07-23 11:32:31 +04:00
}
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;
2014-05-06 14:22:58 +04:00
$this->fenom->addPreFilter(
function ($tpl, $src) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "== $src ==";
}
);
2013-08-01 01:05:19 +04:00
2014-05-06 14:22:58 +04:00
$this->fenom->addPostFilter(
function ($tpl, $code) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "+++ $code +++";
}
);
2013-08-01 01:05:19 +04:00
2014-05-06 14:22:58 +04:00
$this->fenom->addFilter(
function ($tpl, $text) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "|--- $text ---|";
}
);
2013-08-01 01:05:19 +04:00
2014-05-06 14:22:58 +04:00
$this->assertSame(
'+++ |--- == hello ---||--- world == ---| +++',
$this->fenom->compileCode('hello {var $user} misterio {/var} world')->fetch(array())
);
$this->assertSame(
'+++ |--- == hello ---||--- world == ---| +++',
$this->fenom->compileCode('hello {var $user} <?php misterio ?> {/var} world')->fetch(array())
);
2013-08-01 01:05:19 +04:00
}
2013-09-14 11:24:23 +04:00
/**
* @group tag-filter
*/
2014-02-27 16:30:44 +04:00
public function testTagFilter()
{
2014-05-06 14:22:58 +04:00
$tags = array();
$punit = $this;
2014-05-06 14:22:58 +04:00
$this->fenom->addTagFilter(
function ($text, $tpl) use (&$tags, $punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
$tags[] = $text;
return $text;
}
);
$this->fenom->compileCode('hello {var $a} misterio {/var} world, {$b}!');
$this->assertSame(array('var $a', '/var', '$b'), $tags);
}
2014-02-27 16:30:44 +04:00
public function testAddInlineCompilerSmart()
{
$this->fenom->addCompilerSmart('SayA', 'TestTags');
2013-09-14 11:24:23 +04:00
$this->tpl('inline_compiler.tpl', 'I just {SayA}.');
$this->assertSame('I just Say A.', $this->fenom->fetch('inline_compiler.tpl', array()));
}
2014-02-27 16:30:44 +04:00
public function testAddBlockCompilerSmart()
{
2013-09-14 11:24:23 +04:00
$this->fenom->addBlockCompilerSmart('SayBlock', 'TestTags', array('SaySomething'), array('SaySomething'));
$this->tpl('block_compiler.tpl', '{SayBlock} and {SaySomething}. It is all, {/SayBlock}');
2014-05-06 14:22:58 +04:00
$this->assertSame(
'Start saying and say blah-blah-blah. It is all, Stop saying',
$this->fenom->fetch('block_compiler.tpl', array())
);
2013-09-14 11:24:23 +04:00
}
2014-02-27 16:30:44 +04:00
public function testAddFunctions()
{
2013-09-14 11:24:23 +04:00
$this->fenom->setOptions(Fenom::DENY_NATIVE_FUNCS);
$this->assertFalse($this->fenom->isAllowedFunction('substr'));
$this->fenom->addAllowedFunctions(array('substr'));
$this->assertTrue($this->fenom->isAllowedFunction('substr'));
}
2014-05-06 14:22:58 +04:00
/**
2014-05-06 23:26:28 +04:00
* @requires function php_gte_54
2014-05-06 14:22:58 +04:00
* @group pipe
*/
public function testPipe()
{
$iteration = 0;
$test = $this; // for PHP 5.3
$this->fenom->pipe(
"persist:pipe.tpl",
function ($chunk) use (&$iteration, $test) {
if (!$chunk) {
return;
}
$iteration++;
// error_log(var_export($chunk, 1));
$test->assertSame("key$iteration:value$iteration", $chunk);
},
array(
"items" => array(
"key1" => "value1",
"key2" => "value2",
"key3" => "value3",
)
),
11 // strlen(key1) + strlen(:) + strlen(value1)
);
$this->assertSame(3, $iteration);
}
2014-05-08 12:56:37 +04:00
/**
* @group strip
*/
public function testStrip() {
$this->fenom->setOptions(Fenom::AUTO_STRIP);
$tpl = <<<TPL
<div class="item item-one">
<a href="/item/{\$one}">number {\$num.1}</a>
</div>
TPL;
$this->assertRender($tpl, '<div class="item item-one"><a href="/item/1">number one</a></div>');
}
2013-09-14 11:24:23 +04:00
}
2014-02-27 16:30:44 +04:00
class TestTags
{
2013-09-14 11:24:23 +04:00
2014-02-27 16:30:44 +04:00
public static function tagSayA()
{
2013-09-14 11:24:23 +04:00
return 'echo "Say A"';
}
2014-02-27 16:30:44 +04:00
public static function SayBlockOpen()
{
2013-09-14 11:24:23 +04:00
return 'echo "Start saying"';
}
2014-02-27 16:30:44 +04:00
public static function tagSaySomething()
{
2013-09-14 11:24:23 +04:00
return 'echo "say blah-blah-blah"';
}
2014-02-27 16:30:44 +04:00
public static function SayBlockClose()
{
2013-09-14 11:24:23 +04:00
return 'echo "Stop saying"';
}
}