fenom/tests/cases/Fenom/ExtendsTemplateTest.php

158 lines
4.7 KiB
PHP
Raw Normal View History

2013-04-22 10:15:14 +04:00
<?php
2013-06-28 11:53:53 +04:00
namespace Fenom;
use Fenom, Fenom\TestCase;
2013-04-22 10:15:14 +04:00
2013-07-29 14:58:14 +04:00
class ExtendsTemplateTest extends TestCase
{
2013-04-22 10:15:14 +04:00
2013-07-29 14:58:14 +04:00
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;
}
2013-06-08 00:08:00 +04:00
2013-07-29 14:58:14 +04:00
/**
* Templates skeletons
* @param array $vars
* @return array
*/
public static function templates(array $vars)
{
2013-04-22 10:15:14 +04:00
return array(
array(
2013-07-29 14:58:14 +04:00
"name" => "level.0.tpl",
2013-04-22 10:15:14 +04:00
"level" => 0,
"blocks" => array(
2013-05-19 02:04:52 +04:00
"b1" => "default {\$default}",
2013-04-22 10:15:14 +04:00
"b2" => "empty 0"
),
"result" => array(
2013-07-29 14:58:14 +04:00
"b1" => "default " . $vars["default"],
2013-04-22 10:15:14 +04:00
"b2" => "empty 0"
),
),
array(
2013-07-29 14:58:14 +04:00
"name" => "level.1.tpl",
2013-04-22 10:15:14 +04:00
"level" => 1,
2013-07-29 14:58:14 +04:00
"use" => false,
2013-04-22 10:15:14 +04:00
"blocks" => array(
"b1" => "from level 1"
),
"result" => array(
"b1" => "from level 1",
"b2" => "empty 0"
),
),
array(
2013-07-29 14:58:14 +04:00
"name" => "level.2.tpl",
2013-04-22 10:15:14 +04:00
"level" => 2,
2013-07-29 14:58:14 +04:00
"use" => false,
2013-04-22 10:15:14 +04:00
"blocks" => array(
"b2" => "from level 2",
"b4" => "unused block"
),
"result" => array(
"b1" => "from level 1",
"b2" => "from level 2"
),
),
array(
2013-07-29 14:58:14 +04:00
"name" => "level.3.tpl",
2013-04-22 10:15:14 +04:00
"level" => 3,
2013-07-29 14:58:14 +04:00
"use" => false,
2013-04-22 10:15:14 +04:00
"blocks" => array(
"b1" => "from level 3",
"b2" => "also from level 3"
),
"result" => array(
"b1" => "from level 3",
"b2" => "also from level 3"
),
)
);
}
2013-07-29 14:58:14 +04:00
/**
* Generate templates by skeletons
*
* @param $block_mask
* @param $extend_mask
* @param array $skels
* @return array
*/
public static function generate($block_mask, $extend_mask, $skels)
{
2013-04-22 10:15:14 +04:00
$t = array();
2013-07-29 14:58:14 +04:00
foreach ($skels as $level => $tpl) {
$src = 'level#' . $level . ' ';
2013-06-08 00:08:00 +04:00
2013-07-29 14:58:14 +04:00
foreach ($tpl["blocks"] as $bname => &$bcode) {
$src .= sprintf($block_mask, $bname, $bname . ': ' . $bcode) . " level#$level ";
2013-04-22 10:15:14 +04:00
}
$dst = "level#0 ";
2013-07-29 14:58:14 +04:00
foreach ($tpl["result"] as $bname => &$bcode) {
$dst .= $bname . ': ' . $bcode . ' level#0 ';
2013-04-22 10:15:14 +04:00
}
2013-07-29 14:58:14 +04:00
if ($level) {
$src = sprintf($extend_mask, $level - 1) . ' ' . $src;
2013-04-22 10:15:14 +04:00
}
2013-07-29 14:58:14 +04:00
$t[$tpl["name"]] = array("src" => $src, "dst" => $dst);
2013-04-22 10:15:14 +04:00
}
return $t;
}
2013-06-08 00:08:00 +04:00
2013-07-29 14:58:14 +04:00
public function _testTemplateExtends()
{
2013-04-28 11:33:36 +04:00
$vars = array(
"b1" => "b1",
"b2" => "b2",
"b3" => "b3",
"b4" => "b4",
"level" => "level",
"default" => 5
);
2013-06-08 00:08:00 +04:00
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
2013-07-29 14:58:14 +04:00
foreach ($tpls as $name => $tpl) {
2013-04-22 10:15:14 +04:00
$this->tpl($name, $tpl["src"]);
2013-06-28 11:53:53 +04:00
$this->assertSame($this->fenom->fetch($name, $vars), $tpl["dst"]);
2013-04-28 11:33:36 +04:00
}
2013-07-29 14:58:14 +04:00
return;
2013-05-19 02:04:52 +04:00
$vars["default"]++;
2013-06-28 11:53:53 +04:00
$this->fenom->flush();
2013-06-08 00:08:00 +04:00
$tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
2013-04-28 11:33:36 +04:00
arsort($tpls);
2013-07-29 14:58:14 +04:00
foreach ($tpls as $name => $tpl) {
$this->tpl("d." . $name, $tpl["src"]);
$this->assertSame($this->fenom->fetch("d." . $name, $vars), $tpl["dst"]);
2013-05-19 02:04:52 +04:00
}
$vars["default"]++;
2013-06-28 11:53:53 +04:00
$this->fenom->flush();
2013-06-08 00:08:00 +04:00
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "$level.%d.tpl"}', self::templates($vars));
2013-05-19 02:04:52 +04:00
arsort($tpls);
2013-07-29 14:58:14 +04:00
foreach ($tpls as $name => $tpl) {
$this->tpl("x." . $name, $tpl["src"]);
$this->assertSame($this->fenom->fetch("x." . $name, $vars), $tpl["dst"]);
2013-04-22 10:15:14 +04:00
}
}
2013-06-08 00:08:00 +04:00
2013-07-29 14:58:14 +04:00
/**
* @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'));
}
2013-06-08 00:08:00 +04:00
2013-07-29 14:58:14 +04:00
public function _testParent()
{
2013-06-08 00:08:00 +04:00
2013-07-29 14:58:14 +04:00
}
2013-04-22 10:15:14 +04:00
}