More test, reformat code to PSR-0

This commit is contained in:
Ivan Shalganov 2014-02-27 16:30:44 +04:00
parent e00d6c7e50
commit c210303b72
18 changed files with 229 additions and 276 deletions

View File

@ -30,7 +30,7 @@ For example,
It is new or current item
{case 'current'}
It is current item
{case 'new', $.const.NEW_STATUS}
{case 'new', 'newer'}
It is new item, again
{default}
I don't know the type {$type}

View File

@ -8,6 +8,7 @@
* file that was distributed with this source code.
*/
namespace Fenom;
use Fenom\Error\InvalidUsageException;
use Fenom\Error\UnexpectedTokenException;
use Fenom\Tokenizer;

View File

@ -9,6 +9,7 @@
*/
namespace Fenom\Error;
use Fenom\Tokenizer;
/**

View File

@ -8,6 +8,7 @@
* file that was distributed with this source code.
*/
namespace Fenom;
use Fenom;
/**

View File

@ -8,6 +8,7 @@
* file that was distributed with this source code.
*/
namespace Fenom;
use Fenom;
use Fenom\Error\UnexpectedTokenException;
use Fenom\Error\CompileException;
@ -678,7 +679,7 @@ class Template extends Render
if ($tags = $this->_fenom->getTagOwners($action)) { // unknown template tag
throw new TokenizeException("Unexpected tag '$action' (this tag can be used with '" . implode("', '", $tags) . "')");
} else {
throw new TokenizeException("Unexpected tag $action");
throw new TokenizeException("Unexpected tag '$action'");
}
}
@ -952,14 +953,14 @@ class Template extends Render
$tokens->need('.')->next();
$var = $this->parseName($tokens);
if (!defined($var)) {
$var = 'constant(' . var_export($var, true) . ')';
$var = '@constant(' . var_export($var, true) . ')';
}
break;
case 'version':
$var = '\Fenom::VERSION';
break;
default:
throw new UnexpectedTokenException($tokens);
throw new UnexpectedTokenException($tokens->back());
}
return $var;
@ -984,7 +985,7 @@ class Template extends Render
if ($is_var) {
return '(empty(' . $var . ') ? (' . $this->parseExpr($tokens) . ') : ' . $var . ')';
} else {
return '(' . $var . ' ?: (' . $this->parseExpr($tokens) . ')';
return '(' . $var . ' ?: (' . $this->parseExpr($tokens) . '))';
}
} else {
if ($is_var) {
@ -1169,7 +1170,7 @@ class Template extends Render
break;
case '$':
$tokens->next()->need('.')->next()->need(T_CONST)->next();
return 'constant('.$this->parseName($tokens).')';
return @constant($this->parseName($tokens));
default:
throw new UnexpectedTokenException($tokens);
}

View File

@ -594,6 +594,8 @@ class Tokenizer
public function end()
{
$this->p = $this->_max;
unset($this->prev, $this->curr, $this->next);
return $this;
}
/**

View File

@ -1,5 +1,6 @@
<?php
namespace Fenom;
use Fenom, Fenom\Provider as FS;
class TestCase extends \PHPUnit_Framework_TestCase
@ -124,7 +125,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
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");
$this->assertSame(Modifier::strip($result, true), Modifier::strip($tpl->fetch($vars), true), "Test $code");
return $tpl;
}
@ -276,20 +277,24 @@ class TestCase extends \PHPUnit_Framework_TestCase
}
}
class Helper {
class Helper
{
public $word = 'helper';
public function __construct($word) {
public function __construct($word)
{
$this->word = $word;
$this->self = $this;
}
public function chunk() {
public function chunk()
{
return $this;
}
public function __toString() {
public function __toString()
{
return $this->word;
}
}

View File

@ -1,157 +0,0 @@
<?php
namespace Fenom;
use Fenom, Fenom\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;
}
/**
* Templates skeletons
* @param array $vars
* @return array
*/
public static function templates(array $vars)
{
return array(
array(
"name" => "level.0.tpl",
"level" => 0,
"blocks" => array(
"b1" => "default {\$default}",
"b2" => "empty 0"
),
"result" => array(
"b1" => "default " . $vars["default"],
"b2" => "empty 0"
),
),
array(
"name" => "level.1.tpl",
"level" => 1,
"use" => false,
"blocks" => array(
"b1" => "from level 1"
),
"result" => array(
"b1" => "from level 1",
"b2" => "empty 0"
),
),
array(
"name" => "level.2.tpl",
"level" => 2,
"use" => false,
"blocks" => array(
"b2" => "from level 2",
"b4" => "unused block"
),
"result" => array(
"b1" => "from level 1",
"b2" => "from level 2"
),
),
array(
"name" => "level.3.tpl",
"level" => 3,
"use" => false,
"blocks" => array(
"b1" => "from level 3",
"b2" => "also from level 3"
),
"result" => array(
"b1" => "from level 3",
"b2" => "also from level 3"
),
)
);
}
/**
* 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 ($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 ';
}
if ($level) {
$src = sprintf($extend_mask, $level - 1) . ' ' . $src;
}
$t[$tpl["name"]] = array("src" => $src, "dst" => $dst);
}
return $t;
}
public function _testTemplateExtends()
{
$vars = array(
"b1" => "b1",
"b2" => "b2",
"b3" => "b3",
"b4" => "b4",
"level" => "level",
"default" => 5
);
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
foreach ($tpls as $name => $tpl) {
$this->tpl($name, $tpl["src"]);
$this->assertSame($this->fenom->fetch($name, $vars), $tpl["dst"]);
}
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"]);
}
$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"]);
}
}
/**
* @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()
{
}
}

View File

@ -1,5 +1,6 @@
<?php
namespace Fenom;
use Fenom, Fenom\TestCase;
class ExtendsTest extends TestCase
@ -34,7 +35,8 @@ Content of the footer";
/**
* @group testAutoExtends
*/
public function testAutoExtends() {
public function testAutoExtends()
{
$result = "Before header
Child 2 header
Before body
@ -49,7 +51,8 @@ Footer from use";
), array()));
}
public function testStaticExtendLevel1() {
public function testStaticExtendLevel1()
{
$result = "Before header
Content of the header
Before body
@ -59,7 +62,8 @@ Content of the footer";
$this->assertSame($result, $this->fenom->fetch('extends/static/child.1.tpl', array()));
}
public function testStaticExtendLevel3() {
public function testStaticExtendLevel3()
{
$result = "Before header
Child 2 header
Before body
@ -69,7 +73,8 @@ Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/static/child.3.tpl', array()));
}
public function testAutoAndStaticExtend() {
public function testAutoAndStaticExtend()
{
$result = "Before header
Child 2 header
Before body
@ -83,7 +88,8 @@ Footer from use";
), array()));
}
public function testStaticExtendNested() {
public function testStaticExtendNested()
{
$result = "Before body
Before header
@ -94,7 +100,8 @@ Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/static/nested/child.1.tpl', array()));
}
public function testDynamicExtendLevel2() {
public function testDynamicExtendLevel2()
{
$result = "Before header
Child 2 header
Before body
@ -104,7 +111,8 @@ Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/dynamic/child.2.tpl', array()));
}
public function testDynamicExtendLevel3() {
public function testDynamicExtendLevel3()
{
$result = "Before header
Child 2 header
Before body
@ -114,7 +122,8 @@ Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/dynamic/child.3.tpl', array()));
}
public function testDynamicExtendLevel4() {
public function testDynamicExtendLevel4()
{
$result = "Before header
Child 2 header
Before body

View File

@ -1,5 +1,6 @@
<?php
namespace Fenom;
use Fenom;
use Fenom\TestCase;
@ -103,7 +104,8 @@ class ProviderTest extends TestCase
), $list);
}
public function testRm() {
public function testRm()
{
$this->assertTrue(is_dir(FENOM_RESOURCES . '/template/sub'));
Provider::rm(FENOM_RESOURCES . '/template/sub');
$this->assertFalse(is_dir(FENOM_RESOURCES . '/template/sub'));

View File

@ -1,5 +1,6 @@
<?php
namespace Fenom;
use Fenom,
Fenom\Render;

View File

@ -1,5 +1,6 @@
<?php
namespace Fenom;
use Fenom\Template,
Fenom,
Fenom\Render;
@ -74,8 +75,10 @@ class TemplateTest extends TestCase
array('hello, {$b.3[$b.c_char]}!', $c, 'hello, Username!'),
array('hello, {$b[3].c}!', $c, 'hello, Username!'),
array('hello, {$b[2+1].c}!', $c, 'hello, Username!'),
array('hello, {$b[(2+1)].c}!', $c, 'hello, Username!'),
array('hello, {$b[9/3].c}!', $c, 'hello, Username!'),
array('hello, {$b[3].$c}!', $c, 'hello, Username!'),
array('hello, {$b[(3)].$c}!', $c, 'hello, Username!'),
array('hello, {$b[3][$b.c_char]}!', $c, 'hello, Username!'),
array('hello, {$b[ "m{$b.c_char}p" ]} and {$b.3[$b.c_char]}!',
$c, 'hello, Master and Username!'),
@ -90,6 +93,7 @@ class TemplateTest extends TestCase
array('hello, {$b.obj->list.a|upper}!',
$c, 'hello, WORLD!'),
array('hello, {$b[ $b.obj->c ]}!', $b, 'hello, Username!'),
array('hello, {$b[ ( $b.obj->c ) ]}!', $b, 'hello, Username!'),
array('hello, {$b[ "{$b.obj->c}" ]}!',
$b, 'hello, Username!'),
array('hello, {"World"}!', $a, 'hello, World!'),
@ -222,6 +226,7 @@ class TemplateTest extends TestCase
array('If: {$a + (*6)} end', 'Fenom\Error\CompileException', "Unexpected token '*'"),
array('If: {$a + ( 6} end', 'Fenom\Error\CompileException', "Unexpected end of expression, expect ')'"),
array('If: {$a end', 'Fenom\Error\CompileException', "Unclosed tag in line"),
array('If: {!!$a}', 'Fenom\Error\CompileException', "Unexpected token '!'"),
);
}
@ -429,6 +434,8 @@ class TemplateTest extends TestCase
return array(
// ?
array('{if $a?} right {/if}', $a),
array('{if 1?} right {/if}', $a),
array('{if 0?} no way {else} right {/if}', $a),
array('{if $unexists?} no way {else} right {/if}', $a),
array('{if $empty.array?} no way {else} right {/if}', $a),
array('{if $empty.int?} no way {else} right {/if}', $a),
@ -450,11 +457,17 @@ class TemplateTest extends TestCase
array('{$empty.double?:"empty"}', $a, "empty"),
array('{$empty.bool?:"empty"}', $a, "empty"),
array('{$empty.unexist?:"empty"}', $a, "empty"),
array('{0?:"empty"}', $a, "empty"),
// ? ... : ....
array('{$unexists ? "no way" : "right"}', $a),
array('{0 ? "no way" : "right"}', $a),
array('{$a ? "right" : "no way"}', $a),
array('{1 ? "right" : "no way"}', $a),
// !
array('{if $a!} right {/if}', $a),
array('{if 1!} right {/if}', $a),
array('{if 0!} right {/if}', $a),
array('{if null!} no way {else} right {/if}', $a),
array('{if $unexists!} no way {else} right {/if}', $a),
array('{if $empty.array!} right {/if}', $a),
array('{if $empty.int!} right {/if}', $a),
@ -470,9 +483,11 @@ class TemplateTest extends TestCase
// ! ... : ...
array('{$unexists ! "no way" : "right"}', $a),
array('{$a ! "right" : "no way"}', $a),
array('{1 ! "right" : "no way"}', $a),
// !: ...
array('{$unexists !: "right"}', $a),
array('{$a !: "right"}', $a, '1'),
array('{1 !: "right"}', $a, '1'),
);
}
@ -646,6 +661,7 @@ class TemplateTest extends TestCase
return array(
array('Layers: {foreach $list as $e} block1 {if 1} {foreachelse} {/if} {/foreach} end', 'Fenom\Error\CompileException', "Unexpected tag 'foreachelse' (this tag can be used with 'foreach')"),
array('Layers: {foreach $list as $e} block1 {if 1} {/foreach} {/if} end', 'Fenom\Error\CompileException', "Unexpected closing of the tag 'foreach'"),
array('Layers: {blah} end', 'Fenom\Error\CompileException', "Unexpected tag 'blah'"),
array('Layers: {for $a=4 to=6} block1 {if 1} {forelse} {/if} {/for} end', 'Fenom\Error\CompileException', "Unexpected tag 'forelse' (this tag can be used with 'for')"),
array('Layers: {for $a=4 to=6} block1 {if 1} {/for} {/if} end', 'Fenom\Error\CompileException', "Unexpected closing of the tag 'for'"),
array('Layers: {switch 1} {if 1} {case 1} {/if} {/switch} end', 'Fenom\Error\CompileException', "Unexpected tag 'case' (this tag can be used with 'switch')"),
@ -679,6 +695,7 @@ class TemplateTest extends TestCase
array('{if $one is not 1} block1 {else} block2 {/if}', 'block2'),
array('{if $one is not 2} block1 {else} block2 {/if}', 'block1'),
array('{if $one is $one} block1 {else} block2 {/if}', 'block1'),
array('{if $bool is true} block1 {else} block2 {/if}', 'block1'),
array('{if $float is float} block1 {else} block2 {/if}', 'block1'),
array('{if $float is not float} block1 {else} block2 {/if}', 'block2'),
array('{if $obj is object} block1 {else} block2 {/if}', 'block1'),
@ -744,7 +761,19 @@ class TemplateTest extends TestCase
);
}
public static function providerConcat() {
public static function providerInOperatorInvalid()
{
return array(
array('{$one not all 3}', 'Fenom\Error\CompileException', "Unexpected token 'not'"),
array('{$one in all}', 'Fenom\Error\CompileException', "Unexpected token 'all'"),
array('{$one in string [1,2,3]}', 'Fenom\Error\CompileException', "Can not use string operation for array"),
array('{$one in list "blah"}', 'Fenom\Error\CompileException', "Can not use array operation for string"),
array('{$one in true}', 'Fenom\Error\CompileException', "Unexpected token 'true'"),
);
}
public static function providerConcat()
{
return array(
array('{"string" ~ $one ~ up("end")}', "string1END"),
array('{"string" ~ $one++ ~ "end"}', "string1end"),
@ -754,7 +783,8 @@ class TemplateTest extends TestCase
);
}
public static function providerAccessor() {
public static function providerAccessor()
{
return array(
array('{$.get.one}', 'get1'),
array('{$.post.one}', 'post1'),
@ -765,6 +795,7 @@ class TemplateTest extends TestCase
array('{$.cookie.one}', 'cookie1'),
array('{$.server.one}', 'server1'),
array('{$.const.PHP_EOL}', PHP_EOL),
array('{$.const.MY}', ''),
array('{$.version}', Fenom::VERSION),
array('{"string"|append:"_":$.get.one}', 'string_get1'),
@ -780,7 +811,16 @@ class TemplateTest extends TestCase
);
}
public function providerStatic() {
public static function providerAccessorInvalid()
{
return array(
array('{$.nope.one}', 'Fenom\Error\CompileException', "Unexpected token 'nope'"),
array('{$.get.one}', 'Fenom\Error\SecurityException', 'Accessor are disabled', Fenom::DENY_ACCESSOR),
);
}
public function providerStatic()
{
return array(
array('{Fenom\TemplateTest::multi x=3 y=4}', '12'),
array('{Fenom\TemplateTest::multi(3,4)}', '12'),
@ -789,7 +829,8 @@ class TemplateTest extends TestCase
);
}
public function providerStaticInvalid() {
public function providerStaticInvalid()
{
return array(
array('{Fenom\TemplateTest::multi x=3 y=4}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS),
array('{Fenom\TemplateTest::multi(3,4)}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS),
@ -817,6 +858,14 @@ class TemplateTest extends TestCase
exit;
}
/**
* @dataProvider providerScalars
*/
public function testScalars($code, $result)
{
$this->exec("{" . $code . "}", $this->values, $result);
}
/**
* @dataProvider providerVars
*/
@ -1048,6 +1097,15 @@ class TemplateTest extends TestCase
$this->exec($code, self::getVars(), $result);
}
/**
* @group in_operator_invalid
* @dataProvider providerInOperatorInvalid
*/
public function testInOperatorInvalid($code, $exception, $message, $options = 0)
{
$this->execError($code, $exception, $message, $options);
}
/**
* @dataProvider providerConcat
*/
@ -1065,6 +1123,15 @@ class TemplateTest extends TestCase
$this->exec($code, self::getVars(), $result);
}
/**
* @group accessor
* @dataProvider providerAccessorInvalid
*/
public function testAccessorInvalid($code, $exception, $message, $options = 0)
{
$this->execError($code, $exception, $message, $options);
}
/**
* @group static
* @dataProvider providerStatic
@ -1074,7 +1141,8 @@ class TemplateTest extends TestCase
$this->exec($code, self::getVars(), $result, true);
}
public static function multi($x, $y = 42) {
public static function multi($x, $y = 42)
{
return $x * $y;
}

View File

@ -1,11 +1,20 @@
<?php
namespace Fenom;
use Fenom\Error\UnexpectedTokenException;
use Fenom\Tokenizer;
class TokenizerTest extends \PHPUnit_Framework_TestCase
{
public function testGetName()
{
$this->assertSame('T_DOUBLE_COLON', Tokenizer::getName(T_DOUBLE_COLON));
$this->assertSame('++', Tokenizer::getName('++'));
$this->assertSame('T_STRING', Tokenizer::getName(array(308, 'all', "", 1, "T_STRING")));
$this->assertNull(Tokenizer::getName(false));
}
public function testTokens()
{
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
@ -66,6 +75,7 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
$this->assertSame($code, $tokens->getSnippetAsString(-100, 100));
$this->assertSame('+', $tokens->getSnippetAsString(100, -100));
$this->assertSame('sin($x)+tan($x*$t)', $tokens->getSnippetAsString(-4, 6));
$this->assertSame('}', $tokens->end()->current());
}
public function testSkip()

View File

@ -20,7 +20,8 @@ class FenomTest extends \Fenom\TestCase
);
}
public function testCreating() {
public function testCreating()
{
$time = $this->tpl('temp.tpl', 'Template 1 a');
$fenom = new Fenom($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'));
$fenom->setCompileDir(FENOM_RESOURCES . '/compile');
@ -32,7 +33,8 @@ class FenomTest extends \Fenom\TestCase
$fenom->clearAllCompiles();
}
public function testFactory() {
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);
$this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl'));
@ -166,7 +168,8 @@ class FenomTest extends \Fenom\TestCase
/**
* @group tag-filter
*/
public function testTagFilter() {
public function testTagFilter()
{
$tags = array();
$punit = $this;
$this->fenom->addTagFilter(function ($text, $tpl) use (&$tags, $punit) {
@ -180,20 +183,23 @@ class FenomTest extends \Fenom\TestCase
$this->assertSame(array('var $a', '/var', '$b'), $tags);
}
public function testAddInlineCompilerSmart() {
public function testAddInlineCompilerSmart()
{
$this->fenom->addCompilerSmart('SayA', 'TestTags');
$this->tpl('inline_compiler.tpl', 'I just {SayA}.');
$this->assertSame('I just Say A.', $this->fenom->fetch('inline_compiler.tpl', array()));
}
public function testAddBlockCompilerSmart() {
public function testAddBlockCompilerSmart()
{
$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()));
}
public function testAddFunctions() {
public function testAddFunctions()
{
$this->fenom->setOptions(Fenom::DENY_NATIVE_FUNCS);
$this->assertFalse($this->fenom->isAllowedFunction('substr'));
$this->fenom->addAllowedFunctions(array('substr'));
@ -202,22 +208,26 @@ class FenomTest extends \Fenom\TestCase
}
class TestTags
{
class TestTags {
public static function tagSayA() {
public static function tagSayA()
{
return 'echo "Say A"';
}
public static function SayBlockOpen() {
public static function SayBlockOpen()
{
return 'echo "Start saying"';
}
public static function tagSaySomething() {
public static function tagSaySomething()
{
return 'echo "say blah-blah-blah"';
}
public static function SayBlockClose() {
public static function SayBlockClose()
{
return 'echo "Stop saying"';
}
}

View File

@ -1,3 +1,2 @@
{use 'extends/auto/use.tpl'}
{block 'header'}Child 2 header{/block}