Reformat. Fix pipe. Add more tests

This commit is contained in:
bzick 2014-05-06 14:22:58 +04:00
parent 6f969ee124
commit cd490d2bf6
20 changed files with 1625 additions and 739 deletions

View File

@ -140,9 +140,24 @@ class Fenom
* @var array of allowed PHP functions
*/
protected $_allowed_funcs = array(
"count" => 1, "is_string" => 1, "is_array" => 1, "is_numeric" => 1, "is_int" => 1, 'constant' => 1,
"is_object" => 1, "strtotime" => 1, "gettype" => 1, "is_double" => 1, "json_encode" => 1, "json_decode" => 1,
"ip2long" => 1, "long2ip" => 1, "strip_tags" => 1, "nl2br" => 1, "explode" => 1, "implode" => 1
"count" => 1,
"is_string" => 1,
"is_array" => 1,
"is_numeric" => 1,
"is_int" => 1,
'constant' => 1,
"is_object" => 1,
"strtotime" => 1,
"gettype" => 1,
"is_double" => 1,
"json_encode" => 1,
"json_decode" => 1,
"ip2long" => 1,
"long2ip" => 1,
"strip_tags" => 1,
"nl2br" => 1,
"explode" => 1,
"implode" => 1
);
/**
@ -460,8 +475,12 @@ class Fenom
* @param array $tags
* @return Fenom
*/
public function addBlockCompiler($compiler, $open_parser, $close_parser = self::DEFAULT_CLOSE_COMPILER, array $tags = array())
{
public function addBlockCompiler(
$compiler,
$open_parser,
$close_parser = self::DEFAULT_CLOSE_COMPILER,
array $tags = array()
) {
$this->_actions[$compiler] = array(
'type' => self::BLOCK_COMPILER,
'open' => $open_parser,
@ -548,8 +567,12 @@ class Fenom
* @param callable|string $parser_close
* @return Fenom
*/
public function addBlockFunction($function, $callback, $parser_open = self::DEFAULT_FUNC_OPEN, $parser_close = self::DEFAULT_FUNC_CLOSE)
{
public function addBlockFunction(
$function,
$callback,
$parser_open = self::DEFAULT_FUNC_OPEN,
$parser_close = self::DEFAULT_FUNC_CLOSE
) {
$this->_actions[$function] = array(
'type' => self::BLOCK_FUNCTION,
'open' => $parser_open,
@ -766,17 +789,16 @@ class Fenom
}
/**
*
*
* @param string $template name of template
* Creates pipe-line of template's data to callback
* @param string $template name of the template
* @param callable $callback template's data handler
* @param array $vars
* @param callable $callback
* @param float $chunk
* @param float $chunk amount of bytes of chunk
* @return array
*/
public function pipe($template, $callback, array $vars = array(), $chunk = 1e6)
{
ob_start($callback, $chunk, true);
ob_start($callback, $chunk, PHP_OUTPUT_HANDLER_STDFLAGS);
$data = $this->getTemplate($template)->display($vars);
ob_end_flush();
return $data;
@ -792,7 +814,6 @@ class Fenom
public function getTemplate($template, $options = 0)
{
$options |= $this->_options;
// var_dump($this->_options & self::FORCE_COMPILE);
if (is_array($template)) {
$key = dechex($options) . "@" . implode(",", $template);
} else {
@ -975,11 +996,13 @@ class Fenom
if (!$dir) {
$dir = __DIR__;
}
return spl_autoload_register(function($classname) use ($dir) {
return spl_autoload_register(
function ($classname) use ($dir) {
$file = $dir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $classname) . '.php';
if (is_file($file)) {
require_once $file;
}
});
}
);
}
}

View File

@ -235,7 +235,15 @@ class Compiler
*/
public static function forOpen(Tokenizer $tokens, Tag $scope)
{
$p = array("index" => false, "first" => false, "last" => false, "step" => 1, "to" => false, "max" => false, "min" => false);
$p = array(
"index" => false,
"first" => false,
"last" => false,
"step" => 1,
"to" => false,
"max" => false,
"min" => false
);
$scope["after"] = $before = $body = array();
$i = array('', '');
$c = "";
@ -251,16 +259,22 @@ class Compiler
if (is_numeric($p["step"])) {
if ($p["step"] > 0) {
$condition = "$var <= {$p['to']}";
if ($p["last"]) $c = "($var + {$p['step']}) > {$p['to']}";
if ($p["last"]) {
$c = "($var + {$p['step']}) > {$p['to']}";
}
} elseif ($p["step"] < 0) {
$condition = "$var >= {$p['to']}";
if ($p["last"]) $c = "($var + {$p['step']}) < {$p['to']}";
if ($p["last"]) {
$c = "($var + {$p['step']}) < {$p['to']}";
}
} else {
throw new InvalidUsageException("Invalid step value");
}
} else {
$condition = "({$p['step']} > 0 && $var <= {$p['to']} || {$p['step']} < 0 && $var >= {$p['to']})";
if ($p["last"]) $c = "({$p['step']} > 0 && ($var + {$p['step']}) <= {$p['to']} || {$p['step']} < 0 && ($var + {$p['step']}) >= {$p['to']})";
if ($p["last"]) {
$c = "({$p['step']} > 0 && ($var + {$p['step']}) <= {$p['to']} || {$p['step']} < 0 && ($var + {$p['step']}) >= {$p['to']})";
}
}
if ($p["first"]) {
@ -959,6 +973,8 @@ class Compiler
* @param Tokenizer $tokens
* @param Tag $tag
*/
public static function autoescapeClose(Tokenizer $tokens, Tag $tag) { }
public static function autoescapeClose(Tokenizer $tokens, Tag $tag)
{
}
}

View File

@ -27,7 +27,9 @@ class Modifier
{
if (!is_numeric($date)) {
$date = strtotime($date);
if (!$date) $date = time();
if (!$date) {
$date = time();
}
}
return strftime($format, $date);
}
@ -41,7 +43,9 @@ class Modifier
{
if (!is_numeric($date)) {
$date = strtotime($date);
if (!$date) $date = time();
if (!$date) {
$date = time();
}
}
return date($format, $date);
}
@ -103,7 +107,11 @@ class Modifier
if (preg_match('#^(.{' . $length . '}).*?(.{' . $length . '})?$#usS', $string, $match)) {
if (count($match) == 3) {
if ($by_words) {
return preg_replace('#\s.*$#usS', "", $match[1]) . $etc . preg_replace('#.*\s#usS', "", $match[2]);
return preg_replace('#\s.*$#usS', "", $match[1]) . $etc . preg_replace(
'#.*\s#usS',
"",
$match[2]
);
} else {
return $match[1] . $etc . $match[2];
}

View File

@ -93,7 +93,8 @@ class Tag extends \ArrayObject
* @param int $option option constant
* @param bool $value true add option, false remove option
*/
public function setOption($option, $value) {
public function setOption($option, $value)
{
$actual = (bool)($this->tpl->getOptions() & $option);
if ($actual != $value) {
$this->_changed[$option] = $actual;

View File

@ -230,7 +230,11 @@ class Template extends Render
if ($end === false) { // if unexpected end of template
throw new CompileException("Unclosed tag in line {$this->_line}", 0, 1, $this->_name, $this->_line);
}
$tag = substr($this->_src, $start, $end - $start + 1); // variable $tag contains fenom tag '{...}'
$tag = substr(
$this->_src,
$start,
$end - $start + 1
); // variable $tag contains fenom tag '{...}'
$_tag = substr($tag, 1, -1); // strip delimiters '{' and '}'
@ -252,7 +256,10 @@ class Template extends Render
} else {
$this->_appendCode($this->parseTag($tokens), $tag); // start the tag lexer
if ($tokens->key()) { // if tokenizer have tokens - throws exceptions
throw new CompileException("Unexpected token '" . $tokens->current() . "' in {$this} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line);
throw new CompileException("Unexpected token '" . $tokens->current() . "' in {$this} line {$this->_line}, near '{" . $tokens->getSnippetAsString(
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line);
}
}
}
@ -274,7 +281,10 @@ class Template extends Render
}
$_names[] = '{' . $scope->name . '} opened on line ' . $scope->line;
}
throw new CompileException("Unclosed tag" . (count($_names) == 1 ? "" : "s") . ": " . implode(", ", $_names), 0, 1, $this->_name, $_line);
throw new CompileException("Unclosed tag" . (count($_names) == 1 ? "" : "s") . ": " . implode(
", ",
$_names
), 0, 1, $this->_name, $_line);
}
$this->_src = ""; // cleanup
if ($this->_post) {
@ -293,7 +303,8 @@ class Template extends Render
* @param int $option
* @param bool $value
*/
public function setOption($option, $value) {
public function setOption($option, $value)
{
if ($value) {
$this->_options |= $option;
} else {
@ -552,9 +563,15 @@ class Template extends Render
} catch (InvalidUsageException $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\LogicException $e) {
throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\Exception $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
}
}
@ -594,7 +611,8 @@ class Template extends Render
{
$action = $tokens->get(Tokenizer::MACRO_STRING);
$tokens->next();
if ($tokens->is("(", T_DOUBLE_COLON, T_NS_SEPARATOR) && !$tokens->isWhiteSpaced()) { // just invoke function or static method
if ($tokens->is("(", T_DOUBLE_COLON, T_NS_SEPARATOR) && !$tokens->isWhiteSpaced()
) { // just invoke function or static method
$tokens->back();
return $this->out($this->parseExpr($tokens));
} elseif ($tokens->is('.')) {
@ -626,7 +644,10 @@ 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) . "')");
throw new TokenizeException("Unexpected tag '$action' (this tag can be used with '" . implode(
"', '",
$tags
) . "')");
} else {
throw new TokenizeException("Unexpected tag '$action'");
}
@ -952,7 +973,12 @@ class Template extends Render
return '((' . $var . ' !== null) ? ' . $var . ' : (' . $this->parseExpr($tokens) . '))';
}
}
} elseif ($tokens->is(Tokenizer::MACRO_BINARY, Tokenizer::MACRO_BOOLEAN, Tokenizer::MACRO_MATH) || !$tokens->valid()) {
} elseif ($tokens->is(
Tokenizer::MACRO_BINARY,
Tokenizer::MACRO_BOOLEAN,
Tokenizer::MACRO_MATH
) || !$tokens->valid()
) {
if ($empty) {
if ($is_var) {
return '!empty(' . $var . ')';
@ -1263,7 +1289,16 @@ class Template extends Render
$key = true;
$val = false;
$_arr .= $tokens->getAndNext() . ' ';
} elseif ($tokens->is(Tokenizer::MACRO_SCALAR, T_VARIABLE, T_STRING, T_EMPTY, T_ISSET, "(", "#") && !$val) {
} elseif ($tokens->is(
Tokenizer::MACRO_SCALAR,
T_VARIABLE,
T_STRING,
T_EMPTY,
T_ISSET,
"(",
"#"
) && !$val
) {
$_arr .= $this->parseExpr($tokens);
$key = false;
$val = true;
@ -1381,7 +1416,16 @@ class Template extends Render
$tokens->next();
$arg = $colon = false;
while ($tokens->valid()) {
if (!$arg && $tokens->is(T_VARIABLE, T_STRING, "(", Tokenizer::MACRO_SCALAR, '"', Tokenizer::MACRO_UNARY, Tokenizer::MACRO_INCDEC)) {
if (!$arg && $tokens->is(
T_VARIABLE,
T_STRING,
"(",
Tokenizer::MACRO_SCALAR,
'"',
Tokenizer::MACRO_UNARY,
Tokenizer::MACRO_INCDEC
)
) {
$_args .= $this->parseExpr($tokens);
$arg = true;
$colon = false;

View File

@ -93,50 +93,160 @@ class Tokenizer
*/
public static $macros = array(
self::MACRO_STRING => array(
\T_ABSTRACT => 1, \T_ARRAY => 1, \T_AS => 1, \T_BREAK => 1, \T_BREAK => 1, \T_CASE => 1,
\T_CATCH => 1, \T_CLASS => 1, \T_CLASS_C => 1, \T_CLONE => 1, \T_CONST => 1, \T_CONTINUE => 1,
\T_DECLARE => 1, \T_DEFAULT => 1, \T_DIR => 1, \T_DO => 1, \T_ECHO => 1, \T_ELSE => 1,
\T_ELSEIF => 1, \T_EMPTY => 1, \T_ENDDECLARE => 1, \T_ENDFOR => 1, \T_ENDFOREACH => 1, \T_ENDIF => 1,
\T_ENDSWITCH => 1, \T_ENDWHILE => 1, \T_EVAL => 1, \T_EXIT => 1, \T_EXTENDS => 1, \T_FILE => 1,
\T_FINAL => 1, \T_FOR => 1, \T_FOREACH => 1, \T_FUNCTION => 1, \T_FUNC_C => 1, \T_GLOBAL => 1,
\T_GOTO => 1, \T_HALT_COMPILER => 1, \T_IF => 1, \T_IMPLEMENTS => 1, \T_INCLUDE => 1, \T_INCLUDE_ONCE => 1,
\T_INSTANCEOF => 1, \T_INSTEADOF => 1, \T_INTERFACE => 1, \T_ISSET => 1, \T_LINE => 1, \T_LIST => 1,
\T_LOGICAL_AND => 1, \T_LOGICAL_OR => 1, \T_LOGICAL_XOR => 1, \T_METHOD_C => 1, \T_NAMESPACE => 1, \T_NS_C => 1,
\T_NEW => 1, \T_PRINT => 1, \T_PRIVATE => 1, \T_PUBLIC => 1, \T_PROTECTED => 1, \T_REQUIRE => 1,
\T_REQUIRE_ONCE => 1, \T_RETURN => 1, \T_RETURN => 1, \T_STRING => 1, \T_SWITCH => 1, \T_THROW => 1,
\T_TRAIT => 1, \T_TRAIT_C => 1, \T_TRY => 1, \T_UNSET => 1, \T_USE => 1, \T_VAR => 1,
\T_WHILE => 1, \T_YIELD => 1
\T_ABSTRACT => 1,
\T_ARRAY => 1,
\T_AS => 1,
\T_BREAK => 1,
\T_BREAK => 1,
\T_CASE => 1,
\T_CATCH => 1,
\T_CLASS => 1,
\T_CLASS_C => 1,
\T_CLONE => 1,
\T_CONST => 1,
\T_CONTINUE => 1,
\T_DECLARE => 1,
\T_DEFAULT => 1,
\T_DIR => 1,
\T_DO => 1,
\T_ECHO => 1,
\T_ELSE => 1,
\T_ELSEIF => 1,
\T_EMPTY => 1,
\T_ENDDECLARE => 1,
\T_ENDFOR => 1,
\T_ENDFOREACH => 1,
\T_ENDIF => 1,
\T_ENDSWITCH => 1,
\T_ENDWHILE => 1,
\T_EVAL => 1,
\T_EXIT => 1,
\T_EXTENDS => 1,
\T_FILE => 1,
\T_FINAL => 1,
\T_FOR => 1,
\T_FOREACH => 1,
\T_FUNCTION => 1,
\T_FUNC_C => 1,
\T_GLOBAL => 1,
\T_GOTO => 1,
\T_HALT_COMPILER => 1,
\T_IF => 1,
\T_IMPLEMENTS => 1,
\T_INCLUDE => 1,
\T_INCLUDE_ONCE => 1,
\T_INSTANCEOF => 1,
\T_INSTEADOF => 1,
\T_INTERFACE => 1,
\T_ISSET => 1,
\T_LINE => 1,
\T_LIST => 1,
\T_LOGICAL_AND => 1,
\T_LOGICAL_OR => 1,
\T_LOGICAL_XOR => 1,
\T_METHOD_C => 1,
\T_NAMESPACE => 1,
\T_NS_C => 1,
\T_NEW => 1,
\T_PRINT => 1,
\T_PRIVATE => 1,
\T_PUBLIC => 1,
\T_PROTECTED => 1,
\T_REQUIRE => 1,
\T_REQUIRE_ONCE => 1,
\T_RETURN => 1,
\T_RETURN => 1,
\T_STRING => 1,
\T_SWITCH => 1,
\T_THROW => 1,
\T_TRAIT => 1,
\T_TRAIT_C => 1,
\T_TRY => 1,
\T_UNSET => 1,
\T_USE => 1,
\T_VAR => 1,
\T_WHILE => 1,
\T_YIELD => 1
),
self::MACRO_INCDEC => array(
\T_INC => 1, \T_DEC => 1
\T_INC => 1,
\T_DEC => 1
),
self::MACRO_UNARY => array(
"!" => 1, "~" => 1, "-" => 1
"!" => 1,
"~" => 1,
"-" => 1
),
self::MACRO_BINARY => array(
\T_BOOLEAN_AND => 1, \T_BOOLEAN_OR => 1, \T_IS_GREATER_OR_EQUAL => 1, \T_IS_EQUAL => 1, \T_IS_IDENTICAL => 1,
\T_IS_NOT_EQUAL => 1, \T_IS_NOT_IDENTICAL => 1, \T_IS_SMALLER_OR_EQUAL => 1, \T_LOGICAL_AND => 1,
\T_LOGICAL_OR => 1, \T_LOGICAL_XOR => 1, \T_SL => 1, \T_SR => 1,
"+" => 1, "-" => 1, "*" => 1, "/" => 1, ">" => 1, "<" => 1, "^" => 1, "%" => 1, "&" => 1
\T_BOOLEAN_AND => 1,
\T_BOOLEAN_OR => 1,
\T_IS_GREATER_OR_EQUAL => 1,
\T_IS_EQUAL => 1,
\T_IS_IDENTICAL => 1,
\T_IS_NOT_EQUAL => 1,
\T_IS_NOT_IDENTICAL => 1,
\T_IS_SMALLER_OR_EQUAL => 1,
\T_LOGICAL_AND => 1,
\T_LOGICAL_OR => 1,
\T_LOGICAL_XOR => 1,
\T_SL => 1,
\T_SR => 1,
"+" => 1,
"-" => 1,
"*" => 1,
"/" => 1,
">" => 1,
"<" => 1,
"^" => 1,
"%" => 1,
"&" => 1
),
self::MACRO_BOOLEAN => array(
\T_LOGICAL_OR => 1, \T_LOGICAL_XOR => 1, \T_BOOLEAN_AND => 1, \T_BOOLEAN_OR => 1, \T_LOGICAL_AND => 1
\T_LOGICAL_OR => 1,
\T_LOGICAL_XOR => 1,
\T_BOOLEAN_AND => 1,
\T_BOOLEAN_OR => 1,
\T_LOGICAL_AND => 1
),
self::MACRO_MATH => array(
"+" => 1, "-" => 1, "*" => 1, "/" => 1, "^" => 1, "%" => 1, "&" => 1, "|" => 1
"+" => 1,
"-" => 1,
"*" => 1,
"/" => 1,
"^" => 1,
"%" => 1,
"&" => 1,
"|" => 1
),
self::MACRO_COND => array(
\T_IS_EQUAL => 1, \T_IS_IDENTICAL => 1, ">" => 1, "<" => 1, \T_SL => 1, \T_SR => 1,
\T_IS_NOT_EQUAL => 1, \T_IS_NOT_IDENTICAL => 1, \T_IS_SMALLER_OR_EQUAL => 1,
\T_IS_EQUAL => 1,
\T_IS_IDENTICAL => 1,
">" => 1,
"<" => 1,
\T_SL => 1,
\T_SR => 1,
\T_IS_NOT_EQUAL => 1,
\T_IS_NOT_IDENTICAL => 1,
\T_IS_SMALLER_OR_EQUAL => 1,
),
self::MACRO_EQUALS => array(
\T_AND_EQUAL => 1, \T_DIV_EQUAL => 1, \T_MINUS_EQUAL => 1, \T_MOD_EQUAL => 1,
\T_MUL_EQUAL => 1, \T_OR_EQUAL => 1, \T_PLUS_EQUAL => 1, \T_SL_EQUAL => 1, \T_SR_EQUAL => 1,
\T_XOR_EQUAL => 1, '=' => 1,
\T_AND_EQUAL => 1,
\T_DIV_EQUAL => 1,
\T_MINUS_EQUAL => 1,
\T_MOD_EQUAL => 1,
\T_MUL_EQUAL => 1,
\T_OR_EQUAL => 1,
\T_PLUS_EQUAL => 1,
\T_SL_EQUAL => 1,
\T_SR_EQUAL => 1,
\T_XOR_EQUAL => 1,
'=' => 1,
// \T_CONCAT_EQUAL => 1,
),
self::MACRO_SCALAR => array(
\T_LNUMBER => 1, \T_DNUMBER => 1, \T_CONSTANT_ENCAPSED_STRING => 1
\T_LNUMBER => 1,
\T_DNUMBER => 1,
\T_CONSTANT_ENCAPSED_STRING => 1
)
);
@ -157,7 +267,12 @@ class Tokenizer
* @var array
*/
private static $spec = array(
'true' => 1, 'false' => 1, 'null' => 1, 'TRUE' => 1, 'FALSE' => 1, 'NULL' => 1
'true' => 1,
'false' => 1,
'null' => 1,
'TRUE' => 1,
'FALSE' => 1,
'NULL' => 1
);
/**

View File

@ -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");
}

View File

@ -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
),
);
}

View File

@ -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(
$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()));
),
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(
$this->assertSame(
$result,
$this->fenom->fetch(
array(
'extends/auto/child.3.tpl',
'extends/auto/child.2.tpl',
'extends/auto/static/child.1.tpl'
), array()));
),
array()
)
);
}
public function testStaticExtendNested()

View File

@ -7,7 +7,9 @@ class MacrosTest extends TestCase
public function setUp()
{
parent::setUp();
$this->tpl("math.tpl", '
$this->tpl(
"math.tpl",
'
{macro plus(x, y)}
x + y = {$x + $y}
{/macro}
@ -21,42 +23,58 @@ class MacrosTest extends TestCase
{/macro}
Math: {macro.plus x=2 y=3}, {macro.minus x=10 y=4}
');
'
);
$this->tpl("import.tpl", '
$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}
');
'
);
$this->tpl("import_custom.tpl", '
$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}.
');
'
);
$this->tpl("import_miss.tpl", '
$this->tpl(
"import_miss.tpl",
'
{import [minus] from "math.tpl" as math}
a: {macro.plus x=5 y=3}.
');
'
);
$this->tpl("macro_recursive.tpl", '{macro factorial(num)}
$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", '
$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)}
var_dump(
$this->fenom->compileCode(
'{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}')->getBody());
{/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)
);
}
/**

View File

@ -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(
$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(
$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(
$this->assertEquals(
$out,
$tpl->fetch(
array(
"data" => $in,
)));
)
)
);
}
}

View File

@ -97,11 +97,14 @@ class ProviderTest extends TestCase
{
$list = $this->provider->getList();
sort($list);
$this->assertSame(array(
$this->assertSame(
array(
"sub/template3.tpl",
"template1.tpl",
"template2.tpl"
), $list);
),
$list
);
}
public function testRm()

View File

@ -3,10 +3,12 @@
namespace Fenom;
class TagOptionTest extends TestCase {
class TagOptionTest extends TestCase
{
public function testTrim() {
public function testTrim()
{
}
}

View File

@ -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, "
);
}
/**

View File

@ -65,8 +65,11 @@ class TemplateTest extends TestCase
array('hello, {$b[ "mcp" ]}!', $b, 'hello, Master!'),
array('hello, {$b[ "m{$c}p" ]}!', $b, 'hello, Master!'),
array('hello, {$b."m{$c}p"}!', $b, 'hello, Master!'),
array('hello, {$b[ "m{$b.c_char}p" ]}!',
$b, 'hello, Master!'),
array(
'hello, {$b[ "m{$b.c_char}p" ]}!',
$b,
'hello, Master!'
),
array('hello, {$b[ \'m{$c}p\' ]}!', $b, 'hello, Unknown!'),
array('hello, {$b.4}!', $c, 'hello, Mister!'),
array('hello, {$b[4]}!', $c, 'hello, Mister!'),
@ -80,22 +83,33 @@ class TemplateTest extends TestCase
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!'),
array(
'hello, {$b[ "m{$b.c_char}p" ]} and {$b.3[$b.c_char]}!',
$c,
'hello, Master and Username!'
),
array('hello, {$b.obj->name}!', $c, 'hello, Object!'),
array('hello, {$b.obj->list.a}!', $c, 'hello, World!'),
array('hello, {$b[obj]->name}!', $c, 'hello, Object!'),
array('hello, {$b["obj"]->name}!', $c, 'hello, Object!'),
array('hello, {$b."obj"->name}!', $c, 'hello, Object!'),
array('hello, {$b.obj->name|upper}!',
$c, 'hello, OBJECT!'),
array('hello, {$b.obj->list.a|upper}!',
$c, 'hello, WORLD!'),
array(
'hello, {$b.obj->name|upper}!',
$c,
'hello, OBJECT!'
),
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, {$b[ "{$b.obj->c}" ]}!',
$b,
'hello, Username!'
),
array('hello, {"World"}!', $a, 'hello, World!'),
array('hello, {"W{$a}d"}!', $a, 'hello, WWorldd!'),
array('hello, {$world->chunk(1)->self->chunk("new")}!', $b, 'hello, world!'),
@ -113,7 +127,12 @@ class TemplateTest extends TestCase
array('hello, {$b[9/].c}!', 'Fenom\Error\CompileException', "Unexpected token ']'"),
array('hello, {$b[3]$c}!', 'Fenom\Error\CompileException', "Unexpected token '\$c'"),
array('hello, {$b[3]c}!', 'Fenom\Error\CompileException', "Unexpected token 'c'"),
array('hello, {$b.obj->valid()}!', 'Fenom\Error\SecurityException', "Forbidden to call methods", Fenom::DENY_METHODS),
array(
'hello, {$b.obj->valid()}!',
'Fenom\Error\SecurityException',
"Forbidden to call methods",
Fenom::DENY_METHODS
),
);
}
@ -162,7 +181,12 @@ class TemplateTest extends TestCase
{
return array(
array('Mod: {$lorem|}!', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Mod: {$lorem|str_rot13}!', 'Fenom\Error\CompileException', "Modifier str_rot13 not found", Fenom::DENY_INLINE_FUNCS),
array(
'Mod: {$lorem|str_rot13}!',
'Fenom\Error\CompileException',
"Modifier str_rot13 not found",
Fenom::DENY_INLINE_FUNCS
),
array('Mod: {$lorem|my_encode}!', 'Fenom\Error\CompileException', "Modifier my_encode not found"),
array('Mod: {$lorem|truncate:}!', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Mod: {$lorem|truncate:abs}!', 'Fenom\Error\CompileException', "Unexpected token 'abs'"),
@ -184,7 +208,6 @@ class TemplateTest extends TestCase
array('Exp: {$y-' . $x . '} result', $b, 'Exp: 18 result'),
array('Exp: {' . $x . '*$y} result', $b, 'Exp: 243 result'),
array('Exp: {$y^' . $x . '} result', $b, 'Exp: 18 result'),
array('Exp: {$x+$y} result', $b, 'Exp: 36 result'),
array('Exp: {$y/$x} result', $b, 'Exp: 3 result'),
array('Exp: {$y-$x} result', $b, 'Exp: 18 result'),
@ -197,14 +220,19 @@ class TemplateTest extends TestCase
array('Exp: {-1} result', $b, 'Exp: -1 result'),
array('Exp: {$z = 5} {$z} result', $b, 'Exp: 5 5 result'),
array('Exp: {$k.i = "str"} {$k.i} result', $b, 'Exp: str str result'),
array('Exp: {($y*$x - (($x+$y) + $y/$x) ^ $y)/4} result',
$b, 'Exp: 53.75 result'),
array(
'Exp: {($y*$x - (($x+$y) + $y/$x) ^ $y)/4} result',
$b,
'Exp: 53.75 result'
),
array('Exp: {$x+max($x, $y)} result', $b, 'Exp: 36 result'),
array('Exp: {max(1,2)} result', $b, 'Exp: 2 result'),
array('Exp: {round(sin(pi()), 8)} result', $b, 'Exp: 0 result'),
array('Exp: {max($x, $y) + round(sin(pi()), 8) - min($x, $y) +3} result',
$b, 'Exp: 21 result'),
array(
'Exp: {max($x, $y) + round(sin(pi()), 8) - min($x, $y) +3} result',
$b,
'Exp: 21 result'
),
);
}
@ -259,9 +287,17 @@ class TemplateTest extends TestCase
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2),
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2, Fenom::FORCE_INCLUDE),
array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3),
array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3, Fenom::FORCE_INCLUDE),
array('Include {include "welcome.tpl" username="Flame" email="flame@dev.null"} template',
$a, $result4),
array(
'Include {include "welcome.tpl" email="flame@dev.null"} template',
$a,
$result3,
Fenom::FORCE_INCLUDE
),
array(
'Include {include "welcome.tpl" username="Flame" email="flame@dev.null"} template',
$a,
$result4
),
);
}
@ -269,8 +305,16 @@ class TemplateTest extends TestCase
{
return array(
array('Include {include} template', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Include {include another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Unexpected token 'another'"),
array('Include {include "../../TestCase.php"} template', 'Fenom\Error\SecurityException', "Template ../../TestCase.php not found"),
array(
'Include {include another="welcome.tpl"} template',
'Fenom\Error\CompileException',
"Unexpected token 'another'"
),
array(
'Include {include "../../TestCase.php"} template',
'Fenom\Error\SecurityException',
"Template ../../TestCase.php not found"
),
);
}
@ -296,15 +340,51 @@ class TemplateTest extends TestCase
{
return array(
array('Include {insert} template', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Include {insert another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Unexpected token 'another'"),
array('Include {insert $tpl} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "$tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "{$tpl}"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "$name.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "{$name}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "{$pr_name|lower}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "wel{$fragment}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "welcome.tpl" email="flame@dev.null"} template', 'Fenom\Error\CompileException', "Unexpected token 'email'"),
array(
'Include {insert another="welcome.tpl"} template',
'Fenom\Error\CompileException',
"Unexpected token 'another'"
),
array(
'Include {insert $tpl} template',
'Fenom\Error\CompileException',
"Tag {insert} accept only static template name"
),
array(
'Include {insert "$tpl"} template',
'Fenom\Error\CompileException',
"Tag {insert} accept only static template name"
),
array(
'Include {insert "{$tpl}"} template',
'Fenom\Error\CompileException',
"Tag {insert} accept only static template name"
),
array(
'Include {insert "$name.tpl"} template',
'Fenom\Error\CompileException',
"Tag {insert} accept only static template name"
),
array(
'Include {insert "{$name}.tpl"} template',
'Fenom\Error\CompileException',
"Tag {insert} accept only static template name"
),
array(
'Include {insert "{$pr_name|lower}.tpl"} template',
'Fenom\Error\CompileException',
"Tag {insert} accept only static template name"
),
array(
'Include {insert "wel{$fragment}.tpl"} template',
'Fenom\Error\CompileException',
"Tag {insert} accept only static template name"
),
array(
'Include {insert "welcome.tpl" email="flame@dev.null"} template',
'Fenom\Error\CompileException',
"Unexpected token 'email'"
),
);
}
@ -324,12 +404,21 @@ class TemplateTest extends TestCase
array('if: {if $val1} block1 {else} block2 {/if} end', $a, 'if: block1 end'),
array('if: {if $val1 || $val0} block1 {else} block2 {/if} end', $a, 'if: block1 end'),
array('if: {if $val1 && $val0} block1 {else} block2 {/if} end', $a, 'if: block2 end'),
array('if: {if $x-9} block1 {elseif $x} block2 {else} block3 {/if} end',
$a, 'if: block2 end'),
array('if: {if round(sin(pi()), 8)} block1 {elseif $x} block2 {else} block3 {/if} end',
$a, 'if: block2 end'),
array('if: {if round(sin(pi()), 8)} block1 {elseif $val0} block2 {else} block3 {/if} end',
$a, 'if: block3 end'),
array(
'if: {if $x-9} block1 {elseif $x} block2 {else} block3 {/if} end',
$a,
'if: block2 end'
),
array(
'if: {if round(sin(pi()), 8)} block1 {elseif $x} block2 {else} block3 {/if} end',
$a,
'if: block2 end'
),
array(
'if: {if round(sin(pi()), 8)} block1 {elseif $val0} block2 {else} block3 {/if} end',
$a,
'if: block3 end'
),
array('if: {if empty($val0)} block1 {else} block2 {/if} end', $a, 'if: block1 end'),
array('if: {if $val0?} block1 {else} block2 {/if} end', $a, 'if: block2 end'),
array('if: {if $val1?} block1 {else} block2 {/if} end', $a, 'if: block1 end'),
@ -341,8 +430,11 @@ class TemplateTest extends TestCase
array('if: {if false} block1 {else} block2 {/if} end', $a, 'if: block2 end'),
array('if: {if null} block1 {else} block2 {/if} end', $a, 'if: block2 end'),
array('if: {if max(2, 4) > 1 && max(2, 3) < 1} block1 {else} block2 {/if} end', $a, 'if: block2 end'),
array('if: {if ($val1 || $val0) && $x} block1 {else} block2 {/if} end',
$a, 'if: block1 end'),
array(
'if: {if ($val1 || $val0) && $x} block1 {else} block2 {/if} end',
$a,
'if: block1 end'
),
array('if: {if $unexist} block1 {else} block2 {/if} end', $a, 'if: block2 end', Fenom::FORCE_VERIFY),
);
}
@ -351,9 +443,21 @@ class TemplateTest extends TestCase
{
return array(
array('If: {if} block1 {/if} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('If: {if 1} block1 {elseif} block2 {/if} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('If: {if 1} block1 {else} block2 {elseif 0} block3 {/if} end', 'Fenom\Error\CompileException', "Incorrect use of the tag {elseif}"),
array('If: {if 1} block1 {else} block2 {/if} block3 {elseif 0} end', 'Fenom\Error\CompileException', "Unexpected tag 'elseif' (this tag can be used with 'if')"),
array(
'If: {if 1} block1 {elseif} block2 {/if} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array(
'If: {if 1} block1 {else} block2 {elseif 0} block3 {/if} end',
'Fenom\Error\CompileException',
"Incorrect use of the tag {elseif}"
),
array(
'If: {if 1} block1 {else} block2 {/if} block3 {elseif 0} end',
'Fenom\Error\CompileException',
"Unexpected tag 'elseif' (this tag can be used with 'if')"
),
);
}
@ -366,10 +470,14 @@ class TemplateTest extends TestCase
);
return array(
array('Create: {var $v = $x+$y} Result: {$v} end', $a, 'Create: Result: 36 end'),
array('Create: {var $v =
array(
'Create: {var $v =
$x
+
$y} Result: {$v} end', $a, 'Create: Result: 36 end'),
$y} Result: {$v} end',
$a,
'Create: Result: 36 end'
),
array('Create: {var $v = $z++} Result: {$v}, {$z} end', $a, 'Create: Result: 99, 100 end'),
array('Create: {var $v = $z++ + 1} Result: {$v}, {$z} end', $a, 'Create: Result: 100, 100 end'),
array('Create: {var $v = --$z} Result: {$v}, {$z} end', $a, 'Create: Result: 98, 98 end'),
@ -377,16 +485,27 @@ class TemplateTest extends TestCase
array('Create: {var $v = $y-$x} Result: {$v} end', $a, 'Create: Result: 18 end'),
array('Create: {var $v = $y*$x-2} Result: {$v} end', $a, 'Create: Result: 241 end'),
array('Create: {var $v = ($y^$x)+7} Result: {$v} end', $a, 'Create: Result: 25 end'),
array('Create: {var $v = [1,2,3]} Result: {$v.1} end', $a, 'Create: Result: 2 end'),
array('Create: {var $v = []} Result: {if $v} have items {else} empty {/if} end',
$a, 'Create: Result: empty end'),
array('Create: {var $v = ["one"|upper => 1, 4 => $x, "three" => 3]} Result: {$v.three}, {$v.4}, {$v.ONE} end',
$a, 'Create: Result: 3, 9, 1 end'),
array('Create: {var $v = ["key1" => $y*$x-2, "key2" => ["z" => $z]]} Result: {$v.key1}, {$v.key2.z} end',
$a, 'Create: Result: 241, 99 end'),
array('Create: {var $v = count([1,2,3])+7} Result: {$v} end',
$a, 'Create: Result: 10 end'),
array(
'Create: {var $v = []} Result: {if $v} have items {else} empty {/if} end',
$a,
'Create: Result: empty end'
),
array(
'Create: {var $v = ["one"|upper => 1, 4 => $x, "three" => 3]} Result: {$v.three}, {$v.4}, {$v.ONE} end',
$a,
'Create: Result: 3, 9, 1 end'
),
array(
'Create: {var $v = ["key1" => $y*$x-2, "key2" => ["z" => $z]]} Result: {$v.key1}, {$v.key2.z} end',
$a,
'Create: Result: 241, 99 end'
),
array(
'Create: {var $v = count([1,2,3])+7} Result: {$v} end',
$a,
'Create: Result: 10 end'
),
);
}
@ -394,19 +513,60 @@ class TemplateTest extends TestCase
{
return array(
array('Create: {var $v} Result: {$v} end', 'Fenom\Error\CompileException', "Unclosed tag: {var} opened"),
array('Create: {var $v = } Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array(
'Create: {var $v = } Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array('Create: {var $v = 1++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"),
array('Create: {var $v = c} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token 'c'"),
array('Create: {var $v = ($a)++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"),
array('Create: {var $v = --$a++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"),
array('Create: {var $v = $a|upper++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"),
array('Create: {var $v = max($a,2)++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"),
array('Create: {var $v = max($a,2)} Result: {$v} end', 'Fenom\Error\CompileException', "Function max not found", Fenom::DENY_NATIVE_FUNCS),
array('Create: {var $v = 4*} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array(
'Create: {var $v = ($a)++} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected token '++'"
),
array(
'Create: {var $v = --$a++} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected token '++'"
),
array(
'Create: {var $v = $a|upper++} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected token '++'"
),
array(
'Create: {var $v = max($a,2)++} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected token '++'"
),
array(
'Create: {var $v = max($a,2)} Result: {$v} end',
'Fenom\Error\CompileException',
"Function max not found",
Fenom::DENY_NATIVE_FUNCS
),
array(
'Create: {var $v = 4*} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array('Create: {var $v = ""$a} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '\$a'"),
array('Create: {var $v = [1,2} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Create: {var $v = empty(2)} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token 2, isset() and empty() accept only variables"),
array('Create: {var $v = isset(2)} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token 2, isset() and empty() accept only variables"),
array(
'Create: {var $v = [1,2} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array(
'Create: {var $v = empty(2)} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected token 2, isset() and empty() accept only variables"
),
array(
'Create: {var $v = isset(2)} Result: {$v} end',
'Fenom\Error\CompileException',
"Unexpected token 2, isset() and empty() accept only variables"
),
);
}
@ -500,47 +660,179 @@ class TemplateTest extends TestCase
return array(
array('Foreach: {foreach $list as $e} {$e}, {/foreach} end', $a, 'Foreach: one, two, three, end'),
array('Foreach: {foreach $list as $e} {$e},{break} break {/foreach} end', $a, 'Foreach: one, end'),
array('Foreach: {foreach $list as $e} {$e},{continue} continue {/foreach} end', $a, 'Foreach: one, two, three, end'),
array('Foreach: {foreach ["one", "two", "three"] as $e} {$e}, {/foreach} end', $a, 'Foreach: one, two, three, end'),
array('Foreach: {foreach $list as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: 1 => one, 2 => two, 3 => three, end'),
array('Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: 1 => one, 2 => two, 3 => three, end'),
array(
'Foreach: {foreach $list as $e} {$e},{continue} continue {/foreach} end',
$a,
'Foreach: one, two, three, end'
),
array(
'Foreach: {foreach ["one", "two", "three"] as $e} {$e}, {/foreach} end',
$a,
'Foreach: one, two, three, end'
),
array(
'Foreach: {foreach $list as $k => $e} {$k} => {$e}, {/foreach} end',
$a,
'Foreach: 1 => one, 2 => two, 3 => three, end'
),
array(
'Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e} {$k} => {$e}, {/foreach} end',
$a,
'Foreach: 1 => one, 2 => two, 3 => three, end'
),
array('Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array('Foreach: {foreach [] as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array('Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {foreachelse} empty {/foreach} end', $a, 'Foreach: empty end'),
array('Foreach: {foreach $list as $e index=$i} {$i}: {$e}, {/foreach} end', $a, 'Foreach: 0: one, 1: two, 2: three, end'),
array('Foreach: {foreach $list as $k => $e index=$i} {$i}: {$k} => {$e}, {/foreach} end', $a, 'Foreach: 0: 1 => one, 1: 2 => two, 2: 3 => three, end'),
array('Foreach: {foreach $empty as $k => $e index=$i} {$i}: {$k} => {$e}, {foreachelse} empty {/foreach} end', $a, 'Foreach: empty end'),
array('Foreach: {foreach $list as $k => $e first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {/foreach} end', $a, 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, end'),
array('Foreach: {foreach $list as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end', $a, 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end'),
array('Foreach: {foreach $empty as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {foreachelse} empty {/foreach} end', $a, 'Foreach: empty end'),
array('Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end', $a, 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end'),
array(
'Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {foreachelse} empty {/foreach} end',
$a,
'Foreach: empty end'
),
array(
'Foreach: {foreach $list as $e index=$i} {$i}: {$e}, {/foreach} end',
$a,
'Foreach: 0: one, 1: two, 2: three, end'
),
array(
'Foreach: {foreach $list as $k => $e index=$i} {$i}: {$k} => {$e}, {/foreach} end',
$a,
'Foreach: 0: 1 => one, 1: 2 => two, 2: 3 => three, end'
),
array(
'Foreach: {foreach $empty as $k => $e index=$i} {$i}: {$k} => {$e}, {foreachelse} empty {/foreach} end',
$a,
'Foreach: empty end'
),
array(
'Foreach: {foreach $list as $k => $e first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {/foreach} end',
$a,
'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, end'
),
array(
'Foreach: {foreach $list as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end',
$a,
'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end'
),
array(
'Foreach: {foreach $empty as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {foreachelse} empty {/foreach} end',
$a,
'Foreach: empty end'
),
array(
'Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end',
$a,
'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end'
),
);
}
public static function providerForeachInvalid()
{
return array(
array('Foreach: {foreach} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected end of tag {foreach}"),
array('Foreach: {foreach $list} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Foreach: {foreach $list+1 as $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach array_random() as $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'array_random'"),
array('Foreach: {foreach $list as $e+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $k+1 => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as max($i,1) => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as max($e,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '=>'"),
array('Foreach: {foreach $list $k => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '\$k'"),
array('Foreach: {foreach $list as $k =>} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Foreach: {foreach last=$l $list as $e } {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'last' in tag {foreach}"),
array('Foreach: {foreach $list as $e unknown=1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unknown parameter 'unknown'"),
array('Foreach: {foreach $list as $e index=$i+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e first=$f+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e last=$l+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"),
array('Foreach: {foreach $list as $e index=max($i,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e first=max($i,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e last=max($i,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"),
array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {break} {/foreach} end', 'Fenom\Error\CompileException', "Improper usage of the tag {break}"),
array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {continue} {/foreach} end', 'Fenom\Error\CompileException', "Improper usage of the tag {continue}"),
array(
'Foreach: {foreach} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected end of tag {foreach}"
),
array(
'Foreach: {foreach $list} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array(
'Foreach: {foreach $list+1 as $e} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '+'"
),
array(
'Foreach: {foreach array_random() as $e} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token 'array_random'"
),
array(
'Foreach: {foreach $list as $e+1} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '+'"
),
array(
'Foreach: {foreach $list as $k+1 => $e} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '+'"
),
array(
'Foreach: {foreach $list as max($i,1) => $e} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token 'max'"
),
array(
'Foreach: {foreach $list as max($e,1)} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token 'max'"
),
array(
'Foreach: {foreach $list => $e} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '=>'"
),
array(
'Foreach: {foreach $list $k => $e} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '\$k'"
),
array(
'Foreach: {foreach $list as $k =>} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array(
'Foreach: {foreach last=$l $list as $e } {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token 'last' in tag {foreach}"
),
array(
'Foreach: {foreach $list as $e unknown=1} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unknown parameter 'unknown'"
),
array(
'Foreach: {foreach $list as $e index=$i+1} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '+'"
),
array(
'Foreach: {foreach $list as $e first=$f+1} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '+'"
),
array(
'Foreach: {foreach $list as $e last=$l+1} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token '+'"
),
array(
'Foreach: {foreach $list as $e index=max($i,1)} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token 'max'"
),
array(
'Foreach: {foreach $list as $e first=max($i,1)} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token 'max'"
),
array(
'Foreach: {foreach $list as $e last=max($i,1)} {$e}, {/foreach} end',
'Fenom\Error\CompileException',
"Unexpected token 'max'"
),
array(
'Foreach: {foreach $list as $e} {$e}, {foreachelse} {break} {/foreach} end',
'Fenom\Error\CompileException',
"Improper usage of the tag {break}"
),
array(
'Foreach: {foreach $list as $e} {$e}, {foreachelse} {continue} {/foreach} end',
'Fenom\Error\CompileException',
"Improper usage of the tag {continue}"
),
);
}
@ -552,8 +844,12 @@ class TemplateTest extends TestCase
array('{if 0}none{/if} literal:{ignore} {$a} {/ignore} end', $a, 'literal: {$a} end'),
array('{if 0}none{/if} literal: { $a} end', $a, 'literal: { $a} end'),
array('{if 0}none{/if} literal: { $a}{$a}{ $a} end', $a, 'literal: { $a}lit. A{ $a} end'),
array('{if 0}none{/if} literal: {
$a} end', $a, 'literal: { $a} end'),
array(
'{if 0}none{/if} literal: {
$a} end',
$a,
'literal: { $a} end'
),
array('{if 0}none{/if}literal: function () { return 1; } end', $a, 'literal: function () { return 1; } end')
);
}
@ -592,9 +888,21 @@ class TemplateTest extends TestCase
public static function providerSwitchInvalid()
{
return array(
array('Switch: {switch}{case 1} one {/switch} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Switch: {switch 1}{case} one{/switch} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Switch: {switch 1}{case $var} one {/switch} end', 'Fenom\Error\CompileException', "Unexpected token '\$var' in expression"),
array(
'Switch: {switch}{case 1} one {/switch} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array(
'Switch: {switch 1}{case} one{/switch} end',
'Fenom\Error\CompileException',
"Unexpected end of expression"
),
array(
'Switch: {switch 1}{case $var} one {/switch} end',
'Fenom\Error\CompileException',
"Unexpected token '\$var' in expression"
),
);
}
@ -626,16 +934,32 @@ class TemplateTest extends TestCase
array('For: {for $a=$c step=$s to=$m} $a: {$a}, {/for} end', $a, 'For: $a: 1, $a: 2, $a: 3, end'),
array('For: {for $a=-1 step=-max(1,2) to=-5} $a: {$a}, {/for} end', $a, 'For: $a: -1, $a: -3, $a: -5, end'),
array('For: {for $a=4 step=2 to=10} $a: {$a}, {break} break {/for} end', $a, 'For: $a: 4, end'),
array('For: {for $a=4 step=2 to=8} $a: {$a}, {continue} continue {/for} end',
$a, 'For: $a: 4, $a: 6, $a: 8, end'),
array('For: {for $a=4 step=2 to=8 index=$i} $a{$i}: {$a}, {/for} end', $a, 'For: $a0: 4, $a1: 6, $a2: 8, end'),
array('For: {for $a=4 step=2 to=8 index=$i first=$f} {if $f}first{/if} $a{$i}: {$a}, {/for} end',
$a, 'For: first $a0: 4, $a1: 6, $a2: 8, end'),
array('For: {for $a=4 step=2 to=8 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {/for} end',
$a, 'For: first $a0: 4, $a1: 6, $a2: 8, last end'),
array(
'For: {for $a=4 step=2 to=8} $a: {$a}, {continue} continue {/for} end',
$a,
'For: $a: 4, $a: 6, $a: 8, end'
),
array(
'For: {for $a=4 step=2 to=8 index=$i} $a{$i}: {$a}, {/for} end',
$a,
'For: $a0: 4, $a1: 6, $a2: 8, end'
),
array(
'For: {for $a=4 step=2 to=8 index=$i first=$f} {if $f}first{/if} $a{$i}: {$a}, {/for} end',
$a,
'For: first $a0: 4, $a1: 6, $a2: 8, end'
),
array(
'For: {for $a=4 step=2 to=8 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {/for} end',
$a,
'For: first $a0: 4, $a1: 6, $a2: 8, last end'
),
array('For: {for $a=1 to=-1 } $a: {$a}, {forelse} empty {/for} end', $a, 'For: empty end'),
array('For: {for $a=1 to=-1 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {forelse} empty {/for} end',
$a, 'For: empty end'),
array(
'For: {for $a=1 to=-1 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {forelse} empty {/for} end',
$a,
'For: empty end'
),
);
}
@ -645,27 +969,79 @@ class TemplateTest extends TestCase
array('For: {for} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('For: {for $a=} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('For: {for $a+1=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token '+'"),
array('For: {for max($a,$b)=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token '='"),
array(
'For: {for max($a,$b)=3 to=6} block1 {/for} end',
'Fenom\Error\CompileException',
"Unexpected token '='"
),
array('For: {for to=6 $a=3} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'to'"),
array('For: {for index=$i $a=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'index'"),
array('For: {for first=$i $a=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'first'"),
array('For: {for last=$i $a=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'last'"),
array('For: {for $a=4 to=6 unk=4} block1 {/for} end', 'Fenom\Error\CompileException', "Unknown parameter 'unk'"),
array('For: {for $a=4 to=6 step=0} block1 {/for} end', 'Fenom\Error\CompileException', "Invalid step value"),
array('For: {for $a=4 to=6} $a: {$a}, {forelse} {break} {/for} end', 'Fenom\Error\CompileException', "Improper usage of the tag {break}"),
array('For: {for $a=4 to=6} $a: {$a}, {forelse} {continue} {/for} end', 'Fenom\Error\CompileException', "Improper usage of the tag {continue}"),
array(
'For: {for index=$i $a=3 to=6} block1 {/for} end',
'Fenom\Error\CompileException',
"Unexpected token 'index'"
),
array(
'For: {for first=$i $a=3 to=6} block1 {/for} end',
'Fenom\Error\CompileException',
"Unexpected token 'first'"
),
array(
'For: {for last=$i $a=3 to=6} block1 {/for} end',
'Fenom\Error\CompileException',
"Unexpected token 'last'"
),
array(
'For: {for $a=4 to=6 unk=4} block1 {/for} end',
'Fenom\Error\CompileException',
"Unknown parameter 'unk'"
),
array(
'For: {for $a=4 to=6 step=0} block1 {/for} end',
'Fenom\Error\CompileException',
"Invalid step value"
),
array(
'For: {for $a=4 to=6} $a: {$a}, {forelse} {break} {/for} end',
'Fenom\Error\CompileException',
"Improper usage of the tag {break}"
),
array(
'For: {for $a=4 to=6} $a: {$a}, {forelse} {continue} {/for} end',
'Fenom\Error\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', '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: {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')"),
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')"
),
array('Layers: {/switch} end', 'Fenom\Error\CompileException', "Unexpected closing of the tag 'switch'"),
array('Layers: {if 1} end', 'Fenom\Error\CompileException', "Unclosed tag: {if}"),
);
@ -676,10 +1052,22 @@ class TemplateTest extends TestCase
return array(
array('{extends file="parent.tpl"}{block name="bk1"} block1 {/block}', "Template extended by block1"),
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 file="parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage', "Template multi-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"),
array(
'{extends "parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage',
"Template extended by block1"
),
array(
'{extends file="parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage',
"Template multi-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"
),
);
}
@ -752,12 +1140,10 @@ class TemplateTest extends TestCase
array('{if $one in "qwertyuiop"} block1 {else} block2 {/if}', 'block2'),
array('{if $one not in "qwertyuiop 1"} block1 {else} block2 {/if}', 'block2'),
array('{if $one not in "qwertyuiop"} block1 {else}v block2 {/if}', 'block1'),
array('{if $one in [1, 2, 3]} block1 {else} block2 {/if}', 'block1'),
array('{if $one in list [1, 2, 3]} block1 {else} block2 {/if}', 'block1'),
array('{if $one in ["one", "two", "three"]} block1 {else} block2 {/if}', 'block2'),
array('{if $one in keys [1 => "one", 2 => "two", 3 => "three"]} block1 {else} block2 {/if}', 'block1'),
array('{if $one in $two} block1 {else} block2 {/if}', 'block2'),
);
}
@ -799,11 +1185,9 @@ class TemplateTest extends TestCase
array('{$.const.MY}', ''),
array('{$.version}', Fenom::VERSION),
array('{"string"|append:"_":$.get.one}', 'string_get1'),
array('{$.get.one?}', '1'),
array('{$.get.one is set}', '1'),
array('{$.get.two is empty}', '1'),
array('{$.version}', Fenom::VERSION),
array('{$.tpl?}', '1'),
array('{$.tpl.name}', 'runtime.tpl'),
@ -833,22 +1217,61 @@ class TemplateTest extends TestCase
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),
array('{12 + Fenom\TemplateTest::multi(3,4)}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS),
array('{12 + 3|Fenom\TemplateTest::multi:4}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS),
array('{Fenom\TemplateTest::multi_invalid x=3 y=4}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'),
array('{Fenom\TemplateTest::multi_invalid(3,4)}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'),
array('{12 + Fenom\TemplateTest::multi_invalid(3,4)}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'),
array('{12 + 3|Fenom\TemplateTest::multi_invalid:4}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'),
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
),
array(
'{12 + Fenom\TemplateTest::multi(3,4)}',
'Fenom\Error\SecurityException',
"Static methods are disabled",
Fenom::DENY_STATICS
),
array(
'{12 + 3|Fenom\TemplateTest::multi:4}',
'Fenom\Error\SecurityException',
"Static methods are disabled",
Fenom::DENY_STATICS
),
array(
'{Fenom\TemplateTest::multi_invalid x=3 y=4}',
'Fenom\Error\CompileException',
'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'
),
array(
'{Fenom\TemplateTest::multi_invalid(3,4)}',
'Fenom\Error\CompileException',
'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'
),
array(
'{12 + Fenom\TemplateTest::multi_invalid(3,4)}',
'Fenom\Error\CompileException',
'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'
),
array(
'{12 + 3|Fenom\TemplateTest::multi_invalid:4}',
'Fenom\Error\CompileException',
'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'
),
);
}
public function _testSandbox()
{
try {
var_dump($this->fenom->setOptions(0)->compileCode("{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}")->getBody());
var_dump(
$this->fenom->setOptions(0)->compileCode(
"{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}"
)->getBody()
);
} catch (\Exception $e) {
print_r($e->getMessage() . "\n" . $e->getTraceAsString());
while ($e->getPrevious()) {

View File

@ -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(
$this->assertSame(
array(
T_STRING,
'please',
' ',
1,
'T_STRING'
), $tokens->curr);
),
$tokens->curr
);
$this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING));
$tokens->next();

View File

@ -1,8 +1,5 @@
<?php
use Fenom\Render,
Fenom\Provider as FS;
class FenomTest extends \Fenom\TestCase
{
@ -20,6 +17,7 @@ class FenomTest extends \Fenom\TestCase
);
}
public function testCreating()
{
$time = $this->tpl('temp.tpl', 'Template 1 a');
@ -36,7 +34,11 @@ 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);
$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(
$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) {
$this->fenom->addPreFilter(
function ($tpl, $src) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "== $src ==";
});
}
);
$this->fenom->addPostFilter(function ($tpl, $code) use ($punit) {
$this->fenom->addPostFilter(
function ($tpl, $code) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "+++ $code +++";
});
}
);
$this->fenom->addFilter(function ($tpl, $text) use ($punit) {
$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())
);
}
/**
@ -172,11 +222,13 @@ class FenomTest extends \Fenom\TestCase
{
$tags = array();
$punit = $this;
$this->fenom->addTagFilter(function ($text, $tpl) use (&$tags, $punit) {
$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);
}
}

View File

@ -0,0 +1 @@
{foreach $items as $key => $value}{$key}:{$value}{/foreach}