Mass refactory

Rename project
This commit is contained in:
bzick
2013-04-04 10:56:44 +04:00
parent 281757b902
commit 1e97857ea8
56 changed files with 478 additions and 1302 deletions

View File

@@ -1,32 +1,41 @@
<?php
namespace Aspect;
use Aspect, Aspect\FSProvider as FS;
namespace Cytro;
use Cytro, Cytro\FSProvider as FS;
class TestCase extends \PHPUnit_Framework_TestCase {
/**
* @var Aspect
* @var Cytro
*/
public $aspect;
public $cytro;
public $values = array(
"one" => 1,
"two" => 2,
"three" => 3,
1 => "one value",
2 => "two value",
3 => "three value",
);
public function setUp() {
if(!file_exists(ASPECT_RESOURCES.'/compile')) {
mkdir(ASPECT_RESOURCES.'/compile', 0777, true);
if(!file_exists(CYTRO_RESOURCES.'/compile')) {
mkdir(CYTRO_RESOURCES.'/compile', 0777, true);
} else {
FS::clean(ASPECT_RESOURCES.'/compile/');
FS::clean(CYTRO_RESOURCES.'/compile/');
}
$this->aspect = Aspect::factory(ASPECT_RESOURCES.'/template', ASPECT_RESOURCES.'/compile');
$this->cytro = Cytro::factory(CYTRO_RESOURCES.'/template', CYTRO_RESOURCES.'/compile');
}
public static function setUpBeforeClass() {
if(!file_exists(ASPECT_RESOURCES.'/template')) {
mkdir(ASPECT_RESOURCES.'/template', 0777, true);
if(!file_exists(CYTRO_RESOURCES.'/template')) {
mkdir(CYTRO_RESOURCES.'/template', 0777, true);
} else {
FS::clean(ASPECT_RESOURCES.'/template/');
FS::clean(CYTRO_RESOURCES.'/template/');
}
}
public function tpl($name, $code) {
file_put_contents(ASPECT_RESOURCES.'/template/'.$name, $code);
file_put_contents(CYTRO_RESOURCES.'/template/'.$name, $code);
}
/**
@@ -38,7 +47,7 @@ class TestCase extends \PHPUnit_Framework_TestCase {
* @param bool $dump dump source and result code (for debug)
*/
public function exec($code, $vars, $result, $dump = false) {
$tpl = $this->aspect->compileCode($code, "runtime.tpl");
$tpl = $this->cytro->compileCode($code, "runtime.tpl");
if($dump) {
echo "\n========= DUMP BEGIN ===========\n".$code."\n--- to ---\n".$tpl->getBody()."\n========= DUMP END =============\n";
}
@@ -47,7 +56,7 @@ class TestCase extends \PHPUnit_Framework_TestCase {
public function execTpl($name, $code, $vars, $result, $dump = false) {
$this->tpl($name, $code);
$tpl = $this->aspect->getTemplate($name);
$tpl = $this->cytro->getTemplate($name);
if($dump) {
echo "\n========= DUMP BEGIN ===========\n".$code."\n--- to ---\n".$tpl->getBody()."\n========= DUMP END =============\n";
}
@@ -59,20 +68,49 @@ class TestCase extends \PHPUnit_Framework_TestCase {
* @param string $code source of the template
* @param string $exception exception class
* @param string $message exception message
* @param int $options Aspect's options
* @param int $options Cytro's options
*/
public function execError($code, $exception, $message, $options = 0) {
$opt = $this->aspect->getOptions();
$this->aspect->setOptions($options);
$opt = $this->cytro->getOptions();
$this->cytro->setOptions($options);
try {
$this->aspect->compileCode($code, "inline.tpl");
$this->cytro->compileCode($code, "inline.tpl");
} catch(\Exception $e) {
$this->assertSame($exception, get_class($e), "Exception $code");
$this->assertStringStartsWith($message, $e->getMessage());
$this->aspect->setOptions($opt);
$this->cytro->setOptions($opt);
return;
}
self::$aspect->setOptions($opt);
$this->cytro->setOptions($opt);
$this->fail("Code $code must be invalid");
}
public function assertRender($tpl, $result) {
$template = $this->cytro->compileCode($tpl);
$this->assertSame($result, $template->fetch($this->values));
}
}
class Fake implements \ArrayAccess {
public $vars;
public function offsetExists($offset) {
return true;
}
public function offsetGet($offset) {
if($offset == "object") {
return new self();
} else {
return new self($offset);
}
}
public function offsetSet($offset, $value) {
$this->vars[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->vars[$offset]);
}
}

View File

@@ -4,9 +4,9 @@ require_once __DIR__."/../vendor/autoload.php";
define('ASPECT_RESOURCES', __DIR__."/resources");
define('CYTRO_RESOURCES', __DIR__."/resources");
require_once ASPECT_RESOURCES."/actions.php";
require_once CYTRO_RESOURCES."/actions.php";
require_once __DIR__."/TestCase.php";
function drop() {

View File

@@ -0,0 +1,17 @@
<?php
namespace Cytro;
class CustomProvider extends TestCase {
public function setUp() {
$this->setUp();
$this->cytro->addProvider("my", new FSProvider(CYTRO_RESOURCES.'/provider'));
}
public function testCustom() {
$this->render("start: {include 'my:include.tpl'}", 'start: include template');
$this->render("start: {import 'my:macros.tpl' as ops} {ops.add a=3 b=6}");
}
}

View File

@@ -1,10 +1,11 @@
<?php
namespace Aspect;
use Aspect, Aspect\Modifier, Aspect\TestCase;
namespace Cytro;
use Cytro, Cytro\TestCase;
use Symfony\Component\Process\Exception\LogicException;
class ExtendsTemplateTest extends TestCase {
public static function providerExtends() {
$a = array("one" => 1, "two" => 2, "three" => 3);
public static function providerExtends($items) {
return array(
array("parent.tpl", "Parent. B1: {block b1}{/block}\nB2: {block 'b2'}empty {\$iteration}{/block}", $a,
"Parent. B1: \nB2: empty 0"),
@@ -23,6 +24,10 @@ class ExtendsTemplateTest extends TestCase {
return $data;
}
public function setUp() {
$this->cytro = Cytro::factory(CYTRO_RESOURCES.'/template', CYTRO_RESOURCES.'/compile');
}
/**
* @dataProvider providerExtends
* @param $name
@@ -30,7 +35,7 @@ class ExtendsTemplateTest extends TestCase {
* @param $vars
* @param $result
*/
public function testStaticExtends($name, $code, $vars, $result) {
public function _testStaticExtends($name, $code, $vars, $result) {
static $i = 0;
$vars["iteration"] = $i++;
$this->execTpl($name, $code, $vars, $result);
@@ -43,7 +48,7 @@ class ExtendsTemplateTest extends TestCase {
* @param $vars
* @param $result
*/
public function testDynamicExtends($name, $code, $vars, $result) {
public function _testDynamicExtends($name, $code, $vars, $result) {
static $i = 0;
$vars["iteration"] = $i++;
$this->execTpl($name, $code, $vars, $result, 0);
@@ -54,7 +59,7 @@ class ExtendsTemplateTest extends TestCase {
*/
public function _testParentLevel() {
//echo($this->aspect->getTemplate("parent.tpl")->_body); exit;
$this->assertSame($this->aspect->fetch("parent.tpl", array("a" => "a char")), "Parent template\nBlock1: Block2: Block3: default");
$this->assertSame($this->cytro->fetch("parent.tpl", array("a" => "a char")), "Parent template\nBlock1: Block2: Block3: default");
}
/**
@@ -68,7 +73,7 @@ class ExtendsTemplateTest extends TestCase {
* @group extends
*/
public function _testChildLevel3() {
echo($this->aspect->getTemplate("child3.tpl")->getBody()); exit;
echo($this->cytro->getTemplate("child3.tpl")->getBody()); exit;
}
}

View File

@@ -1,5 +1,5 @@
<?php
namespace Aspect;
namespace Cytro;
class MacrosTest extends TestCase {
@@ -45,30 +45,30 @@ class MacrosTest extends TestCase {
}
public function testMacros() {
$tpl = $this->aspect->compile('math.tpl');
$tpl = $this->cytro->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() {
$tpl = $this->aspect->compile('import.tpl');
$tpl = $this->cytro->compile('import.tpl');
$this->assertSame('Imp: x + y = 3 , x - y - z = 3', Modifier::strip($tpl->fetch(array()), true));
}
public function testImportCustom() {
$tpl = $this->aspect->compile('import_custom.tpl');
$tpl = $this->cytro->compile('import_custom.tpl');
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
}
/**
* @expectedExceptionMessage Undefined macro 'plus'
* @expectedException \Aspect\CompileException
* @expectedException \Cytro\CompileException
*/
public function testImportMiss() {
$tpl = $this->aspect->compile('import_miss.tpl');
$tpl = $this->cytro->compile('import_miss.tpl');
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
}

View File

@@ -1,5 +1,5 @@
<?php
namespace Aspect;
namespace Cytro;
class ModifiersTest extends TestCase {
@@ -32,7 +32,7 @@ class ModifiersTest extends TestCase {
* @param bool $by_word
*/
public function testTruncate($in, $out, $count, $delim = '...', $by_words = false, $middle = false) {
$tpl = $this->aspect->compileCode('{$text|truncate:$count:$delim:$by_words:$middle}');
$tpl = $this->cytro->compileCode('{$text|truncate:$count:$delim:$by_words:$middle}');
$this->assertEquals($out, $tpl->fetch(array(
"text" => $in,
"count" => $count,

View File

@@ -1,8 +1,8 @@
<?php
namespace Aspect;
use Aspect;
namespace Cytro;
use Cytro;
class FSProviderTest extends \Aspect\TestCase {
class FSProviderTest extends \Cytro\TestCase {
/**
* @var FSProvider
*/
@@ -12,7 +12,7 @@ class FSProviderTest extends \Aspect\TestCase {
parent::setUp();
$this->tpl("template1.tpl", 'Template 1 {$a}');
$this->tpl("template2.tpl", 'Template 2 {$a}');
$this->provider = new FSProvider(ASPECT_RESOURCES.'/template');
$this->provider = new FSProvider(CYTRO_RESOURCES.'/template');
}
public function testIsTemplateExists() {
@@ -23,8 +23,8 @@ class FSProviderTest extends \Aspect\TestCase {
public function testGetSource() {
$src = $this->provider->getSource("template1.tpl", $time);
clearstatcache();
$this->assertEquals(file_get_contents(ASPECT_RESOURCES.'/template/template1.tpl'), $src);
$this->assertEquals(filemtime(ASPECT_RESOURCES.'/template/template1.tpl'), $time);
$this->assertEquals(file_get_contents(CYTRO_RESOURCES.'/template/template1.tpl'), $src);
$this->assertEquals(filemtime(CYTRO_RESOURCES.'/template/template1.tpl'), $time);
}
/**
@@ -37,7 +37,7 @@ class FSProviderTest extends \Aspect\TestCase {
public function testGetLastModified() {
$time = $this->provider->getLastModified("template1.tpl");
clearstatcache();
$this->assertEquals(filemtime(ASPECT_RESOURCES.'/template/template1.tpl'), $time);
$this->assertEquals(filemtime(CYTRO_RESOURCES.'/template/template1.tpl'), $time);
}
/**
@@ -52,7 +52,7 @@ class FSProviderTest extends \Aspect\TestCase {
$this->assertSame($tpls, array_keys($times));
clearstatcache();
foreach($times as $template => $time) {
$this->assertEquals(filemtime(ASPECT_RESOURCES."/template/$template"), $time);
$this->assertEquals(filemtime(CYTRO_RESOURCES."/template/$template"), $time);
}
}

View File

@@ -1,7 +1,7 @@
<?php
namespace Aspect;
use Aspect,
Aspect\Render;
namespace Cytro;
use Cytro,
Cytro\Render;
class RenderTest extends \PHPUnit_Framework_TestCase {
@@ -11,7 +11,7 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
public static $render;
public static function setUpBeforeClass() {
self::$render = new Render(Aspect::factory("."), function ($tpl) {
self::$render = new Render(Cytro::factory("."), function ($tpl) {
echo "It is render's function ".$tpl["render"];
}, array(
"name" => "render.tpl"
@@ -19,7 +19,7 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
}
public function testCreate() {
$r = new Render(Aspect::factory("."), function () {
$r = new Render(Cytro::factory("."), function () {
echo "Test render";
}, array(
"name" => "test.render.tpl"
@@ -43,7 +43,7 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage template error
*/
public function testFetchException() {
$render = new Render(Aspect::factory("."), function () {
$render = new Render(Cytro::factory("."), function () {
echo "error";
throw new \RuntimeException("template error");
}, array(

View File

@@ -1,17 +1,17 @@
<?php
namespace Aspect;
namespace Cytro;
class ScopeTest extends TestCase {
public function openTag($tokenizer, $scope) {
$this->assertInstanceOf('Aspect\Tokenizer', $tokenizer);
$this->assertInstanceOf('Aspect\Scope', $scope);
$this->assertInstanceOf('Cytro\Tokenizer', $tokenizer);
$this->assertInstanceOf('Cytro\Scope', $scope);
$scope["value"] = true;
return "open-tag";
}
public function closeTag($tokenizer, $scope) {
$this->assertInstanceOf('Aspect\Tokenizer', $tokenizer);
$this->assertInstanceOf('Aspect\Scope', $scope);
$this->assertInstanceOf('Cytro\Tokenizer', $tokenizer);
$this->assertInstanceOf('Cytro\Scope', $scope);
$this->assertTrue($scope["value"]);
return "close-tag";
}

View File

@@ -1,14 +1,14 @@
<?php
namespace Aspect;
use Aspect\Template,
Aspect,
Aspect\Render;
namespace Cytro;
use Cytro\Template,
Cytro,
Cytro\Render;
class TemplateTest extends TestCase {
public function setUp() {
parent::setUp();
$this->aspect->addTemplate(new Render($this->aspect, function ($tpl) {
$this->cytro->addTemplate(new Render($this->cytro, function ($tpl) {
echo "<b>Welcome, ".$tpl["username"]." (".$tpl["email"].")</b>";
}, array(
"name" => "welcome.tpl"
@@ -111,14 +111,14 @@ class TemplateTest extends TestCase {
public static function providerVarsInvalid() {
return array(
array('hello, {$a.}!', 'Aspect\CompileException', "Unexpected end of expression"),
array('hello, {$b[c}!', 'Aspect\CompileException', "Unexpected end of expression"),
array('hello, {$b.c]}!', 'Aspect\CompileException', "Unexpected token ']'"),
array('hello, {$b[ ]}!', 'Aspect\CompileException', "Unexpected token ']'"),
array('hello, {$b[9/].c}!', 'Aspect\CompileException', "Unexpected token ']'"),
array('hello, {$b[3]$c}!', 'Aspect\CompileException', "Unexpected token '\$c'"),
array('hello, {$b[3]c}!', 'Aspect\CompileException', "Unexpected token 'c'"),
array('hello, {$b.obj->valid()}!', 'Aspect\SecurityException', "Forbidden to call methods", Aspect::DENY_METHODS),
array('hello, {$a.}!', 'Cytro\CompileException', "Unexpected end of expression"),
array('hello, {$b[c}!', 'Cytro\CompileException', "Unexpected end of expression"),
array('hello, {$b.c]}!', 'Cytro\CompileException', "Unexpected token ']'"),
array('hello, {$b[ ]}!', 'Cytro\CompileException', "Unexpected token ']'"),
array('hello, {$b[9/].c}!', 'Cytro\CompileException', "Unexpected token ']'"),
array('hello, {$b[3]$c}!', 'Cytro\CompileException', "Unexpected token '\$c'"),
array('hello, {$b[3]c}!', 'Cytro\CompileException', "Unexpected token 'c'"),
array('hello, {$b.obj->valid()}!', 'Cytro\SecurityException', "Forbidden to call methods", Cytro::DENY_METHODS),
);
}
@@ -173,12 +173,12 @@ class TemplateTest extends TestCase {
public static function providerModifiersInvalid() {
return array(
array('Mod: {$lorem|}!', 'Aspect\CompileException', "Unexpected end of expression"),
array('Mod: {$lorem|str_rot13}!', 'Aspect\CompileException', "Modifier str_rot13 not found", Aspect::DENY_INLINE_FUNCS),
array('Mod: {$lorem|my_encode}!', 'Aspect\CompileException', "Modifier my_encode not found"),
array('Mod: {$lorem|truncate:}!', 'Aspect\CompileException', "Unexpected end of expression"),
array('Mod: {$lorem|truncate:abs}!', 'Aspect\CompileException', "Unexpected token 'abs'"),
array('Mod: {$lorem|truncate:80|}!', 'Aspect\CompileException', "Unexpected end of expression"),
array('Mod: {$lorem|}!', 'Cytro\CompileException', "Unexpected end of expression"),
array('Mod: {$lorem|str_rot13}!', 'Cytro\CompileException', "Modifier str_rot13 not found", Cytro::DENY_INLINE_FUNCS),
array('Mod: {$lorem|my_encode}!', 'Cytro\CompileException', "Modifier my_encode not found"),
array('Mod: {$lorem|truncate:}!', 'Cytro\CompileException', "Unexpected end of expression"),
array('Mod: {$lorem|truncate:abs}!', 'Cytro\CompileException', "Unexpected token 'abs'"),
array('Mod: {$lorem|truncate:80|}!', 'Cytro\CompileException', "Unexpected end of expression"),
);
}
@@ -220,15 +220,15 @@ class TemplateTest extends TestCase {
public static function providerExpressionsInvalid() {
return array(
array('If: {-"hi"} end', 'Aspect\CompileException', "Unexpected token '-'"),
array('If: {($a++)++} end', 'Aspect\CompileException', "Unexpected token '++'"),
array('If: {$a + * $c} end', 'Aspect\CompileException', "Unexpected token '*'"),
array('If: {/$a} end', 'Aspect\CompileException', "Unexpected token '\$a'"),
array('If: {$a == 5 > 4} end', 'Aspect\CompileException', "Unexpected token '>'"),
array('If: {$a != 5 <= 4} end', 'Aspect\CompileException', "Unexpected token '<='"),
array('If: {$a != 5 => 4} end', 'Aspect\CompileException', "Unexpected token '=>'"),
array('If: {$a + (*6)} end', 'Aspect\CompileException', "Unexpected token '*'"),
array('If: {$a + ( 6} end', 'Aspect\CompileException', "Brackets don't match"),
array('If: {-"hi"} end', 'Cytro\CompileException', "Unexpected token '-'"),
array('If: {($a++)++} end', 'Cytro\CompileException', "Unexpected token '++'"),
array('If: {$a + * $c} end', 'Cytro\CompileException', "Unexpected token '*'"),
array('If: {/$a} end', 'Cytro\CompileException', "Unexpected token '\$a'"),
array('If: {$a == 5 > 4} end', 'Cytro\CompileException', "Unexpected token '>'"),
array('If: {$a != 5 <= 4} end', 'Cytro\CompileException', "Unexpected token '<='"),
array('If: {$a != 5 => 4} end', 'Cytro\CompileException', "Unexpected token '=>'"),
array('If: {$a + (*6)} end', 'Cytro\CompileException', "Unexpected token '*'"),
array('If: {$a + ( 6} end', 'Cytro\CompileException', "Brackets don't match"),
);
}
@@ -265,8 +265,8 @@ class TemplateTest extends TestCase {
public static function providerIncludeInvalid() {
return array(
array('Include {include} template', 'Aspect\CompileException', "Unexpected end of expression"),
array('Include {include another="welcome.tpl"} template', 'Aspect\CompileException', "Unexpected token '='"),
array('Include {include} template', 'Cytro\CompileException', "Unexpected end of expression"),
array('Include {include another="welcome.tpl"} template', 'Cytro\CompileException', "Unexpected token '='"),
);
}
@@ -306,10 +306,10 @@ class TemplateTest extends TestCase {
public static function providerIfInvalid() {
return array(
array('If: {if} block1 {/if} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('If: {if 1} block1 {elseif} block2 {/if} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('If: {if 1} block1 {else} block2 {elseif 0} block3 {/if} end', 'Aspect\CompileException', "Incorrect use of the tag {elseif}"),
array('If: {if 1} block1 {else} block2 {/if} block3 {elseif 0} end', 'Aspect\CompileException', "Unexpected tag 'elseif' (this tag can be used with 'if')"),
array('If: {if} block1 {/if} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('If: {if 1} block1 {elseif} block2 {/if} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('If: {if 1} block1 {else} block2 {elseif 0} block3 {/if} end', 'Cytro\CompileException', "Incorrect use of the tag {elseif}"),
array('If: {if 1} block1 {else} block2 {/if} block3 {elseif 0} end', 'Cytro\CompileException', "Unexpected tag 'elseif' (this tag can be used with 'if')"),
);
}
@@ -347,20 +347,20 @@ class TemplateTest extends TestCase {
public static function providerCreateVarInvalid() {
return array(
array('Create: {var $v} Result: {$v} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('Create: {var $v = } Result: {$v} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('Create: {var $v = 1++} Result: {$v} end', 'Aspect\CompileException', "Unexpected token '++'"),
array('Create: {var $v = c} Result: {$v} end', 'Aspect\CompileException', "Unexpected token 'c'"),
array('Create: {var $v = ($a)++} Result: {$v} end', 'Aspect\CompileException', "Unexpected token '++'"),
array('Create: {var $v = --$a++} Result: {$v} end', 'Aspect\CompileException', "Unexpected token '++'"),
array('Create: {var $v = $a|upper++} Result: {$v} end', 'Aspect\CompileException', "Unexpected token '++'"),
array('Create: {var $v = max($a,2)++} Result: {$v} end', 'Aspect\CompileException', "Unexpected token '++'"),
array('Create: {var $v = max($a,2)} Result: {$v} end', 'Aspect\CompileException', "Modifier max not found", Aspect::DENY_INLINE_FUNCS),
array('Create: {var $v = 4*} Result: {$v} end', 'Aspect\CompileException', "Unexpected token '*'"),
array('Create: {var $v = ""$a} Result: {$v} end', 'Aspect\CompileException', "Unexpected token '\$a'"),
array('Create: {var $v = [1,2} Result: {$v} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('Create: {var $v = empty(2)} Result: {$v} end', 'Aspect\CompileException', "Unexpected token 2, isset() and empty() accept only variables"),
array('Create: {var $v = isset(2)} Result: {$v} end', 'Aspect\CompileException', "Unexpected token 2, isset() and empty() accept only variables"),
array('Create: {var $v} Result: {$v} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('Create: {var $v = } Result: {$v} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('Create: {var $v = 1++} Result: {$v} end', 'Cytro\CompileException', "Unexpected token '++'"),
array('Create: {var $v = c} Result: {$v} end', 'Cytro\CompileException', "Unexpected token 'c'"),
array('Create: {var $v = ($a)++} Result: {$v} end', 'Cytro\CompileException', "Unexpected token '++'"),
array('Create: {var $v = --$a++} Result: {$v} end', 'Cytro\CompileException', "Unexpected token '++'"),
array('Create: {var $v = $a|upper++} Result: {$v} end', 'Cytro\CompileException', "Unexpected token '++'"),
array('Create: {var $v = max($a,2)++} Result: {$v} end', 'Cytro\CompileException', "Unexpected token '++'"),
array('Create: {var $v = max($a,2)} Result: {$v} end', 'Cytro\CompileException', "Modifier max not found", Cytro::DENY_INLINE_FUNCS),
array('Create: {var $v = 4*} Result: {$v} end', 'Cytro\CompileException', "Unexpected token '*'"),
array('Create: {var $v = ""$a} Result: {$v} end', 'Cytro\CompileException', "Unexpected token '\$a'"),
array('Create: {var $v = [1,2} Result: {$v} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('Create: {var $v = empty(2)} Result: {$v} end', 'Cytro\CompileException', "Unexpected token 2, isset() and empty() accept only variables"),
array('Create: {var $v = isset(2)} Result: {$v} end', 'Cytro\CompileException', "Unexpected token 2, isset() and empty() accept only variables"),
);
}
@@ -453,27 +453,27 @@ class TemplateTest extends TestCase {
public static function providerForeachInvalid() {
return array(
array('Foreach: {foreach} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected end of tag {foreach}"),
array('Foreach: {foreach $list} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('Foreach: {foreach $list+1 as $e} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach array_random() as $e} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token 'array_random'"),
array('Foreach: {foreach $list as $e+1} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $k+1 => $e} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as max($i,1) => $e} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as max($e,1)} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list => $e} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '=>'"),
array('Foreach: {foreach $list $k => $e} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '\$k'"),
array('Foreach: {foreach $list as $k =>} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('Foreach: {foreach last=$l $list as $e } {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token 'last' in tag {foreach}"),
array('Foreach: {foreach $list as $e unknown=1} {$e}, {/foreach} end', 'Aspect\CompileException', "Unknown parameter 'unknown'"),
array('Foreach: {foreach $list as $e index=$i+1} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e first=$f+1} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e last=$l+1} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e index=max($i,1)} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e first=max($i,1)} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e last=max($i,1)} {$e}, {/foreach} end', 'Aspect\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {break} {/foreach} end', 'Aspect\CompileException', "Improper usage of the tag {break}"),
array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {continue} {/foreach} end', 'Aspect\CompileException', "Improper usage of the tag {continue}"),
array('Foreach: {foreach} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected end of tag {foreach}"),
array('Foreach: {foreach $list} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('Foreach: {foreach $list+1 as $e} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach array_random() as $e} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token 'array_random'"),
array('Foreach: {foreach $list as $e+1} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $k+1 => $e} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as max($i,1) => $e} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as max($e,1)} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list => $e} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '=>'"),
array('Foreach: {foreach $list $k => $e} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '\$k'"),
array('Foreach: {foreach $list as $k =>} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('Foreach: {foreach last=$l $list as $e } {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token 'last' in tag {foreach}"),
array('Foreach: {foreach $list as $e unknown=1} {$e}, {/foreach} end', 'Cytro\CompileException', "Unknown parameter 'unknown'"),
array('Foreach: {foreach $list as $e index=$i+1} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e first=$f+1} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e last=$l+1} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e index=max($i,1)} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e first=max($i,1)} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e last=max($i,1)} {$e}, {/foreach} end', 'Cytro\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {break} {/foreach} end', 'Cytro\CompileException', "Improper usage of the tag {break}"),
array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {continue} {/foreach} end', 'Cytro\CompileException', "Improper usage of the tag {continue}"),
);
}
@@ -518,9 +518,9 @@ class TemplateTest extends TestCase {
public static function providerSwitchInvalid() {
return array(
array('Switch: {switch}{case 1} one {break}{/switch} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('Switch: {switch 1}{case} one {break}{/switch} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('Switch: {switch 1}{break}{case} one {/switch} end', 'Aspect\CompileException', "Improper usage of the tag {break}"),
array('Switch: {switch}{case 1} one {break}{/switch} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('Switch: {switch 1}{case} one {break}{/switch} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('Switch: {switch 1}{break}{case} one {/switch} end', 'Cytro\CompileException', "Improper usage of the tag {break}"),
);
}
@@ -536,7 +536,7 @@ class TemplateTest extends TestCase {
public static function providerWhileInvalid() {
return array(
array('While: {while} block {/while} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('While: {while} block {/while} end', 'Cytro\CompileException', "Unexpected end of expression"),
);
}
@@ -564,29 +564,29 @@ class TemplateTest extends TestCase {
public static function providerForInvalid() {
return array(
array('For: {for} block1 {/for} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('For: {for $a=} block1 {/for} end', 'Aspect\CompileException', "Unexpected end of expression"),
array('For: {for $a+1=3 to=6} block1 {/for} end', 'Aspect\CompileException', "Unexpected token '+'"),
array('For: {for max($a,$b)=3 to=6} block1 {/for} end', 'Aspect\CompileException', "Unexpected token 'max'"),
array('For: {for to=6 $a=3} block1 {/for} end', 'Aspect\CompileException', "Unexpected token 'to'"),
array('For: {for index=$i $a=3 to=6} block1 {/for} end', 'Aspect\CompileException', "Unexpected token 'index'"),
array('For: {for first=$i $a=3 to=6} block1 {/for} end', 'Aspect\CompileException', "Unexpected token 'first'"),
array('For: {for last=$i $a=3 to=6} block1 {/for} end', 'Aspect\CompileException', "Unexpected token 'last'"),
array('For: {for $a=4 to=6 unk=4} block1 {/for} end', 'Aspect\CompileException', "Unknown parameter 'unk'"),
array('For: {for $a=4 to=6} $a: {$a}, {forelse} {break} {/for} end', 'Aspect\CompileException', "Improper usage of the tag {break}"),
array('For: {for $a=4 to=6} $a: {$a}, {forelse} {continue} {/for} end', 'Aspect\CompileException', "Improper usage of the tag {continue}"),
array('For: {for} block1 {/for} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('For: {for $a=} block1 {/for} end', 'Cytro\CompileException', "Unexpected end of expression"),
array('For: {for $a+1=3 to=6} block1 {/for} end', 'Cytro\CompileException', "Unexpected token '+'"),
array('For: {for max($a,$b)=3 to=6} block1 {/for} end', 'Cytro\CompileException', "Unexpected token 'max'"),
array('For: {for to=6 $a=3} block1 {/for} end', 'Cytro\CompileException', "Unexpected token 'to'"),
array('For: {for index=$i $a=3 to=6} block1 {/for} end', 'Cytro\CompileException', "Unexpected token 'index'"),
array('For: {for first=$i $a=3 to=6} block1 {/for} end', 'Cytro\CompileException', "Unexpected token 'first'"),
array('For: {for last=$i $a=3 to=6} block1 {/for} end', 'Cytro\CompileException', "Unexpected token 'last'"),
array('For: {for $a=4 to=6 unk=4} block1 {/for} end', 'Cytro\CompileException', "Unknown parameter 'unk'"),
array('For: {for $a=4 to=6} $a: {$a}, {forelse} {break} {/for} end', 'Cytro\CompileException', "Improper usage of the tag {break}"),
array('For: {for $a=4 to=6} $a: {$a}, {forelse} {continue} {/for} end', 'Cytro\CompileException', "Improper usage of the tag {continue}"),
);
}
public static function providerLayersInvalid() {
return array(
array('Layers: {foreach $list as $e} block1 {if 1} {foreachelse} {/if} {/foreach} end', 'Aspect\CompileException', "Unexpected tag 'foreachelse' (this tag can be used with 'foreach')"),
array('Layers: {foreach $list as $e} block1 {if 1} {/foreach} {/if} end', 'Aspect\CompileException', "Unexpected closing of the tag 'foreach'"),
array('Layers: {for $a=4 to=6} block1 {if 1} {forelse} {/if} {/for} end', 'Aspect\CompileException', "Unexpected tag 'forelse' (this tag can be used with 'for')"),
array('Layers: {for $a=4 to=6} block1 {if 1} {/for} {/if} end', 'Aspect\CompileException', "Unexpected closing of the tag 'for'"),
array('Layers: {switch 1} {if 1} {case 1} {/if} {/switch} end', 'Aspect\CompileException', "Unexpected tag 'case' (this tag can be used with 'switch')"),
array('Layers: {/switch} end', 'Aspect\CompileException', "Unexpected closing of the tag 'switch'"),
array('Layers: {if 1} end', 'Aspect\CompileException', "Unclosed tag(s): {if}"),
array('Layers: {foreach $list as $e} block1 {if 1} {foreachelse} {/if} {/foreach} end', 'Cytro\CompileException', "Unexpected tag 'foreachelse' (this tag can be used with 'foreach')"),
array('Layers: {foreach $list as $e} block1 {if 1} {/foreach} {/if} end', 'Cytro\CompileException', "Unexpected closing of the tag 'foreach'"),
array('Layers: {for $a=4 to=6} block1 {if 1} {forelse} {/if} {/for} end', 'Cytro\CompileException', "Unexpected tag 'forelse' (this tag can be used with 'for')"),
array('Layers: {for $a=4 to=6} block1 {if 1} {/for} {/if} end', 'Cytro\CompileException', "Unexpected closing of the tag 'for'"),
array('Layers: {switch 1} {if 1} {case 1} {/if} {/switch} end', 'Cytro\CompileException', "Unexpected tag 'case' (this tag can be used with 'switch')"),
array('Layers: {/switch} end', 'Cytro\CompileException', "Unexpected closing of the tag 'switch'"),
array('Layers: {if 1} end', 'Cytro\CompileException', "Unclosed tag(s): {if}"),
);
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Aspect;
use Aspect\Tokenizer;
namespace Cytro;
use Cytro\Tokenizer;
class TokenizerTest extends \PHPUnit_Framework_TestCase {
@@ -54,7 +54,7 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase {
try {
$tokens->skip(T_STRING)->skip('(')->skip(':');
} catch(\Exception $e) {
$this->assertInstanceOf('Aspect\UnexpectedTokenException', $e);
$this->assertInstanceOf('Cytro\UnexpectedTokenException', $e);
$this->assertStringStartsWith("Unexpected token '3' in expression, expect ':'", $e->getMessage());
}
$this->assertTrue($tokens->valid());

View File

@@ -1,22 +1,22 @@
<?php
use Aspect\Render,
Aspect\FSProvider as FS;
use Cytro\Render,
Cytro\FSProvider as FS;
class AspectTest extends \Aspect\TestCase {
class CytroTest extends \Cytro\TestCase {
public function testAddRender() {
$test = $this;
$this->aspect->addTemplate(new Render($this->aspect, function($tpl) use ($test) {
$this->cytro->addTemplate(new Render($this->cytro, function($tpl) use ($test) {
/** @var \PHPUnit_Framework_TestCase $test */
$test->assertInstanceOf('Aspect\Render', $tpl);
$test->assertInstanceOf('Cytro\Render', $tpl);
echo "Inline render";
}, array(
"name" => 'render.tpl',
"scm" => false
)));
$this->assertSame("Inline render", $this->aspect->fetch('render.tpl', array()));
$this->assertSame("Inline render", $this->cytro->fetch('render.tpl', array()));
}
public function testCompileFile() {
@@ -26,71 +26,71 @@ class AspectTest extends \Aspect\TestCase {
);
$this->tpl('template1.tpl', 'Template 1 a');
$this->tpl('template2.tpl', 'Template 2 b');
$this->assertSame("Template 1 a", $this->aspect->fetch('template1.tpl', $a));
$this->assertSame("Template 2 b", $this->aspect->fetch('template2.tpl', $a));
$this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template1.tpl'));
$this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template2.tpl'));
$this->assertSame(3, iterator_count(new FilesystemIterator(ASPECT_RESOURCES.'/compile')));
$this->assertSame("Template 1 a", $this->cytro->fetch('template1.tpl', $a));
$this->assertSame("Template 2 b", $this->cytro->fetch('template2.tpl', $a));
$this->assertInstanceOf('Cytro\Render', $this->cytro->getTemplate('template1.tpl'));
$this->assertInstanceOf('Cytro\Render', $this->cytro->getTemplate('template2.tpl'));
$this->assertSame(3, iterator_count(new FilesystemIterator(CYTRO_RESOURCES.'/compile')));
}
public function testStorage() {
$this->tpl('custom.tpl', 'Custom template');
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom template", $this->cytro->fetch('custom.tpl', array()));
//$this->aspect->clearCompiledTemplate('custom.tpl', false);
//$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom template 2');
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom template", $this->cytro->fetch('custom.tpl', array()));
}
public function testCheckMTime() {
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
$this->cytro->setOptions(Cytro::FORCE_COMPILE);
$this->tpl('custom.tpl', 'Custom template');
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom template", $this->cytro->fetch('custom.tpl', array()));
sleep(1);
$this->tpl('custom.tpl', 'Custom template (new)');
$this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom template (new)", $this->cytro->fetch('custom.tpl', array()));
}
public function testForceCompile() {
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
$this->cytro->setOptions(Cytro::FORCE_COMPILE);
$this->tpl('custom.tpl', 'Custom template');
$this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom template", $this->cytro->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom template (new)');
$this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom template (new)", $this->cytro->fetch('custom.tpl', array()));
}
public function testSetModifier() {
$this->aspect->addModifier("mymod", "myMod");
$this->cytro->addModifier("mymod", "myMod");
$this->tpl('custom.tpl', 'Custom modifier {$a|mymod}');
$this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->aspect->fetch('custom.tpl', array("a" => "Custom")));
$this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->cytro->fetch('custom.tpl', array("a" => "Custom")));
}
/**
* @group add_functions
*/
public function testSetFunctions() {
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
$this->aspect->addFunction("myfunc", "myFunc");
$this->aspect->addBlockFunction("myblockfunc", "myBlockFunc");
$this->cytro->setOptions(Cytro::FORCE_COMPILE);
$this->cytro->addFunction("myfunc", "myFunc");
$this->cytro->addBlockFunction("myblockfunc", "myBlockFunc");
$this->tpl('custom.tpl', 'Custom function {myfunc name="foo"}');
$this->assertSame("Custom function MyFunc:foo", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom function MyFunc:foo", $this->cytro->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}');
$this->assertSame("Custom function Block:foo:this block1:Block", $this->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom function Block:foo:this block1:Block", $this->cytro->fetch('custom.tpl', array()));
}
public function testSetCompilers() {
$this->aspect->setOptions(Aspect::FORCE_COMPILE);
$this->aspect->addCompiler("mycompiler", 'myCompiler');
$this->aspect->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
$this->cytro->setOptions(Cytro::FORCE_COMPILE);
$this->cytro->addCompiler("mycompiler", 'myCompiler');
$this->cytro->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->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar)", $this->cytro->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->aspect->fetch('custom.tpl', array()));
$this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar) block1 Tag baz of compiler block2 End of compiler", $this->cytro->fetch('custom.tpl', array()));
}
}

View File

@@ -12,20 +12,20 @@ function myBlockFunc($params, $content) {
return "Block:".$params["name"].':'.trim($content).':Block';
}
function myCompiler(Aspect\Tokenizer $tokenizer, Aspect\Template $tpl) {
function myCompiler(Cytro\Tokenizer $tokenizer, Cytro\Template $tpl) {
$p = $tpl->parseParams($tokenizer);
return 'echo "PHP_VERSION: ".PHP_VERSION." (for ".'.$p["name"].'.")";';
}
function myBlockCompilerOpen(Aspect\Tokenizer $tokenizer, Aspect\Scope $scope) {
function myBlockCompilerOpen(Cytro\Tokenizer $tokenizer, Cytro\Scope $scope) {
return myCompiler($tokenizer, $scope->tpl);
}
function myBlockCompilerClose(Aspect\Tokenizer $tokenizer, Aspect\Scope $scope) {
function myBlockCompilerClose(Cytro\Tokenizer $tokenizer, Cytro\Scope $scope) {
return 'echo "End of compiler";';
}
function myBlockCompilerTag(Aspect\Tokenizer $tokenizer, Aspect\Scope $scope) {
function myBlockCompilerTag(Cytro\Tokenizer $tokenizer, Cytro\Scope $scope) {
$p = $scope->tpl->parseParams($tokenizer);
return 'echo "Tag ".'.$p["name"].'." of compiler";';
}

View File

@@ -0,0 +1,7 @@
{block 'header'}
header block
{/block}
{block 'bottom'}
bottom block
{/block}

View File

@@ -0,0 +1,6 @@
{extends 'parent.tpl'}
up
{block 'header'}empty header{/block}
middle
{block 'bottom'}empty bottom{/block}
down

View File

@@ -0,0 +1 @@
include template

View File

@@ -0,0 +1,7 @@
{macro add(a, b)}
a + b = {$a + $b}
{/macro}
{macro rem(a, b)}
a - b = {$a - $b}
{/macro}

View File

@@ -0,0 +1,2 @@
{block 'header'}header body{/block}
{block 'bottom'}bottom body{/block}