mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
To PSR-x format
This commit is contained in:
@ -2,7 +2,8 @@
|
||||
namespace Fenom;
|
||||
use Fenom, Fenom\Provider as FS;
|
||||
|
||||
class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
class TestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Fenom
|
||||
*/
|
||||
@ -10,26 +11,27 @@ class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
public $values = array(
|
||||
"zero" => 0,
|
||||
"one" => 1,
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"three" => 3,
|
||||
"float" => 4.5,
|
||||
"bool" => true,
|
||||
"bool" => true,
|
||||
0 => "empty value",
|
||||
1 => "one value",
|
||||
2 => "two value",
|
||||
3 => "three value",
|
||||
);
|
||||
|
||||
public static function getVars() {
|
||||
public static function getVars()
|
||||
{
|
||||
return array(
|
||||
"zero" => 0,
|
||||
"one" => 1,
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"three" => 3,
|
||||
"float" => 4.5,
|
||||
"bool" => true,
|
||||
"obj" => new \StdClass,
|
||||
"bool" => true,
|
||||
"obj" => new \StdClass,
|
||||
"list" => array(
|
||||
"a" => 1,
|
||||
"b" => 2
|
||||
@ -41,50 +43,57 @@ class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
);
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
if(!file_exists(FENOM_RESOURCES.'/compile')) {
|
||||
mkdir(FENOM_RESOURCES.'/compile', 0777, true);
|
||||
public function setUp()
|
||||
{
|
||||
if (!file_exists(FENOM_RESOURCES . '/compile')) {
|
||||
mkdir(FENOM_RESOURCES . '/compile', 0777, true);
|
||||
} else {
|
||||
FS::clean(FENOM_RESOURCES.'/compile/');
|
||||
FS::clean(FENOM_RESOURCES . '/compile/');
|
||||
}
|
||||
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES.'/template', FENOM_RESOURCES.'/compile');
|
||||
$this->fenom->addModifier('dots', __CLASS__.'::dots');
|
||||
$this->fenom->addModifier('concat', __CLASS__.'::concat');
|
||||
$this->fenom->addFunction('test_function', __CLASS__.'::inlineFunction');
|
||||
$this->fenom->addBlockFunction('test_block_function', __CLASS__.'::blockFunction');
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/template', FENOM_RESOURCES . '/compile');
|
||||
$this->fenom->addModifier('dots', __CLASS__ . '::dots');
|
||||
$this->fenom->addModifier('concat', __CLASS__ . '::concat');
|
||||
$this->fenom->addFunction('test_function', __CLASS__ . '::inlineFunction');
|
||||
$this->fenom->addBlockFunction('test_block_function', __CLASS__ . '::blockFunction');
|
||||
}
|
||||
|
||||
public static function dots($value) {
|
||||
return $value."...";
|
||||
public static function dots($value)
|
||||
{
|
||||
return $value . "...";
|
||||
}
|
||||
|
||||
public static function concat() {
|
||||
return call_user_func_array('var_export', func_get_args());
|
||||
}
|
||||
public static function concat()
|
||||
{
|
||||
return call_user_func_array('var_export', func_get_args());
|
||||
}
|
||||
|
||||
public static function inlineFunction($params) {
|
||||
public static function inlineFunction($params)
|
||||
{
|
||||
return isset($params["text"]) ? $params["text"] : "";
|
||||
}
|
||||
|
||||
public static function blockFunction($params, $text) {
|
||||
public static function blockFunction($params, $text)
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
|
||||
public static function setUpBeforeClass() {
|
||||
if(!file_exists(FENOM_RESOURCES.'/template')) {
|
||||
mkdir(FENOM_RESOURCES.'/template', 0777, true);
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
if (!file_exists(FENOM_RESOURCES . '/template')) {
|
||||
mkdir(FENOM_RESOURCES . '/template', 0777, true);
|
||||
} else {
|
||||
FS::clean(FENOM_RESOURCES.'/template/');
|
||||
FS::clean(FENOM_RESOURCES . '/template/');
|
||||
}
|
||||
}
|
||||
|
||||
public function tpl($name, $code) {
|
||||
public function tpl($name, $code)
|
||||
{
|
||||
$dir = dirname($name);
|
||||
if($dir != "." && !is_dir(FENOM_RESOURCES.'/template/'.$dir)) {
|
||||
mkdir(FENOM_RESOURCES.'/template/'.$dir, 0777, true);
|
||||
if ($dir != "." && !is_dir(FENOM_RESOURCES . '/template/' . $dir)) {
|
||||
mkdir(FENOM_RESOURCES . '/template/' . $dir, 0777, true);
|
||||
}
|
||||
file_put_contents(FENOM_RESOURCES.'/template/'.$name, $code);
|
||||
file_put_contents(FENOM_RESOURCES . '/template/' . $name, $code);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,20 +105,22 @@ class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
* @param int $options
|
||||
* @param bool $dump dump source and result code (for debug)
|
||||
*/
|
||||
public function exec($code, $vars, $result, $options = 0, $dump = false) {
|
||||
public function exec($code, $vars, $result, $options = 0, $dump = false)
|
||||
{
|
||||
$this->fenom->setOptions($options);
|
||||
$tpl = $this->fenom->compileCode($code, "runtime.tpl");
|
||||
if($dump) {
|
||||
echo "\n========= DUMP BEGIN ===========\n".$code."\n--- to ---\n".$tpl->getBody()."\n========= DUMP END =============\n";
|
||||
if ($dump) {
|
||||
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");
|
||||
}
|
||||
|
||||
public function execTpl($name, $code, $vars, $result, $dump = false) {
|
||||
public function execTpl($name, $code, $vars, $result, $dump = false)
|
||||
{
|
||||
$this->tpl($name, $code);
|
||||
$tpl = $this->fenom->getTemplate($name);
|
||||
if($dump) {
|
||||
echo "\n========= DUMP BEGIN ===========\n".$code."\n--- to ---\n".$tpl->getBody()."\n========= DUMP END =============\n";
|
||||
if ($dump) {
|
||||
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody() . "\n========= DUMP END =============\n";
|
||||
}
|
||||
$this->assertSame(Modifier::strip($result, true), Modifier::strip($tpl->fetch($vars), true), "Test tpl $name");
|
||||
}
|
||||
@ -121,12 +132,13 @@ class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
* @param string $message exception message
|
||||
* @param int $options Fenom's options
|
||||
*/
|
||||
public function execError($code, $exception, $message, $options = 0) {
|
||||
public function execError($code, $exception, $message, $options = 0)
|
||||
{
|
||||
$opt = $this->fenom->getOptions();
|
||||
$this->fenom->setOptions($options);
|
||||
try {
|
||||
$this->fenom->compileCode($code, "inline.tpl");
|
||||
} catch(\Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
$this->assertSame($exception, get_class($e), "Exception $code");
|
||||
$this->assertStringStartsWith($message, $e->getMessage());
|
||||
$this->fenom->setOptions($opt);
|
||||
@ -136,28 +148,31 @@ class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
$this->fail("Code $code must be invalid");
|
||||
}
|
||||
|
||||
public function assertRender($tpl, $result, $debug = false) {
|
||||
public function assertRender($tpl, $result, $debug = false)
|
||||
{
|
||||
$template = $this->fenom->compileCode($tpl);
|
||||
if($debug) {
|
||||
print_r("$tpl:\n".$template->getBody());
|
||||
if ($debug) {
|
||||
print_r("$tpl:\n" . $template->getBody());
|
||||
}
|
||||
$this->assertSame($result, $template->fetch($this->values));
|
||||
}
|
||||
|
||||
|
||||
public static function providerNumbers() {
|
||||
return array(
|
||||
array('0', 0),
|
||||
array('77', 77),
|
||||
array('-33', -33),
|
||||
array('0.2', 0.2),
|
||||
array('-0.3', -0.3),
|
||||
array('1e6', 1e6),
|
||||
array('-2e6', -2e6),
|
||||
);
|
||||
}
|
||||
public static function providerNumbers()
|
||||
{
|
||||
return array(
|
||||
array('0', 0),
|
||||
array('77', 77),
|
||||
array('-33', -33),
|
||||
array('0.2', 0.2),
|
||||
array('-0.3', -0.3),
|
||||
array('1e6', 1e6),
|
||||
array('-2e6', -2e6),
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerStrings() {
|
||||
public static function providerStrings()
|
||||
{
|
||||
return array(
|
||||
array('"str"', 'str'),
|
||||
array('"str\nand\nmany\nlines"', "str\nand\nmany\nlines"),
|
||||
@ -187,81 +202,92 @@ class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
);
|
||||
}
|
||||
|
||||
public function providerVariables() {
|
||||
return array();
|
||||
}
|
||||
public function providerVariables()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public static function providerObjects() {
|
||||
return array();
|
||||
}
|
||||
public static function providerObjects()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public static function providerArrays() {
|
||||
$scalars = array();
|
||||
$data = array(
|
||||
array('[]', array()),
|
||||
array('[[],[]]', array(array(), array())),
|
||||
);
|
||||
foreach(self::providerScalars() as $scalar) {
|
||||
$scalars[0][] = $scalar[0];
|
||||
$scalars[1][] = $scalar[1];
|
||||
public static function providerArrays()
|
||||
{
|
||||
$scalars = array();
|
||||
$data = array(
|
||||
array('[]', array()),
|
||||
array('[[],[]]', array(array(), array())),
|
||||
);
|
||||
foreach (self::providerScalars() as $scalar) {
|
||||
$scalars[0][] = $scalar[0];
|
||||
$scalars[1][] = $scalar[1];
|
||||
|
||||
$data[] = array(
|
||||
"[".$scalar[0]."]",
|
||||
array($scalar[1])
|
||||
);
|
||||
$data[] = array(
|
||||
"['some_key' =>".$scalar[0]."]",
|
||||
array('some_key' => $scalar[1])
|
||||
);
|
||||
}
|
||||
$data[] = array(
|
||||
"[".implode(", ", $scalars[0])."]",
|
||||
$scalars[1]
|
||||
);
|
||||
return $data;
|
||||
}
|
||||
$data[] = array(
|
||||
"[" . $scalar[0] . "]",
|
||||
array($scalar[1])
|
||||
);
|
||||
$data[] = array(
|
||||
"['some_key' =>" . $scalar[0] . "]",
|
||||
array('some_key' => $scalar[1])
|
||||
);
|
||||
}
|
||||
$data[] = array(
|
||||
"[" . implode(", ", $scalars[0]) . "]",
|
||||
$scalars[1]
|
||||
);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function providerScalars() {
|
||||
return array_merge(
|
||||
self::providerNumbers(),
|
||||
self::providerStrings()
|
||||
);
|
||||
}
|
||||
public static function providerScalars()
|
||||
{
|
||||
return array_merge(
|
||||
self::providerNumbers(),
|
||||
self::providerStrings()
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerValues() {
|
||||
return array_merge(
|
||||
self::providerScalars(),
|
||||
self::providerArrays(),
|
||||
self::providerVariables(),
|
||||
self::providerObjects()
|
||||
);
|
||||
}
|
||||
public static function providerValues()
|
||||
{
|
||||
return array_merge(
|
||||
self::providerScalars(),
|
||||
self::providerArrays(),
|
||||
self::providerVariables(),
|
||||
self::providerObjects()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Fake implements \ArrayAccess {
|
||||
class Fake implements \ArrayAccess
|
||||
{
|
||||
public $vars;
|
||||
|
||||
public function offsetExists($offset) {
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function offsetGet($offset) {
|
||||
if($offset == "object") {
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
if ($offset == "object") {
|
||||
return new self();
|
||||
} else {
|
||||
return new self($offset);
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value) {
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->vars[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->vars[$offset]);
|
||||
}
|
||||
|
||||
public function proxy() {
|
||||
return implode(", ", func_get_args());
|
||||
}
|
||||
public function proxy()
|
||||
{
|
||||
return implode(", ", func_get_args());
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,27 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/../vendor/autoload.php";
|
||||
require_once __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
|
||||
define('FENOM_RESOURCES', __DIR__ . "/resources");
|
||||
|
||||
define('FENOM_RESOURCES', __DIR__."/resources");
|
||||
|
||||
require_once FENOM_RESOURCES."/actions.php";
|
||||
require_once __DIR__."/TestCase.php";
|
||||
require_once FENOM_RESOURCES . "/actions.php";
|
||||
require_once __DIR__ . "/TestCase.php";
|
||||
|
||||
ini_set('date.timezone', 'Europe/Moscow');
|
||||
|
||||
function drop() {
|
||||
function drop()
|
||||
{
|
||||
call_user_func_array("var_dump", func_get_args());
|
||||
$e = new Exception();
|
||||
echo "-------\nDump trace: \n".$e->getTraceAsString()."\n";
|
||||
echo "-------\nDump trace: \n" . $e->getTraceAsString() . "\n";
|
||||
exit();
|
||||
}
|
||||
|
||||
function dump() {
|
||||
foreach(func_get_args() as $arg) {
|
||||
fwrite(STDERR, "DUMP: ".call_user_func("print_r", $arg, true)."\n");
|
||||
function dump()
|
||||
{
|
||||
foreach (func_get_args() as $arg) {
|
||||
fwrite(STDERR, "DUMP: " . call_user_func("print_r", $arg, true) . "\n");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -3,9 +3,11 @@
|
||||
namespace Fenom;
|
||||
|
||||
|
||||
class AutoEscapeTest extends TestCase {
|
||||
class AutoEscapeTest extends TestCase
|
||||
{
|
||||
|
||||
public static function providerHTML() {
|
||||
public static function providerHTML()
|
||||
{
|
||||
$html = "<script>alert('injection');</script>";
|
||||
$escaped = htmlspecialchars($html, ENT_COMPAT, 'UTF-8');
|
||||
$vars = array(
|
||||
@ -17,7 +19,7 @@ class AutoEscapeTest extends TestCase {
|
||||
array('{$html}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{raw $html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{raw $html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{raw "{$html|up}"}, {$html}', strtoupper($html).", $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{raw "{$html|up}"}, {$html}', strtoupper($html) . ", $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{$html}{/autoescape}, {$html}', "$escaped, $html", $vars, 0),
|
||||
array('{autoescape false}{$html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{$html}{/autoescape}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
@ -31,7 +33,7 @@ class AutoEscapeTest extends TestCase {
|
||||
array('{test_function text=$html}, {$html}', "$html, $html", $vars, 0),
|
||||
array('{test_function text=$html}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{raw:test_function text=$html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{raw:test_function text="{$html|up}"}, {$html}', strtoupper($html).", $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{raw:test_function text="{$html|up}"}, {$html}', strtoupper($html) . ", $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$escaped, $html", $vars, 0),
|
||||
array('{autoescape false}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
@ -56,7 +58,8 @@ class AutoEscapeTest extends TestCase {
|
||||
/**
|
||||
* @dataProvider providerHTML
|
||||
*/
|
||||
public function testEscaping($tpl, $result, $vars, $options) {
|
||||
public function testEscaping($tpl, $result, $vars, $options)
|
||||
{
|
||||
$this->values = $vars;
|
||||
$this->fenom->setOptions($options);
|
||||
$this->assertRender($tpl, $result);
|
||||
|
@ -3,29 +3,33 @@
|
||||
namespace Fenom;
|
||||
|
||||
|
||||
class CommentTest extends TestCase {
|
||||
class CommentTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testInline($tpl_val) {
|
||||
public function testInline($tpl_val)
|
||||
{
|
||||
$this->assertRender("before {* $tpl_val *} after", "before after");
|
||||
$this->assertRender("before {* {{$tpl_val}} {{$tpl_val}} *} after", "before after");
|
||||
$this->assertRender("before {*{{$tpl_val}}*} after", "before after");
|
||||
}
|
||||
|
||||
public function testError() {
|
||||
public function testError()
|
||||
{
|
||||
$this->execError('{* ', 'Fenom\CompileException', "Unclosed comment block in line");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testMultiLine($tpl_val) {
|
||||
$this->assertRender(
|
||||
"before-1\nbefore-2 {* before-3\nbefore-4 $tpl_val after-1\nafter-2 *} after-3\nafter-4{* dummy *}\nafter-5",
|
||||
"before-1\nbefore-2 after-3\nafter-4\nafter-5"
|
||||
);
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testMultiLine($tpl_val)
|
||||
{
|
||||
$this->assertRender(
|
||||
"before-1\nbefore-2 {* before-3\nbefore-4 $tpl_val after-1\nafter-2 *} after-3\nafter-4{* dummy *}\nafter-5",
|
||||
"before-1\nbefore-2 after-3\nafter-4\nafter-5"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -3,14 +3,17 @@
|
||||
namespace Fenom;
|
||||
|
||||
|
||||
class CustomProviderTest extends TestCase {
|
||||
class CustomProviderTest extends TestCase
|
||||
{
|
||||
|
||||
public function setUp() {
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->fenom->addProvider("my", new Provider(FENOM_RESOURCES.'/provider'));
|
||||
$this->fenom->addProvider("my", new Provider(FENOM_RESOURCES . '/provider'));
|
||||
}
|
||||
|
||||
public function testCustom() {
|
||||
public function testCustom()
|
||||
{
|
||||
$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}");
|
||||
}
|
||||
|
@ -2,41 +2,44 @@
|
||||
namespace Fenom;
|
||||
use Fenom, Fenom\TestCase;
|
||||
|
||||
class ExtendsTemplateTest extends TestCase {
|
||||
class ExtendsTemplateTest extends TestCase
|
||||
{
|
||||
|
||||
public function _testSandbox() {
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES.'/provider', FENOM_RESOURCES.'/compile');
|
||||
try {
|
||||
print_r($this->fenom->getTemplate('use/child.tpl')->getBody());
|
||||
} catch (\Exception $e) {
|
||||
echo "$e";
|
||||
}
|
||||
exit;
|
||||
}
|
||||
public function _testSandbox()
|
||||
{
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/provider', FENOM_RESOURCES . '/compile');
|
||||
try {
|
||||
print_r($this->fenom->getTemplate('use/child.tpl')->getBody());
|
||||
} catch (\Exception $e) {
|
||||
echo "$e";
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Templates skeletons
|
||||
* @param array $vars
|
||||
* @return array
|
||||
*/
|
||||
public static function templates(array $vars) {
|
||||
/**
|
||||
* Templates skeletons
|
||||
* @param array $vars
|
||||
* @return array
|
||||
*/
|
||||
public static function templates(array $vars)
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
"name" => "level.0.tpl",
|
||||
"name" => "level.0.tpl",
|
||||
"level" => 0,
|
||||
"blocks" => array(
|
||||
"b1" => "default {\$default}",
|
||||
"b2" => "empty 0"
|
||||
),
|
||||
"result" => array(
|
||||
"b1" => "default ".$vars["default"],
|
||||
"b1" => "default " . $vars["default"],
|
||||
"b2" => "empty 0"
|
||||
),
|
||||
),
|
||||
array(
|
||||
"name" => "level.1.tpl",
|
||||
"name" => "level.1.tpl",
|
||||
"level" => 1,
|
||||
"use" => false,
|
||||
"use" => false,
|
||||
"blocks" => array(
|
||||
"b1" => "from level 1"
|
||||
),
|
||||
@ -46,9 +49,9 @@ class ExtendsTemplateTest extends TestCase {
|
||||
),
|
||||
),
|
||||
array(
|
||||
"name" => "level.2.tpl",
|
||||
"name" => "level.2.tpl",
|
||||
"level" => 2,
|
||||
"use" => false,
|
||||
"use" => false,
|
||||
"blocks" => array(
|
||||
"b2" => "from level 2",
|
||||
"b4" => "unused block"
|
||||
@ -59,9 +62,9 @@ class ExtendsTemplateTest extends TestCase {
|
||||
),
|
||||
),
|
||||
array(
|
||||
"name" => "level.3.tpl",
|
||||
"name" => "level.3.tpl",
|
||||
"level" => 3,
|
||||
"use" => false,
|
||||
"use" => false,
|
||||
"blocks" => array(
|
||||
"b1" => "from level 3",
|
||||
"b2" => "also from level 3"
|
||||
@ -74,35 +77,37 @@ class ExtendsTemplateTest extends TestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate templates by skeletons
|
||||
*
|
||||
* @param $block_mask
|
||||
* @param $extend_mask
|
||||
* @param array $skels
|
||||
* @return array
|
||||
*/
|
||||
public static function generate($block_mask, $extend_mask, $skels) {
|
||||
/**
|
||||
* Generate templates by skeletons
|
||||
*
|
||||
* @param $block_mask
|
||||
* @param $extend_mask
|
||||
* @param array $skels
|
||||
* @return array
|
||||
*/
|
||||
public static function generate($block_mask, $extend_mask, $skels)
|
||||
{
|
||||
$t = array();
|
||||
foreach($skels as $level => $tpl) {
|
||||
$src = 'level#'.$level.' ';
|
||||
foreach ($skels as $level => $tpl) {
|
||||
$src = 'level#' . $level . ' ';
|
||||
|
||||
foreach($tpl["blocks"] as $bname => &$bcode) {
|
||||
$src .= sprintf($block_mask, $bname, $bname.': '.$bcode)." level#$level ";
|
||||
foreach ($tpl["blocks"] as $bname => &$bcode) {
|
||||
$src .= sprintf($block_mask, $bname, $bname . ': ' . $bcode) . " level#$level ";
|
||||
}
|
||||
$dst = "level#0 ";
|
||||
foreach($tpl["result"] as $bname => &$bcode) {
|
||||
$dst .= $bname.': '.$bcode.' level#0 ';
|
||||
foreach ($tpl["result"] as $bname => &$bcode) {
|
||||
$dst .= $bname . ': ' . $bcode . ' level#0 ';
|
||||
}
|
||||
if($level) {
|
||||
$src = sprintf($extend_mask, $level-1).' '.$src;
|
||||
if ($level) {
|
||||
$src = sprintf($extend_mask, $level - 1) . ' ' . $src;
|
||||
}
|
||||
$t[ $tpl["name"] ] = array("src" => $src, "dst" => $dst);
|
||||
$t[$tpl["name"]] = array("src" => $src, "dst" => $dst);
|
||||
}
|
||||
return $t;
|
||||
}
|
||||
|
||||
public function _testTemplateExtends() {
|
||||
public function _testTemplateExtends()
|
||||
{
|
||||
$vars = array(
|
||||
"b1" => "b1",
|
||||
"b2" => "b2",
|
||||
@ -112,39 +117,41 @@ class ExtendsTemplateTest extends TestCase {
|
||||
"default" => 5
|
||||
);
|
||||
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
|
||||
foreach($tpls as $name => $tpl) {
|
||||
foreach ($tpls as $name => $tpl) {
|
||||
$this->tpl($name, $tpl["src"]);
|
||||
$this->assertSame($this->fenom->fetch($name, $vars), $tpl["dst"]);
|
||||
}
|
||||
return;
|
||||
return;
|
||||
$vars["default"]++;
|
||||
$this->fenom->flush();
|
||||
$tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
|
||||
arsort($tpls);
|
||||
foreach($tpls as $name => $tpl) {
|
||||
$this->tpl("d.".$name, $tpl["src"]);
|
||||
$this->assertSame($this->fenom->fetch("d.".$name, $vars), $tpl["dst"]);
|
||||
foreach ($tpls as $name => $tpl) {
|
||||
$this->tpl("d." . $name, $tpl["src"]);
|
||||
$this->assertSame($this->fenom->fetch("d." . $name, $vars), $tpl["dst"]);
|
||||
}
|
||||
$vars["default"]++;
|
||||
$this->fenom->flush();
|
||||
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "$level.%d.tpl"}', self::templates($vars));
|
||||
arsort($tpls);
|
||||
foreach($tpls as $name => $tpl) {
|
||||
$this->tpl("x.".$name, $tpl["src"]);
|
||||
$this->assertSame($this->fenom->fetch("x.".$name, $vars), $tpl["dst"]);
|
||||
foreach ($tpls as $name => $tpl) {
|
||||
$this->tpl("x." . $name, $tpl["src"]);
|
||||
$this->assertSame($this->fenom->fetch("x." . $name, $vars), $tpl["dst"]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @group use
|
||||
*/
|
||||
public function testUse() {
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES.'/provider', FENOM_RESOURCES.'/compile');
|
||||
$this->assertSame("<html>\n block 1 blocks \n block 2 child \n</html>", $this->fenom->fetch('use/child.tpl'));
|
||||
}
|
||||
/**
|
||||
* @group use
|
||||
*/
|
||||
public function testUse()
|
||||
{
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/provider', FENOM_RESOURCES . '/compile');
|
||||
$this->assertSame("<html>\n block 1 blocks \n block 2 child \n</html>", $this->fenom->fetch('use/child.tpl'));
|
||||
}
|
||||
|
||||
public function _testParent() {
|
||||
public function _testParent()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,70 +1,82 @@
|
||||
<?php
|
||||
namespace Fenom;
|
||||
|
||||
class FunctionsTest extends TestCase {
|
||||
class FunctionsTest extends TestCase
|
||||
{
|
||||
|
||||
const FUNCTION_ARGUMENT_CONSTANT = 1;
|
||||
const FUNCTION_ARGUMENT_CONSTANT = 1;
|
||||
|
||||
public static function functionSum($of = array()) {
|
||||
return array_sum($of);
|
||||
}
|
||||
public static function functionSum($of = array())
|
||||
{
|
||||
return array_sum($of);
|
||||
}
|
||||
|
||||
public static function functionPow($a, $n = 2) {
|
||||
return pow($a, $n);
|
||||
}
|
||||
public static function functionPow($a, $n = 2)
|
||||
{
|
||||
return pow($a, $n);
|
||||
}
|
||||
|
||||
public static function functionInc($a, $i = self::FUNCTION_ARGUMENT_CONSTANT) {
|
||||
return $a + $i;
|
||||
}
|
||||
public static function functionInc($a, $i = self::FUNCTION_ARGUMENT_CONSTANT)
|
||||
{
|
||||
return $a + $i;
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->fenom->addFunctionSmart('sum', __CLASS__ . '::functionSum');
|
||||
$this->fenom->addFunctionSmart('pow', __CLASS__ . '::functionPow');
|
||||
$this->fenom->addFunctionSmart('inc', __CLASS__ . '::functionInc');
|
||||
$this->fenom->addFunctionSmart('sum', __CLASS__ . '::functionSum');
|
||||
$this->fenom->addFunctionSmart('pow', __CLASS__ . '::functionPow');
|
||||
$this->fenom->addFunctionSmart('inc', __CLASS__ . '::functionInc');
|
||||
|
||||
$this->tpl('function_params_scalar.tpl', '{pow a=2 n=3}');
|
||||
$this->tpl('function_params_dynamic.tpl', '{pow a=$a n=$n}');
|
||||
$this->tpl('function_default_param_scalar.tpl', '{pow a=2}');
|
||||
$this->tpl('function_default_param_empty_array.tpl', '{sum}');
|
||||
$this->tpl('function_default_param_const.tpl', '{inc a=1}');
|
||||
$this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}');
|
||||
$this->tpl('function_array_param_pos.tpl', '{sum [1, 2, 3, 4, 5]}');
|
||||
}
|
||||
$this->tpl('function_params_scalar.tpl', '{pow a=2 n=3}');
|
||||
$this->tpl('function_params_dynamic.tpl', '{pow a=$a n=$n}');
|
||||
$this->tpl('function_default_param_scalar.tpl', '{pow a=2}');
|
||||
$this->tpl('function_default_param_empty_array.tpl', '{sum}');
|
||||
$this->tpl('function_default_param_const.tpl', '{inc a=1}');
|
||||
$this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}');
|
||||
$this->tpl('function_array_param_pos.tpl', '{sum [1, 2, 3, 4, 5]}');
|
||||
}
|
||||
|
||||
public function testFunctionWithParams() {
|
||||
public function testFunctionWithParams()
|
||||
{
|
||||
$output = $this->fenom->fetch('function_params_scalar.tpl');
|
||||
$this->assertEquals('8', $output);
|
||||
$this->assertEquals('8', $output);
|
||||
}
|
||||
|
||||
public function testFunctionWithDynamicParams() {
|
||||
public function testFunctionWithDynamicParams()
|
||||
{
|
||||
$output = $this->fenom->fetch('function_params_dynamic.tpl', array('a' => 3, 'n' => 4));
|
||||
$this->assertEquals('81', $output);
|
||||
$this->assertEquals('81', $output);
|
||||
}
|
||||
|
||||
public function testFunctionWithDefaultParamScalar() {
|
||||
$output = $this->fenom->fetch('function_default_param_scalar.tpl');
|
||||
$this->assertEquals('4', $output);
|
||||
}
|
||||
public function testFunctionWithDefaultParamScalar()
|
||||
{
|
||||
$output = $this->fenom->fetch('function_default_param_scalar.tpl');
|
||||
$this->assertEquals('4', $output);
|
||||
}
|
||||
|
||||
public function testFunctionWithDefaultParamArray() {
|
||||
$output = $this->fenom->fetch('function_default_param_empty_array.tpl');
|
||||
$this->assertEquals('0', $output);
|
||||
}
|
||||
public function testFunctionWithDefaultParamArray()
|
||||
{
|
||||
$output = $this->fenom->fetch('function_default_param_empty_array.tpl');
|
||||
$this->assertEquals('0', $output);
|
||||
}
|
||||
|
||||
public function testFunctionWithDefaultParamConst() {
|
||||
$output = $this->fenom->fetch('function_default_param_const.tpl');
|
||||
$this->assertEquals('2', $output);
|
||||
}
|
||||
public function testFunctionWithDefaultParamConst()
|
||||
{
|
||||
$output = $this->fenom->fetch('function_default_param_const.tpl');
|
||||
$this->assertEquals('2', $output);
|
||||
}
|
||||
|
||||
public function testFunctionWithArrayNamedParam() {
|
||||
$output = $this->fenom->fetch('function_array_param.tpl');
|
||||
$this->assertEquals('15', $output);
|
||||
}
|
||||
public function testFunctionWithArrayNamedParam()
|
||||
{
|
||||
$output = $this->fenom->fetch('function_array_param.tpl');
|
||||
$this->assertEquals('15', $output);
|
||||
}
|
||||
|
||||
public function testFunctionWithArrayPositionalParam() {
|
||||
$output = $this->fenom->fetch('function_array_param_pos.tpl');
|
||||
$this->assertEquals('15', $output);
|
||||
}
|
||||
public function testFunctionWithArrayPositionalParam()
|
||||
{
|
||||
$output = $this->fenom->fetch('function_array_param_pos.tpl');
|
||||
$this->assertEquals('15', $output);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
<?php
|
||||
namespace Fenom;
|
||||
|
||||
class MacrosTest extends TestCase {
|
||||
class MacrosTest extends TestCase
|
||||
{
|
||||
|
||||
public function setUp() {
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->tpl("math.tpl", '
|
||||
{macro plus(x, y)}
|
||||
@ -52,31 +54,35 @@ class MacrosTest extends TestCase {
|
||||
{macro.factorial num=10}');
|
||||
}
|
||||
|
||||
public function _testSandbox() {
|
||||
public function _testSandbox()
|
||||
{
|
||||
try {
|
||||
$this->fenom->compile("macro_recursive.tpl");
|
||||
$this->fenom->flush();
|
||||
var_dump($this->fenom->fetch("macro_recursive.tpl", []));
|
||||
} catch(\Exception $e) {
|
||||
var_dump($e->getMessage().": ".$e->getTraceAsString());
|
||||
$this->fenom->compile("macro_recursive.tpl");
|
||||
$this->fenom->flush();
|
||||
var_dump($this->fenom->fetch("macro_recursive.tpl", []));
|
||||
} catch (\Exception $e) {
|
||||
var_dump($e->getMessage() . ": " . $e->getTraceAsString());
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
public function testMacros() {
|
||||
public function testMacros()
|
||||
{
|
||||
$tpl = $this->fenom->compile('math.tpl');
|
||||
|
||||
$this->assertStringStartsWith('x + y = ', trim($tpl->macros["plus"]["body"]));
|
||||
$this->assertSame('Math: x + y = 5 , x - y - z = 6', Modifier::strip($tpl->fetch(array()), true));
|
||||
}
|
||||
|
||||
public function testImport() {
|
||||
public function testImport()
|
||||
{
|
||||
$tpl = $this->fenom->compile('import.tpl');
|
||||
|
||||
$this->assertSame('Imp: x + y = 3 , x - y - z = 3', Modifier::strip($tpl->fetch(array()), true));
|
||||
}
|
||||
|
||||
public function testImportCustom() {
|
||||
public function testImportCustom()
|
||||
{
|
||||
$tpl = $this->fenom->compile('import_custom.tpl');
|
||||
|
||||
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
|
||||
@ -86,13 +92,15 @@ class MacrosTest extends TestCase {
|
||||
* @expectedExceptionMessage Undefined macro 'plus'
|
||||
* @expectedException \Fenom\CompileException
|
||||
*/
|
||||
public function testImportMiss() {
|
||||
public function testImportMiss()
|
||||
{
|
||||
$tpl = $this->fenom->compile('import_miss.tpl');
|
||||
|
||||
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
|
||||
}
|
||||
|
||||
public function testRecursive() {
|
||||
public function testRecursive()
|
||||
{
|
||||
$this->fenom->compile('macro_recursive.tpl');
|
||||
$this->fenom->flush();
|
||||
$tpl = $this->fenom->getTemplate('macro_recursive.tpl');
|
||||
|
@ -2,11 +2,13 @@
|
||||
namespace Fenom;
|
||||
|
||||
|
||||
class ModifiersTest extends TestCase {
|
||||
class ModifiersTest extends TestCase
|
||||
{
|
||||
|
||||
public static function providerTruncate() {
|
||||
public static function providerTruncate()
|
||||
{
|
||||
$lorem = 'Lorem ipsum dolor sit amet'; // en
|
||||
$uni = 'Лорем ипсум долор сит амет'; // ru
|
||||
$uni = 'Лорем ипсум долор сит амет'; // ru
|
||||
return array(
|
||||
// ascii chars
|
||||
array($lorem, 'Lorem ip...', 8),
|
||||
@ -33,7 +35,8 @@ class ModifiersTest extends TestCase {
|
||||
* @param bool $by_words
|
||||
* @param bool $middle
|
||||
*/
|
||||
public function testTruncate($in, $out, $count, $delim = '...', $by_words = false, $middle = false) {
|
||||
public function testTruncate($in, $out, $count, $delim = '...', $by_words = false, $middle = false)
|
||||
{
|
||||
$tpl = $this->fenom->compileCode('{$text|truncate:$count:$delim:$by_words:$middle}');
|
||||
$this->assertEquals($out, $tpl->fetch(array(
|
||||
"text" => $in,
|
||||
@ -44,7 +47,8 @@ class ModifiersTest extends TestCase {
|
||||
)));
|
||||
}
|
||||
|
||||
public static function providerUpLow() {
|
||||
public static function providerUpLow()
|
||||
{
|
||||
return array(
|
||||
array("up", "lorem", "LOREM"),
|
||||
array("up", "Lorem", "LOREM"),
|
||||
@ -64,14 +68,16 @@ class ModifiersTest extends TestCase {
|
||||
* @param $in
|
||||
* @param $out
|
||||
*/
|
||||
public function testUpLow($modifier, $in, $out) {
|
||||
$tpl = $this->fenom->compileCode('{$text|'.$modifier.'}');
|
||||
public function testUpLow($modifier, $in, $out)
|
||||
{
|
||||
$tpl = $this->fenom->compileCode('{$text|' . $modifier . '}');
|
||||
$this->assertEquals($out, $tpl->fetch(array(
|
||||
"text" => $in,
|
||||
)));
|
||||
}
|
||||
|
||||
public static function providerLength() {
|
||||
public static function providerLength()
|
||||
{
|
||||
return array(
|
||||
array("length", 6),
|
||||
array("длина", 5),
|
||||
@ -90,7 +96,8 @@ class ModifiersTest extends TestCase {
|
||||
* @param $in
|
||||
* @param $out
|
||||
*/
|
||||
public function testLength($in, $out) {
|
||||
public function testLength($in, $out)
|
||||
{
|
||||
$tpl = $this->fenom->compileCode('{$data|length}');
|
||||
$this->assertEquals($out, $tpl->fetch(array(
|
||||
"data" => $in,
|
||||
|
@ -3,87 +3,97 @@ namespace Fenom;
|
||||
use Fenom;
|
||||
use Fenom\TestCase;
|
||||
|
||||
class ProviderTest extends TestCase {
|
||||
class ProviderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Provider
|
||||
*/
|
||||
public $provider;
|
||||
|
||||
public function setUp() {
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->tpl("template1.tpl", 'Template 1 {$a}');
|
||||
$this->tpl("template2.tpl", 'Template 2 {$a}');
|
||||
$this->tpl("sub/template3.tpl", 'Template 3 {$a}');
|
||||
$this->provider = new Provider(FENOM_RESOURCES.'/template');
|
||||
$this->provider = new Provider(FENOM_RESOURCES . '/template');
|
||||
clearstatcache();
|
||||
}
|
||||
|
||||
public function testIsTemplateExists() {
|
||||
public function testIsTemplateExists()
|
||||
{
|
||||
clearstatcache();
|
||||
$this->assertTrue($this->provider->templateExists("template1.tpl"));
|
||||
$this->assertFalse($this->provider->templateExists("unexists.tpl"));
|
||||
}
|
||||
|
||||
public function testGetSource() {
|
||||
public function testGetSource()
|
||||
{
|
||||
clearstatcache();
|
||||
$src = $this->provider->getSource("template1.tpl", $time);
|
||||
clearstatcache();
|
||||
$this->assertEquals(file_get_contents(FENOM_RESOURCES.'/template/template1.tpl'), $src);
|
||||
$this->assertEquals(filemtime(FENOM_RESOURCES.'/template/template1.tpl'), $time);
|
||||
$this->assertEquals(file_get_contents(FENOM_RESOURCES . '/template/template1.tpl'), $src);
|
||||
$this->assertEquals(filemtime(FENOM_RESOURCES . '/template/template1.tpl'), $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetSourceInvalid() {
|
||||
public function testGetSourceInvalid()
|
||||
{
|
||||
$this->provider->getSource("unexists.tpl", $time);
|
||||
}
|
||||
|
||||
public function testGetLastModified() {
|
||||
public function testGetLastModified()
|
||||
{
|
||||
$time = $this->provider->getLastModified("template1.tpl");
|
||||
clearstatcache();
|
||||
$this->assertEquals(filemtime(FENOM_RESOURCES.'/template/template1.tpl'), $time);
|
||||
$this->assertEquals(filemtime(FENOM_RESOURCES . '/template/template1.tpl'), $time);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetLastModifiedInvalid() {
|
||||
public function testGetLastModifiedInvalid()
|
||||
{
|
||||
$this->provider->getLastModified("unexists.tpl");
|
||||
}
|
||||
|
||||
public function testVerify() {
|
||||
public function testVerify()
|
||||
{
|
||||
$templates = array(
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
|
||||
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl')
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
|
||||
"template2.tpl" => filemtime(FENOM_RESOURCES . '/template/template2.tpl')
|
||||
);
|
||||
clearstatcache();
|
||||
$this->assertTrue($this->provider->verify($templates));
|
||||
clearstatcache();
|
||||
$templates = array(
|
||||
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl'),
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl')
|
||||
"template2.tpl" => filemtime(FENOM_RESOURCES . '/template/template2.tpl'),
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl')
|
||||
);
|
||||
clearstatcache();
|
||||
$this->assertTrue($this->provider->verify($templates));
|
||||
}
|
||||
|
||||
public function testVerifyInvalid() {
|
||||
public function testVerifyInvalid()
|
||||
{
|
||||
$templates = array(
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
|
||||
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl') + 1
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
|
||||
"template2.tpl" => filemtime(FENOM_RESOURCES . '/template/template2.tpl') + 1
|
||||
);
|
||||
clearstatcache();
|
||||
$this->assertFalse($this->provider->verify($templates));
|
||||
clearstatcache();
|
||||
$templates = array(
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'),
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
|
||||
"unexists.tpl" => 1234567890
|
||||
);
|
||||
$this->assertFalse($this->provider->verify($templates));
|
||||
}
|
||||
|
||||
public function testGetAll() {
|
||||
public function testGetAll()
|
||||
{
|
||||
$list = $this->provider->getList();
|
||||
sort($list);
|
||||
$this->assertSame(array(
|
||||
|
@ -3,22 +3,25 @@ namespace Fenom;
|
||||
use Fenom,
|
||||
Fenom\Render;
|
||||
|
||||
class RenderTest extends \PHPUnit_Framework_TestCase {
|
||||
class RenderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Render
|
||||
*/
|
||||
public static $render;
|
||||
|
||||
public static function setUpBeforeClass() {
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::$render = new Render(Fenom::factory("."), function ($tpl) {
|
||||
echo "It is render's function ".$tpl["render"];
|
||||
echo "It is render's function " . $tpl["render"];
|
||||
}, array(
|
||||
"name" => "render.tpl"
|
||||
));
|
||||
}
|
||||
|
||||
public function testCreate() {
|
||||
public function testCreate()
|
||||
{
|
||||
$r = new Render(Fenom::factory("."), function () {
|
||||
echo "Test render";
|
||||
}, array(
|
||||
@ -27,14 +30,16 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertSame("Test render", $r->fetch(array()));
|
||||
}
|
||||
|
||||
public function testDisplay() {
|
||||
public function testDisplay()
|
||||
{
|
||||
ob_start();
|
||||
self::$render->display(array("render" => "display"));
|
||||
$out = ob_get_clean();
|
||||
$this->assertSame("It is render's function display", $out);
|
||||
}
|
||||
|
||||
public function testFetch() {
|
||||
public function testFetch()
|
||||
{
|
||||
$this->assertSame("It is render's function fetch", self::$render->fetch(array("render" => "fetch")));
|
||||
}
|
||||
|
||||
@ -42,7 +47,8 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
|
||||
* @expectedException \RuntimeException
|
||||
* @expectedExceptionMessage template error
|
||||
*/
|
||||
public function testFetchException() {
|
||||
public function testFetchException()
|
||||
{
|
||||
$render = new Render(Fenom::factory("."), function () {
|
||||
echo "error";
|
||||
throw new \RuntimeException("template error");
|
||||
|
@ -1,22 +1,26 @@
|
||||
<?php
|
||||
namespace Fenom;
|
||||
|
||||
class ScopeTest extends TestCase {
|
||||
public function openTag($tokenizer, $scope) {
|
||||
class ScopeTest extends TestCase
|
||||
{
|
||||
public function openTag($tokenizer, $scope)
|
||||
{
|
||||
$this->assertInstanceOf('Fenom\Tokenizer', $tokenizer);
|
||||
$this->assertInstanceOf('Fenom\Scope', $scope);
|
||||
$scope["value"] = true;
|
||||
return "open-tag";
|
||||
}
|
||||
|
||||
public function closeTag($tokenizer, $scope) {
|
||||
public function closeTag($tokenizer, $scope)
|
||||
{
|
||||
$this->assertInstanceOf('Fenom\Tokenizer', $tokenizer);
|
||||
$this->assertInstanceOf('Fenom\Scope', $scope);
|
||||
$this->assertTrue($scope["value"]);
|
||||
return "close-tag";
|
||||
}
|
||||
|
||||
public function testBlock() {
|
||||
public function testBlock()
|
||||
{
|
||||
/*$scope = new Scope($this->fenom, new Template($this->fenom), 1, array(
|
||||
"open" => array($this, "openTag"),
|
||||
"close" => array($this, "closeTag")
|
||||
@ -28,5 +32,5 @@ class ScopeTest extends TestCase {
|
||||
$content = " some ?> content\n\nwith /*#9999999#* / many\n\tlines";
|
||||
$scope->tpl->_body = "start <?php ".$scope->open($tokenizer)." ?>".$content;
|
||||
$this->assertSame($content, $scope->getContent());*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,54 +3,62 @@
|
||||
namespace Fenom;
|
||||
|
||||
|
||||
class TagsTest extends TestCase {
|
||||
class TagsTest extends TestCase
|
||||
{
|
||||
|
||||
public function _testSandbox() {
|
||||
try {
|
||||
var_dump($this->fenom->compileCode('{var $a=Fenom\TestCase::dots("asd")}')->getBody());
|
||||
} catch(\Exception $e) {
|
||||
echo "$e";
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testVar($tpl_val, $val) {
|
||||
$this->assertRender("{var \$a=$tpl_val}\nVar: {\$a}", "\nVar: ".$val);
|
||||
public function _testSandbox()
|
||||
{
|
||||
try {
|
||||
var_dump($this->fenom->compileCode('{var $a=Fenom\TestCase::dots("asd")}')->getBody());
|
||||
} catch (\Exception $e) {
|
||||
echo "$e";
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testVarBlock($tpl_val, $val) {
|
||||
$this->assertRender("{var \$a}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: before ".$val." after");
|
||||
public function testVar($tpl_val, $val)
|
||||
{
|
||||
$this->assertRender("{var \$a=$tpl_val}\nVar: {\$a}", "\nVar: " . $val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testVarBlockModified($tpl_val, $val) {
|
||||
$this->assertRender("{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: ".strtolower("before ".$val." after")."...");
|
||||
public function testVarBlock($tpl_val, $val)
|
||||
{
|
||||
$this->assertRender("{var \$a}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: before " . $val . " after");
|
||||
}
|
||||
|
||||
public function testCycle() {
|
||||
$this->assertRender('{for $i=0 to=4}{cycle ["one", "two"]}, {/for}', "one, two, one, two, one, ");
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testVarBlockModified($tpl_val, $val)
|
||||
{
|
||||
$this->assertRender("{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: " . strtolower("before " . $val . " after") . "...");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testCycleIndex() {
|
||||
$this->assertRender('{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}', "two, one, two, one, two, ");
|
||||
}
|
||||
public function testCycle()
|
||||
{
|
||||
$this->assertRender('{for $i=0 to=4}{cycle ["one", "two"]}, {/for}', "one, two, one, two, one, ");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testFilter($tpl_val, $val) {
|
||||
$this->assertRender("{filter|up} before {{$tpl_val}} after {/filter}", strtoupper(" before {$val} after "));
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testCycleIndex()
|
||||
{
|
||||
$this->assertRender('{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}', "two, one, two, one, two, ");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerScalars
|
||||
*/
|
||||
public function testFilter($tpl_val, $val)
|
||||
{
|
||||
$this->assertRender("{filter|up} before {{$tpl_val}} after {/filter}", strtoupper(" before {$val} after "));
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -2,58 +2,61 @@
|
||||
namespace Fenom;
|
||||
use Fenom\Tokenizer;
|
||||
|
||||
class TokenizerTest extends \PHPUnit_Framework_TestCase {
|
||||
class TokenizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testTokens() {
|
||||
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
|
||||
$tokens = new Tokenizer($code);
|
||||
$this->assertSame(T_STRING, $tokens->key());
|
||||
$this->assertSame("hello", $tokens->current());
|
||||
public function testTokens()
|
||||
{
|
||||
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
|
||||
$tokens = new Tokenizer($code);
|
||||
$this->assertSame(T_STRING, $tokens->key());
|
||||
$this->assertSame("hello", $tokens->current());
|
||||
|
||||
$this->assertTrue($tokens->isNext(","));
|
||||
$this->assertFalse($tokens->isNext("="));
|
||||
$this->assertFalse($tokens->isNext(T_STRING));
|
||||
$this->assertFalse($tokens->isNext($tokens::MACRO_UNARY));
|
||||
$this->assertTrue($tokens->isNext(","));
|
||||
$this->assertFalse($tokens->isNext("="));
|
||||
$this->assertFalse($tokens->isNext(T_STRING));
|
||||
$this->assertFalse($tokens->isNext($tokens::MACRO_UNARY));
|
||||
|
||||
$this->assertFalse($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY));
|
||||
$this->assertTrue($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY, ","));
|
||||
$this->assertFalse($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY));
|
||||
$this->assertTrue($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY, ","));
|
||||
|
||||
$this->assertSame(",", $tokens->getNext());
|
||||
$this->assertSame(",", $tokens->key());
|
||||
$this->assertSame("please", $tokens->getNext(T_STRING));
|
||||
$this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING));
|
||||
$this->assertSame(",", $tokens->getNext());
|
||||
$this->assertSame(",", $tokens->key());
|
||||
$this->assertSame("please", $tokens->getNext(T_STRING));
|
||||
$this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING));
|
||||
|
||||
$tokens->next();
|
||||
$tokens->next();
|
||||
$tokens->next();
|
||||
$tokens->next();
|
||||
$tokens->next();
|
||||
$tokens->next();
|
||||
|
||||
$this->assertSame(":", $tokens->current());
|
||||
$this->assertSame(":", $tokens->key());
|
||||
$this->assertSame(":", $tokens->current());
|
||||
$this->assertSame(":", $tokens->key());
|
||||
|
||||
|
||||
$this->assertSame("sin", $tokens->getNext($tokens::MACRO_STRING));
|
||||
$this->assertSame("sin", $tokens->current());
|
||||
$this->assertSame(T_STRING, $tokens->key());
|
||||
$this->assertTrue($tokens->is(T_STRING));
|
||||
$this->assertTrue($tokens->is($tokens::MACRO_STRING));
|
||||
$this->assertFalse($tokens->is($tokens::MACRO_EQUALS));
|
||||
$this->assertFalse($tokens->is(T_DNUMBER));
|
||||
$this->assertFalse($tokens->is(":"));
|
||||
$this->assertSame("(", $tokens->getNext("(",")"));
|
||||
$this->assertSame("sin", $tokens->getNext($tokens::MACRO_STRING));
|
||||
$this->assertSame("sin", $tokens->current());
|
||||
$this->assertSame(T_STRING, $tokens->key());
|
||||
$this->assertTrue($tokens->is(T_STRING));
|
||||
$this->assertTrue($tokens->is($tokens::MACRO_STRING));
|
||||
$this->assertFalse($tokens->is($tokens::MACRO_EQUALS));
|
||||
$this->assertFalse($tokens->is(T_DNUMBER));
|
||||
$this->assertFalse($tokens->is(":"));
|
||||
$this->assertSame("(", $tokens->getNext("(", ")"));
|
||||
|
||||
$tokens->next();
|
||||
$tokens->next();
|
||||
$this->assertSame("+", $tokens->getNext($tokens::MACRO_BINARY));
|
||||
}
|
||||
$tokens->next();
|
||||
$tokens->next();
|
||||
$this->assertSame("+", $tokens->getNext($tokens::MACRO_BINARY));
|
||||
}
|
||||
|
||||
public function testSkip() {
|
||||
public function testSkip()
|
||||
{
|
||||
$text = "1 foo: bar ( 3 + double ) ";
|
||||
$tokens = new Tokenizer($text);
|
||||
|
||||
$tokens->skip()->skip(T_STRING)->skip(':');
|
||||
try {
|
||||
$tokens->skip(T_STRING)->skip('(')->skip(':');
|
||||
} catch(\Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('Fenom\UnexpectedTokenException', $e);
|
||||
$this->assertStringStartsWith("Unexpected token '3' in expression, expect ':'", $e->getMessage());
|
||||
}
|
||||
|
@ -3,22 +3,25 @@
|
||||
use Fenom\Render,
|
||||
Fenom\Provider as FS;
|
||||
|
||||
class FenomTest extends \Fenom\TestCase {
|
||||
class FenomTest extends \Fenom\TestCase
|
||||
{
|
||||
|
||||
public static function providerOptions() {
|
||||
public static function providerOptions()
|
||||
{
|
||||
return array(
|
||||
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)
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
public function testCompileFile() {
|
||||
public function testCompileFile()
|
||||
{
|
||||
$a = array(
|
||||
"a" => "a",
|
||||
"b" => "b"
|
||||
@ -29,17 +32,19 @@ class FenomTest extends \Fenom\TestCase {
|
||||
$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'));
|
||||
$this->assertSame(3, iterator_count(new FilesystemIterator(FENOM_RESOURCES.'/compile')));
|
||||
$this->assertSame(3, iterator_count(new FilesystemIterator(FENOM_RESOURCES . '/compile')));
|
||||
}
|
||||
|
||||
public function testStorage() {
|
||||
public function testStorage()
|
||||
{
|
||||
$this->tpl('custom.tpl', 'Custom template');
|
||||
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
|
||||
$this->tpl('custom.tpl', 'Custom template 2');
|
||||
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
|
||||
}
|
||||
|
||||
public function 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()));
|
||||
@ -49,7 +54,8 @@ class FenomTest extends \Fenom\TestCase {
|
||||
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
|
||||
}
|
||||
|
||||
public function testForceCompile() {
|
||||
public function testForceCompile()
|
||||
{
|
||||
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
|
||||
$this->tpl('custom.tpl', 'Custom template');
|
||||
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
|
||||
@ -57,7 +63,8 @@ class FenomTest extends \Fenom\TestCase {
|
||||
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
|
||||
}
|
||||
|
||||
public function testSetModifier() {
|
||||
public function testSetModifier()
|
||||
{
|
||||
$this->fenom->addModifier("mymod", "myMod");
|
||||
$this->tpl('custom.tpl', 'Custom modifier {$a|mymod}');
|
||||
$this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->fenom->fetch('custom.tpl', array("a" => "Custom")));
|
||||
@ -66,7 +73,8 @@ class FenomTest extends \Fenom\TestCase {
|
||||
/**
|
||||
* @group add_functions
|
||||
*/
|
||||
public function testSetFunctions() {
|
||||
public function testSetFunctions()
|
||||
{
|
||||
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
|
||||
$this->fenom->addFunction("myfunc", "myFunc");
|
||||
$this->fenom->addBlockFunction("myblockfunc", "myBlockFunc");
|
||||
@ -76,22 +84,24 @@ class FenomTest extends \Fenom\TestCase {
|
||||
$this->assertSame("Custom function Block:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array()));
|
||||
}
|
||||
|
||||
public function testSetCompilers() {
|
||||
public function testSetCompilers()
|
||||
{
|
||||
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
|
||||
$this->fenom->addCompiler("mycompiler", 'myCompiler');
|
||||
$this->fenom->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
|
||||
'tag' => 'myBlockCompilerTag'
|
||||
));
|
||||
$this->tpl('custom.tpl', 'Custom compiler {mycompiler name="bar"}');
|
||||
$this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar)", $this->fenom->fetch('custom.tpl', array()));
|
||||
$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()));
|
||||
$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()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerOptions
|
||||
*/
|
||||
public function testOptions($code, $option) {
|
||||
public function testOptions($code, $option)
|
||||
{
|
||||
static $options = array();
|
||||
static $flags = 0;
|
||||
$options[$code] = true;
|
||||
|
@ -1,31 +1,38 @@
|
||||
<?php
|
||||
|
||||
function myMod($str) {
|
||||
return "(myMod)".$str."(/myMod)";
|
||||
function myMod($str)
|
||||
{
|
||||
return "(myMod)" . $str . "(/myMod)";
|
||||
}
|
||||
|
||||
function myFunc($params) {
|
||||
return "MyFunc:".$params["name"];
|
||||
function myFunc($params)
|
||||
{
|
||||
return "MyFunc:" . $params["name"];
|
||||
}
|
||||
|
||||
function myBlockFunc($params, $content) {
|
||||
return "Block:".$params["name"].':'.trim($content).':Block';
|
||||
function myBlockFunc($params, $content)
|
||||
{
|
||||
return "Block:" . $params["name"] . ':' . trim($content) . ':Block';
|
||||
}
|
||||
|
||||
function myCompiler(Fenom\Tokenizer $tokenizer, Fenom\Template $tpl) {
|
||||
function myCompiler(Fenom\Tokenizer $tokenizer, Fenom\Template $tpl)
|
||||
{
|
||||
$p = $tpl->parseParams($tokenizer);
|
||||
return 'echo "PHP_VERSION: ".PHP_VERSION." (for ".'.$p["name"].'.")";';
|
||||
return 'echo "PHP_VERSION: ".PHP_VERSION." (for ".' . $p["name"] . '.")";';
|
||||
}
|
||||
|
||||
function myBlockCompilerOpen(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope) {
|
||||
function myBlockCompilerOpen(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope)
|
||||
{
|
||||
return myCompiler($tokenizer, $scope->tpl);
|
||||
}
|
||||
|
||||
function myBlockCompilerClose(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope) {
|
||||
function myBlockCompilerClose(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope)
|
||||
{
|
||||
return 'echo "End of compiler";';
|
||||
}
|
||||
|
||||
function myBlockCompilerTag(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope) {
|
||||
function myBlockCompilerTag(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope)
|
||||
{
|
||||
$p = $scope->tpl->parseParams($tokenizer);
|
||||
return 'echo "Tag ".'.$p["name"].'." of compiler";';
|
||||
return 'echo "Tag ".' . $p["name"] . '." of compiler";';
|
||||
}
|
Reference in New Issue
Block a user