Refactory template ctor

Dev extends
This commit is contained in:
bzick
2013-02-13 20:51:27 +04:00
parent 95daa95cd2
commit 8c618c2b34
8 changed files with 260 additions and 136 deletions

View File

@@ -11,15 +11,19 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
public static $render;
public static function setUpBeforeClass() {
self::$render = new Render("render.tpl", function ($tpl) {
echo "It is render function ".$tpl["render"];
}, array());
self::$render = new Render(Aspect::factory("."), function ($tpl) {
echo "It is render's function ".$tpl["render"];
}, array(
"name" => "render.tpl"
));
}
public function testCreate() {
$r = new Render("test.render.tpl", function () {
$r = new Render(Aspect::factory("."), function () {
echo "Test render";
}, array());
}, array(
"name" => "test.render.tpl"
));
$this->assertSame("Test render", $r->fetch(array()));
}
@@ -27,22 +31,24 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
ob_start();
self::$render->display(array("render" => "display"));
$out = ob_get_clean();
$this->assertSame("It is render function display", $out);
$this->assertSame("It is render's function display", $out);
}
public function testFetch() {
$this->assertSame("It is render function fetch", self::$render->fetch(array("render" => "fetch")));
$this->assertSame("It is render's function fetch", self::$render->fetch(array("render" => "fetch")));
}
/**
* @expectedException RuntimeException
* @expectedException \RuntimeException
* @expectedExceptionMessage template error
*/
public function testFetchException() {
$render = new Render("render.tpl", function ($tpl) {
$render = new Render(Aspect::factory("."), function () {
echo "error";
throw new \RuntimeException("template error");
});
}, array(
"name" => "render.tpl"
));
$render->fetch(array());
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Aspect\Template;
use Aspect, Aspect\Modifier;
class ExtendsTest extends \PHPUnit_Framework_TestCase {
/**
* @var Aspect
*/
public static $aspect;
public function setUp() {
if(!file_exists(ASPECT_RESOURCES.'/compile')) {
mkdir(ASPECT_RESOURCES.'/compile', 0777, true);
} else {
exec("rm -f ".ASPECT_RESOURCES.'/compile/*');
}
self::$aspect = Aspect::factory(ASPECT_RESOURCES.'/template', ASPECT_RESOURCES.'/compile');
}
public static function providerExtends() {
return array(
array('{extends "parent.tpl"}{block "bk1"} block1 {/block}', "Template extended by block1"),
array('{extends "parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage', "Template extended by block1"),
array('{extends "parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} {block "bk3"} block3 {/block} garbage', "Template multi-extended by block1"),
array('{extends "parent.tpl"}{var $bk = "bk3"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} {block "$bk"} block3 {/block} garbage', "Template multi-extended by block1"),
);
}
public function exec($code, $vars, $result, $dump = false) {
$tpl = self::$aspect->compileCode($code, "inline.tpl");
if($dump) {
echo "\n===========================\n".$code.": ".$tpl->getBody();
}
$this->assertSame(Modifier::strip($result), Modifier::strip($tpl->fetch($vars), true), "Test $code");
}
public function execError($code, $exception, $message, $options) {
self::$aspect->setOptions($options);
try {
self::$aspect->compileCode($code, "inline.tpl");
} catch(\Exception $e) {
$this->assertSame($exception, get_class($e), "Exception $code");
$this->assertStringStartsWith($message, $e->getMessage());
self::$aspect->setOptions(0);
return;
}
self::$aspect->setOptions(0);
$this->fail("Code $code must be invalid");
}
/**
* @group extends
*/
public function testParent() {
//echo(self::$aspect->getTemplate("parent.tpl")->getBody()); exit;
}
/**
* @group extends
*/
public function ___testChildLevel1() {
echo(self::$aspect->getTemplate("child1.tpl")->getBody()); exit;
}
/**
* @group extends
*/
public function __testExtends() {
echo(self::$aspect->fetch("child1.tpl", array("a" => "value", "z" => ""))."\n"); exit;
}
}

View File

@@ -17,9 +17,11 @@ class TemplateTest extends \PHPUnit_Framework_TestCase {
exec("rm -f ".ASPECT_RESOURCES.'/compile/*');
}
self::$aspect = Aspect::factory(ASPECT_RESOURCES.'/template', ASPECT_RESOURCES.'/compile');
self::$aspect->addTemplate(new Render("welcome.tpl", function ($tpl) {
self::$aspect->addTemplate(new Render(self::$aspect, function ($tpl) {
echo "<b>Welcome, ".$tpl["username"]." (".$tpl["email"].")</b>";
}, array()));
}, array(
"name" => "welcome.tpl"
)));
}

View File

@@ -21,7 +21,6 @@ class AspectTest extends \PHPUnit_Framework_TestCase {
self::tearDownAfterClass();
$this->aspect = $aspect = Aspect::factory(ASPECT_RESOURCES.'/template', ASPECT_RESOURCES.'/compile');
$aspect->setCompileDir(ASPECT_RESOURCES.'/compile');
$aspect->addTemplateDir(ASPECT_RESOURCES.'/template');
$aspect->setForceCompile(false);
$aspect->setCompileCheck(false);
}
@@ -36,11 +35,13 @@ class AspectTest extends \PHPUnit_Framework_TestCase {
public function testAddRender() {
$test = $this;
$this->aspect->addTemplate(new Render('render.tpl', function($tpl) use ($test) {
$this->aspect->addTemplate(new Render($this->aspect, function($tpl) use ($test) {
/** @var \PHPUnit_Framework_TestCase $test */
$test->assertInstanceOf('Aspect\Render', $tpl);
echo "Inline render";
}, array()));
}, array(
"name" => 'render.tpl'
)));
$this->assertSame("Inline render", $this->aspect->fetch('render.tpl', array()));
}