mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Reformat. Fix pipe. Add more tests
This commit is contained in:
@@ -12,38 +12,38 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
public $fenom;
|
||||
|
||||
public $values = array(
|
||||
"zero" => 0,
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"zero" => 0,
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"three" => 3,
|
||||
"float" => 4.5,
|
||||
"bool" => true,
|
||||
0 => "empty value",
|
||||
1 => "one value",
|
||||
2 => "two value",
|
||||
3 => "three value",
|
||||
"bool" => true,
|
||||
0 => "empty value",
|
||||
1 => "one value",
|
||||
2 => "two value",
|
||||
3 => "three value",
|
||||
);
|
||||
|
||||
public static function getVars()
|
||||
{
|
||||
return array(
|
||||
"zero" => 0,
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"zero" => 0,
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"three" => 3,
|
||||
"float" => 4.5,
|
||||
"bool" => true,
|
||||
"obj" => new \StdClass,
|
||||
"list" => array(
|
||||
"a" => 1,
|
||||
"bool" => true,
|
||||
"obj" => new \StdClass,
|
||||
"list" => array(
|
||||
"a" => 1,
|
||||
"one" => 1,
|
||||
"b" => 2,
|
||||
"b" => 2,
|
||||
"two" => 2
|
||||
),
|
||||
0 => "empty value",
|
||||
1 => "one value",
|
||||
2 => "two value",
|
||||
3 => "three value",
|
||||
0 => "empty value",
|
||||
1 => "one value",
|
||||
2 => "two value",
|
||||
3 => "three value",
|
||||
);
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/' . $this->template_path, FENOM_RESOURCES . '/compile');
|
||||
$this->fenom->addProvider('persist', new Provider(FENOM_RESOURCES . '/provider'));
|
||||
$this->fenom->addModifier('dots', __CLASS__ . '::dots');
|
||||
$this->fenom->addModifier('concat', __CLASS__ . '::concat');
|
||||
$this->fenom->addModifier('append', __CLASS__ . '::append');
|
||||
@@ -123,7 +124,8 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
$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";
|
||||
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 $code");
|
||||
return $tpl;
|
||||
@@ -134,7 +136,8 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
$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";
|
||||
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");
|
||||
}
|
||||
@@ -234,7 +237,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
public static function providerArrays()
|
||||
{
|
||||
$scalars = array();
|
||||
$data = array(
|
||||
$data = array(
|
||||
array('[]', array()),
|
||||
array('[[],[]]', array(array(), array())),
|
||||
);
|
||||
|
||||
@@ -19,7 +19,7 @@ class AutoEscapeTest extends TestCase
|
||||
array('{$html}, {$html}', "$html, $html", $vars, 0),
|
||||
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}, {$html}', "$html, $html", $vars, 0),
|
||||
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),
|
||||
@@ -29,34 +29,122 @@ class AutoEscapeTest extends TestCase
|
||||
array('{autoescape false}{raw $html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{raw $html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape false}{raw $html}{/autoescape}, {$html}', "$html, $html", $vars, 0),
|
||||
|
||||
// inline function
|
||||
array('{test_function text=$html}, {$html}', "$html, $html", $vars, 0),
|
||||
array('{test_function text=$html}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{test_function:raw text=$html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{test_function:raw 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),
|
||||
array('{autoescape false}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$html, $html", $vars, 0),
|
||||
array('{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $html", $vars, 0),
|
||||
array('{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $html", $vars, 0),
|
||||
|
||||
array(
|
||||
'{test_function:raw 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
|
||||
),
|
||||
array(
|
||||
'{autoescape false}{test_function text=$html}{/autoescape}, {test_function text=$html}',
|
||||
"$html, $html",
|
||||
$vars,
|
||||
0
|
||||
),
|
||||
array(
|
||||
'{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
|
||||
"$html, $html",
|
||||
$vars,
|
||||
0
|
||||
),
|
||||
array(
|
||||
'{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
|
||||
"$html, $escaped",
|
||||
$vars,
|
||||
\Fenom::AUTO_ESCAPE
|
||||
),
|
||||
array(
|
||||
'{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
|
||||
"$html, $escaped",
|
||||
$vars,
|
||||
\Fenom::AUTO_ESCAPE
|
||||
),
|
||||
array(
|
||||
'{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
|
||||
"$html, $html",
|
||||
$vars,
|
||||
0
|
||||
),
|
||||
// block function
|
||||
array('{test_block_function}{$html}{/test_block_function}', $html, $vars, 0),
|
||||
array('{test_block_function}{$html}{/test_block_function}', $escaped, $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{test_block_function:raw}{$html}{/test_block_function}', $html, $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{test_block_function:raw}{"{$html|up}"}{/test_block_function}', strtoupper($html), $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$escaped, $html", $vars, 0),
|
||||
array('{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $html", $vars, 0),
|
||||
array('{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $html", $vars, 0),
|
||||
array('{autoescape false}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}', "$escaped, $html", $vars, \Fenom::AUTO_ESCAPE),
|
||||
array('{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}', "$html, $html", $vars, 0),
|
||||
array(
|
||||
'{test_block_function:raw}{"{$html|up}"}{/test_block_function}',
|
||||
strtoupper($html),
|
||||
$vars,
|
||||
\Fenom::AUTO_ESCAPE
|
||||
),
|
||||
array(
|
||||
'{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
|
||||
"$escaped, $html",
|
||||
$vars,
|
||||
0
|
||||
),
|
||||
array(
|
||||
'{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
|
||||
"$html, $escaped",
|
||||
$vars,
|
||||
\Fenom::AUTO_ESCAPE
|
||||
),
|
||||
array(
|
||||
'{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
|
||||
"$escaped, $escaped",
|
||||
$vars,
|
||||
\Fenom::AUTO_ESCAPE
|
||||
),
|
||||
array(
|
||||
'{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
|
||||
"$html, $html",
|
||||
$vars,
|
||||
0
|
||||
),
|
||||
array(
|
||||
'{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
|
||||
"$html, $html",
|
||||
$vars,
|
||||
0
|
||||
),
|
||||
array(
|
||||
'{autoescape false}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
|
||||
"$html, $escaped",
|
||||
$vars,
|
||||
\Fenom::AUTO_ESCAPE
|
||||
),
|
||||
array(
|
||||
'{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}',
|
||||
"$escaped, $html",
|
||||
$vars,
|
||||
\Fenom::AUTO_ESCAPE
|
||||
),
|
||||
array(
|
||||
'{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}',
|
||||
"$html, $html",
|
||||
$vars,
|
||||
0
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,9 +21,21 @@ class ExtendsTest extends TestCase
|
||||
public static function providerExtendsInvalid()
|
||||
{
|
||||
return array(
|
||||
array('{extends "extends/dynamic/child.3.tpl"} {extends "extends/dynamic/child.3.tpl"}', 'Fenom\Error\CompileException', "Only one {extends} allowed"),
|
||||
array('{if true}{extends "extends/dynamic/child.3.tpl"}{/if}', 'Fenom\Error\CompileException', "Tag {extends} can not be nested"),
|
||||
array('{if true}{use "extends/dynamic/use.tpl"}{/if}', 'Fenom\Error\CompileException', "Tag {use} can not be nested"),
|
||||
array(
|
||||
'{extends "extends/dynamic/child.3.tpl"} {extends "extends/dynamic/child.3.tpl"}',
|
||||
'Fenom\Error\CompileException',
|
||||
"Only one {extends} allowed"
|
||||
),
|
||||
array(
|
||||
'{if true}{extends "extends/dynamic/child.3.tpl"}{/if}',
|
||||
'Fenom\Error\CompileException',
|
||||
"Tag {extends} can not be nested"
|
||||
),
|
||||
array(
|
||||
'{if true}{use "extends/dynamic/use.tpl"}{/if}',
|
||||
'Fenom\Error\CompileException',
|
||||
"Tag {use} can not be nested"
|
||||
),
|
||||
array('{use $use_this}', 'Fenom\Error\CompileException', "Invalid template name for tag {use}"),
|
||||
array('{block $use_this}{/block}', 'Fenom\Error\CompileException', "Invalid block name"),
|
||||
);
|
||||
@@ -54,12 +66,18 @@ Before body
|
||||
Child 3 content
|
||||
Before footer
|
||||
Footer from use";
|
||||
$this->assertSame($result, $this->fenom->fetch(array(
|
||||
'extends/auto/child.3.tpl',
|
||||
'extends/auto/child.2.tpl',
|
||||
'extends/auto/child.1.tpl',
|
||||
'extends/auto/parent.tpl',
|
||||
), array()));
|
||||
$this->assertSame(
|
||||
$result,
|
||||
$this->fenom->fetch(
|
||||
array(
|
||||
'extends/auto/child.3.tpl',
|
||||
'extends/auto/child.2.tpl',
|
||||
'extends/auto/child.1.tpl',
|
||||
'extends/auto/parent.tpl',
|
||||
),
|
||||
array()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function testStaticExtendLevel1()
|
||||
@@ -92,11 +110,17 @@ Before body
|
||||
Child 3 content
|
||||
Before footer
|
||||
Footer from use";
|
||||
$this->assertSame($result, $this->fenom->fetch(array(
|
||||
'extends/auto/child.3.tpl',
|
||||
'extends/auto/child.2.tpl',
|
||||
'extends/auto/static/child.1.tpl'
|
||||
), array()));
|
||||
$this->assertSame(
|
||||
$result,
|
||||
$this->fenom->fetch(
|
||||
array(
|
||||
'extends/auto/child.3.tpl',
|
||||
'extends/auto/child.2.tpl',
|
||||
'extends/auto/static/child.1.tpl'
|
||||
),
|
||||
array()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function testStaticExtendNested()
|
||||
|
||||
@@ -7,56 +7,74 @@ class MacrosTest extends TestCase
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->tpl("math.tpl", '
|
||||
{macro plus(x, y)}
|
||||
x + y = {$x + $y}
|
||||
{/macro}
|
||||
$this->tpl(
|
||||
"math.tpl",
|
||||
'
|
||||
{macro plus(x, y)}
|
||||
x + y = {$x + $y}
|
||||
{/macro}
|
||||
|
||||
{macro minus(x, y, z=0)}
|
||||
x - y - z = {$x - $y - $z}
|
||||
{/macro}
|
||||
{macro minus(x, y, z=0)}
|
||||
x - y - z = {$x - $y - $z}
|
||||
{/macro}
|
||||
|
||||
{macro multi(x, y)}
|
||||
x * y = {$x * $y}
|
||||
{/macro}
|
||||
{macro multi(x, y)}
|
||||
x * y = {$x * $y}
|
||||
{/macro}
|
||||
|
||||
Math: {macro.plus x=2 y=3}, {macro.minus x=10 y=4}
|
||||
');
|
||||
Math: {macro.plus x=2 y=3}, {macro.minus x=10 y=4}
|
||||
'
|
||||
);
|
||||
|
||||
$this->tpl("import.tpl", '
|
||||
{import "math.tpl"}
|
||||
{import "math.tpl" as math}
|
||||
$this->tpl(
|
||||
"import.tpl",
|
||||
'
|
||||
{import "math.tpl"}
|
||||
{import "math.tpl" as math}
|
||||
|
||||
Imp: {macro.plus x=1 y=2}, {math.minus x=6 y=2 z=1}
|
||||
');
|
||||
Imp: {macro.plus x=1 y=2}, {math.minus x=6 y=2 z=1}
|
||||
'
|
||||
);
|
||||
|
||||
$this->tpl("import_custom.tpl", '
|
||||
{macro minus($x, $y)}
|
||||
new minus macros
|
||||
{/macro}
|
||||
{import [plus, minus] from "math.tpl" as math}
|
||||
$this->tpl(
|
||||
"import_custom.tpl",
|
||||
'
|
||||
{macro minus($x, $y)}
|
||||
new minus macros
|
||||
{/macro}
|
||||
{import [plus, minus] from "math.tpl" as math}
|
||||
|
||||
a: {math.plus x=1 y=2}, {math.minus x=6 y=2 z=1}, {macro.minus x=5 y=3}.
|
||||
');
|
||||
a: {math.plus x=1 y=2}, {math.minus x=6 y=2 z=1}, {macro.minus x=5 y=3}.
|
||||
'
|
||||
);
|
||||
|
||||
$this->tpl("import_miss.tpl", '
|
||||
{import [minus] from "math.tpl" as math}
|
||||
$this->tpl(
|
||||
"import_miss.tpl",
|
||||
'
|
||||
{import [minus] from "math.tpl" as math}
|
||||
|
||||
a: {macro.plus x=5 y=3}.
|
||||
');
|
||||
a: {macro.plus x=5 y=3}.
|
||||
'
|
||||
);
|
||||
|
||||
$this->tpl("macro_recursive.tpl", '{macro factorial(num)}
|
||||
{if $num}
|
||||
{$num} {macro.factorial num=$num-1} {$num}
|
||||
{/if}
|
||||
{/macro}
|
||||
$this->tpl(
|
||||
"macro_recursive.tpl",
|
||||
'{macro factorial(num)}
|
||||
{if $num}
|
||||
{$num} {macro.factorial num=$num-1} {$num}
|
||||
{/if}
|
||||
{/macro}
|
||||
|
||||
{macro.factorial num=10}');
|
||||
{macro.factorial num=10}'
|
||||
);
|
||||
|
||||
$this->tpl("macro_recursive_import.tpl", '
|
||||
{import "macro_recursive.tpl" as math}
|
||||
$this->tpl(
|
||||
"macro_recursive_import.tpl",
|
||||
'
|
||||
{import "macro_recursive.tpl" as math}
|
||||
|
||||
{math.factorial num=10}');
|
||||
{math.factorial num=10}'
|
||||
);
|
||||
}
|
||||
|
||||
public function _testSandbox()
|
||||
@@ -65,11 +83,15 @@ class MacrosTest extends TestCase
|
||||
// $this->fenom->compile("macro_recursive.tpl")->display([]);
|
||||
// $this->fenom->flush();
|
||||
// var_dump($this->fenom->fetch("macro_recursive.tpl", []));
|
||||
var_dump($this->fenom->compileCode('{macro factorial(num)}
|
||||
{if $num}
|
||||
{$num} {macro.factorial num=$num-1} {$num}
|
||||
{/if}
|
||||
{/macro}')->getBody());
|
||||
var_dump(
|
||||
$this->fenom->compileCode(
|
||||
'{macro factorial(num)}
|
||||
{if $num}
|
||||
{$num} {macro.factorial num=$num-1} {$num}
|
||||
{/if}
|
||||
{/macro}'
|
||||
)->getBody()
|
||||
);
|
||||
// var_dump($this->fenom->display("macro_recursive_import.tpl", array()));
|
||||
} catch (\Exception $e) {
|
||||
var_dump($e->getMessage() . ": " . $e->getTraceAsString());
|
||||
@@ -99,7 +121,10 @@ class MacrosTest extends TestCase
|
||||
{
|
||||
$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));
|
||||
$this->assertSame(
|
||||
'a: x + y = 3 , x - y - z = 3 , new minus macros .',
|
||||
Modifier::strip($tpl->fetch(array()), true)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,7 +135,10 @@ class MacrosTest extends TestCase
|
||||
{
|
||||
$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));
|
||||
$this->assertSame(
|
||||
'a: x + y = 3 , x - y - z = 3 , new minus macros .',
|
||||
Modifier::strip($tpl->fetch(array()), true)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,7 @@ class ModifiersTest extends TestCase
|
||||
public static function providerTruncate()
|
||||
{
|
||||
$lorem = 'Lorem ipsum dolor sit amet'; // en
|
||||
$uni = 'Лорем ипсум долор сит амет'; // ru
|
||||
$uni = 'Лорем ипсум долор сит амет'; // ru
|
||||
return array(
|
||||
// ascii chars
|
||||
array($lorem, 'Lorem ip...', 8),
|
||||
@@ -38,13 +38,18 @@ class ModifiersTest extends TestCase
|
||||
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,
|
||||
"count" => $count,
|
||||
"delim" => $delim,
|
||||
"by_words" => $by_words,
|
||||
"middle" => $middle
|
||||
)));
|
||||
$this->assertEquals(
|
||||
$out,
|
||||
$tpl->fetch(
|
||||
array(
|
||||
"text" => $in,
|
||||
"count" => $count,
|
||||
"delim" => $delim,
|
||||
"by_words" => $by_words,
|
||||
"middle" => $middle
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerUpLow()
|
||||
@@ -71,9 +76,14 @@ class ModifiersTest extends TestCase
|
||||
public function testUpLow($modifier, $in, $out)
|
||||
{
|
||||
$tpl = $this->fenom->compileCode('{$text|' . $modifier . '}');
|
||||
$this->assertEquals($out, $tpl->fetch(array(
|
||||
"text" => $in,
|
||||
)));
|
||||
$this->assertEquals(
|
||||
$out,
|
||||
$tpl->fetch(
|
||||
array(
|
||||
"text" => $in,
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerLength()
|
||||
@@ -99,9 +109,14 @@ class ModifiersTest extends TestCase
|
||||
public function testLength($in, $out)
|
||||
{
|
||||
$tpl = $this->fenom->compileCode('{$data|length}');
|
||||
$this->assertEquals($out, $tpl->fetch(array(
|
||||
"data" => $in,
|
||||
)));
|
||||
$this->assertEquals(
|
||||
$out,
|
||||
$tpl->fetch(
|
||||
array(
|
||||
"data" => $in,
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -88,7 +88,7 @@ class ProviderTest extends TestCase
|
||||
clearstatcache();
|
||||
$templates = array(
|
||||
"template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
|
||||
"unexists.tpl" => 1234567890
|
||||
"unexists.tpl" => 1234567890
|
||||
);
|
||||
$this->assertFalse($this->provider->verify($templates));
|
||||
}
|
||||
@@ -97,11 +97,14 @@ class ProviderTest extends TestCase
|
||||
{
|
||||
$list = $this->provider->getList();
|
||||
sort($list);
|
||||
$this->assertSame(array(
|
||||
"sub/template3.tpl",
|
||||
"template1.tpl",
|
||||
"template2.tpl"
|
||||
), $list);
|
||||
$this->assertSame(
|
||||
array(
|
||||
"sub/template3.tpl",
|
||||
"template1.tpl",
|
||||
"template2.tpl"
|
||||
),
|
||||
$list
|
||||
);
|
||||
}
|
||||
|
||||
public function testRm()
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
namespace Fenom;
|
||||
|
||||
|
||||
class TagOptionTest extends TestCase {
|
||||
class TagOptionTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
public function testTrim() {
|
||||
public function testTrim()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,10 @@ class TagsTest extends TestCase
|
||||
*/
|
||||
public function testVarBlockModified($tpl_val, $val)
|
||||
{
|
||||
$this->assertRender("{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}", "Var: " . strtolower("before " . $val . " after") . "...");
|
||||
$this->assertRender(
|
||||
"{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}",
|
||||
"Var: " . strtolower("before " . $val . " after") . "..."
|
||||
);
|
||||
}
|
||||
|
||||
public function testCycle()
|
||||
@@ -50,7 +53,10 @@ class TagsTest extends TestCase
|
||||
*/
|
||||
public function testCycleIndex()
|
||||
{
|
||||
$this->assertRender('{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}', "two, one, two, one, two, ");
|
||||
$this->assertRender(
|
||||
'{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}',
|
||||
"two, one, two, one, two, "
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -17,7 +17,7 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testTokens()
|
||||
{
|
||||
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
|
||||
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
|
||||
$tokens = new Tokenizer($code);
|
||||
$this->assertSame(27, $tokens->count());
|
||||
$this->assertSame($tokens, $tokens->back());
|
||||
@@ -38,13 +38,16 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertSame(",", $tokens->getNext());
|
||||
$this->assertSame(",", $tokens->key());
|
||||
$this->assertSame("please", $tokens->getNext(T_STRING));
|
||||
$this->assertSame(array(
|
||||
T_STRING,
|
||||
'please',
|
||||
' ',
|
||||
1,
|
||||
'T_STRING'
|
||||
), $tokens->curr);
|
||||
$this->assertSame(
|
||||
array(
|
||||
T_STRING,
|
||||
'please',
|
||||
' ',
|
||||
1,
|
||||
'T_STRING'
|
||||
),
|
||||
$tokens->curr
|
||||
);
|
||||
$this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING));
|
||||
|
||||
$tokens->next();
|
||||
@@ -80,7 +83,7 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testSkip()
|
||||
{
|
||||
$text = "1 foo: bar ( 3 + double ) ";
|
||||
$text = "1 foo: bar ( 3 + double ) ";
|
||||
$tokens = new Tokenizer($text);
|
||||
|
||||
$tokens->skip()->skip(T_STRING)->skip(':');
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
<?php
|
||||
|
||||
use Fenom\Render,
|
||||
Fenom\Provider as FS;
|
||||
|
||||
class FenomTest extends \Fenom\TestCase
|
||||
{
|
||||
|
||||
@@ -20,9 +17,10 @@ class FenomTest extends \Fenom\TestCase
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testCreating()
|
||||
{
|
||||
$time = $this->tpl('temp.tpl', 'Template 1 a');
|
||||
$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'));
|
||||
@@ -35,8 +33,12 @@ class FenomTest extends \Fenom\TestCase
|
||||
|
||||
public function testFactory()
|
||||
{
|
||||
$time = $this->tpl('temp.tpl', 'Template 1 a');
|
||||
$fenom = Fenom::factory($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'), FENOM_RESOURCES . '/compile', Fenom::AUTO_ESCAPE);
|
||||
$time = $this->tpl('temp.tpl', 'Template 1 a');
|
||||
$fenom = Fenom::factory(
|
||||
$provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'),
|
||||
FENOM_RESOURCES . '/compile',
|
||||
Fenom::AUTO_ESCAPE
|
||||
);
|
||||
$this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl'));
|
||||
$this->assertSame($provider, $tpl->getProvider());
|
||||
$this->assertSame('temp.tpl', $tpl->getBaseName());
|
||||
@@ -45,6 +47,25 @@ class FenomTest extends \Fenom\TestCase
|
||||
$fenom->clearAllCompiles();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
public function testCompileFile()
|
||||
{
|
||||
$a = array(
|
||||
@@ -97,7 +118,10 @@ class FenomTest extends \Fenom\TestCase
|
||||
{
|
||||
$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")));
|
||||
$this->assertSame(
|
||||
"Custom modifier (myMod)Custom(/myMod)",
|
||||
$this->fenom->fetch('custom.tpl', array("a" => "Custom"))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,13 +142,27 @@ class FenomTest extends \Fenom\TestCase
|
||||
{
|
||||
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
|
||||
$this->fenom->addCompiler("mycompiler", 'myCompiler');
|
||||
$this->fenom->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
|
||||
'tag' => 'myBlockCompilerTag'
|
||||
));
|
||||
$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->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)",
|
||||
$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())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,23 +184,35 @@ class FenomTest extends \Fenom\TestCase
|
||||
public function testFilter()
|
||||
{
|
||||
$punit = $this;
|
||||
$this->fenom->addPreFilter(function ($tpl, $src) use ($punit) {
|
||||
$punit->assertInstanceOf('Fenom\Template', $tpl);
|
||||
return "== $src ==";
|
||||
});
|
||||
$this->fenom->addPreFilter(
|
||||
function ($tpl, $src) use ($punit) {
|
||||
$punit->assertInstanceOf('Fenom\Template', $tpl);
|
||||
return "== $src ==";
|
||||
}
|
||||
);
|
||||
|
||||
$this->fenom->addPostFilter(function ($tpl, $code) use ($punit) {
|
||||
$punit->assertInstanceOf('Fenom\Template', $tpl);
|
||||
return "+++ $code +++";
|
||||
});
|
||||
$this->fenom->addPostFilter(
|
||||
function ($tpl, $code) use ($punit) {
|
||||
$punit->assertInstanceOf('Fenom\Template', $tpl);
|
||||
return "+++ $code +++";
|
||||
}
|
||||
);
|
||||
|
||||
$this->fenom->addFilter(function ($tpl, $text) use ($punit) {
|
||||
$punit->assertInstanceOf('Fenom\Template', $tpl);
|
||||
return "|--- $text ---|";
|
||||
});
|
||||
$this->fenom->addFilter(
|
||||
function ($tpl, $text) use ($punit) {
|
||||
$punit->assertInstanceOf('Fenom\Template', $tpl);
|
||||
return "|--- $text ---|";
|
||||
}
|
||||
);
|
||||
|
||||
$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()));
|
||||
$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())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,13 +220,15 @@ class FenomTest extends \Fenom\TestCase
|
||||
*/
|
||||
public function testTagFilter()
|
||||
{
|
||||
$tags = array();
|
||||
$tags = array();
|
||||
$punit = $this;
|
||||
$this->fenom->addTagFilter(function ($text, $tpl) use (&$tags, $punit) {
|
||||
$punit->assertInstanceOf('Fenom\Template', $tpl);
|
||||
$tags[] = $text;
|
||||
return $text;
|
||||
});
|
||||
$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}!');
|
||||
|
||||
@@ -194,8 +246,10 @@ class FenomTest extends \Fenom\TestCase
|
||||
{
|
||||
$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()));
|
||||
$this->assertSame(
|
||||
'Start saying and say blah-blah-blah. It is all, Stop saying',
|
||||
$this->fenom->fetch('block_compiler.tpl', array())
|
||||
);
|
||||
}
|
||||
|
||||
public function testAddFunctions()
|
||||
@@ -205,6 +259,35 @@ class FenomTest extends \Fenom\TestCase
|
||||
$this->fenom->addAllowedFunctions(array('substr'));
|
||||
$this->assertTrue($this->fenom->isAllowedFunction('substr'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
1
tests/resources/provider/pipe.tpl
Normal file
1
tests/resources/provider/pipe.tpl
Normal file
@@ -0,0 +1 @@
|
||||
{foreach $items as $key => $value}{$key}:{$value}{/foreach}
|
||||
Reference in New Issue
Block a user