diff --git a/CHANGELOG.md b/CHANGELOG.md index f18aa0c..c133f88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ Changelog ========= +### 1.4.4 + +- Bug fixes +- Tests++ + +### 1.4.3 + +- Bug fixes + ### 1.4.2 (2013-09-06) - Added check the cache directory to record diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index 84cc0ce..75ca77b 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -71,7 +71,7 @@ class Compiler } $inc = $tpl->getStorage()->compile($name, false); $tpl->addDepend($inc); - return '?>' . $tpl->getBody() . '' . $inc->getBody() . 'tpl->escape = $scope["escape"]; } - - /** - * Unset present variables - * - * @param Tokenizer $tokens - * @param Template $tpl - * @return string - * @throws InvalidUsageException - */ - public static function tagUnset(Tokenizer $tokens, Template $tpl) - { - $vars = array(); - while ($tokens->valid()) { - $vars[] = $tpl->parseVariable($tokens); - } - if (!$vars) { - throw new InvalidUsageException("Unset must accept variable(s)"); - } - return 'unset(' . implode(', ', $vars) . ')'; - } } diff --git a/src/Fenom/Tokenizer.php b/src/Fenom/Tokenizer.php index 74765cf..9e97240 100644 --- a/src/Fenom/Tokenizer.php +++ b/src/Fenom/Tokenizer.php @@ -304,26 +304,6 @@ class Tokenizer return $this->next ? $this->next[1] == $token : false; } - /** - * Return substring. This method doesn't move pointer. - * @param int $offset - * @param int $limit - * @return string - */ - public function getSubstr($offset, $limit = 0) - { - $str = ''; - if (!$limit) { - $limit = $this->_max; - } else { - $limit += $offset; - } - for ($i = $offset; $i <= $limit; $i++) { - $str .= $this->tokens[$i][1] . $this->tokens[$i][2]; - } - return $str; - } - /** * Return token and move pointer * @return mixed diff --git a/tests/TestCase.php b/tests/TestCase.php index f10c152..05a6624 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -103,6 +103,7 @@ class TestCase extends \PHPUnit_Framework_TestCase mkdir(FENOM_RESOURCES . '/template/' . $dir, 0777, true); } file_put_contents(FENOM_RESOURCES . '/template/' . $name, $code); + return filemtime(FENOM_RESOURCES . '/template/' . $name); } /** diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index be1b236..7dbd658 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -229,6 +229,7 @@ class TemplateTest extends TestCase $result4 = 'Include Welcome, Flame (flame@dev.null) template'; return array( array('Include {include "welcome.tpl"} template', $a, $result), + array('Include {include "welcome.tpl"} template', $a, $result, Fenom::FORCE_INCLUDE), array('Include {include $tpl} template', $a, $result), array('Include {include "$tpl"} template', $a, $result), array('Include {include "{$tpl}"} template', $a, $result), @@ -238,7 +239,9 @@ class TemplateTest extends TestCase array('Include {include "wel{$fragment}.tpl"} template', $a, $result), array('Include {include "wel{$pr_fragment|lower}.tpl"} template', $a, $result), 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), ); @@ -252,6 +255,40 @@ class TemplateTest extends TestCase ); } + public static function providerInsert() + { + $a = array( + "name" => "welcome", + "tpl" => "welcome.tpl", + "fragment" => "come", + "pr_fragment" => "Come", + "pr_name" => "Welcome", + "username" => "Master", + "email" => "dev@null.net" + ); + $result = 'Include Welcome, Master (dev@null.net) template'; + return array( + array('Include {insert "welcome.tpl"} template', $a, $result), + array("Include {insert 'welcome.tpl'} template", $a, $result), + ); + } + + public static function providerInsertInvalid() + { + return array( + array('Include {insert} template', 'Fenom\Error\CompileException', "Unexpected end of expression"), + array('Include {insert another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Template another not found"), + 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'"), + ); + } + public static function providerIf() { $a = array( @@ -783,9 +820,9 @@ class TemplateTest extends TestCase * @group include * @dataProvider providerInclude */ - public function testInclude($code, $vars, $result) + public function testInclude($code, $vars, $result, $options = 0) { - $this->exec($code, $vars, $result); + $this->exec($code, $vars, $result, $options); } /** @@ -796,6 +833,24 @@ class TemplateTest extends TestCase $this->execError($code, $exception, $message, $options); } + /** + * @group insert + * @dataProvider providerInsert + */ + public function testInsert($code, $vars, $result) + { + $this->exec($code, $vars, $result); + } + + /** + * @group insert + * @dataProvider providerInsertInvalid + */ + public function testInsertInvalid($code, $exception, $message, $options = 0) + { + $this->execError($code, $exception, $message, $options); + } + /** * @dataProvider providerIf * @group test-if diff --git a/tests/cases/Fenom/TokenizerTest.php b/tests/cases/Fenom/TokenizerTest.php index d5a3512..ff656af 100644 --- a/tests/cases/Fenom/TokenizerTest.php +++ b/tests/cases/Fenom/TokenizerTest.php @@ -7,12 +7,16 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase public function testTokens() { - $code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}'; + $code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}'; $tokens = new Tokenizer($code); + $this->assertSame($tokens, $tokens->back()); $this->assertSame(T_STRING, $tokens->key()); $this->assertSame("hello", $tokens->current()); + $this->assertSame(1, $tokens->getLine()); + $this->assertTrue($tokens->isNext(",")); + $this->assertFalse($tokens->isNext("=")); $this->assertFalse($tokens->isNext(T_STRING)); $this->assertFalse($tokens->isNext($tokens::MACRO_UNARY)); @@ -23,6 +27,13 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase $this->assertSame(",", $tokens->getNext()); $this->assertSame(",", $tokens->key()); $this->assertSame("please", $tokens->getNext(T_STRING)); + $this->assertSame(array( + T_STRING, + 'please', + ' ', + 1, + 'T_STRING' + ), $tokens->curr); $this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING)); $tokens->next(); @@ -46,6 +57,9 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase $tokens->next(); $tokens->next(); $this->assertSame("+", $tokens->getNext($tokens::MACRO_BINARY)); + + $this->assertSame('sin($x)+tan($x*$t)', $tokens->getSnippetAsString(-4, 6)); + $this->assertSame($code, $tokens->getSnippetAsString(-100, 100)); } public function testSkip() @@ -62,7 +76,14 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase } $this->assertTrue($tokens->valid()); $this->assertSame("3", $tokens->current()); + $this->assertSame(T_LNUMBER, $tokens->key()); + $this->assertSame($tokens, $tokens->next()); $tokens->next(); + $this->assertSame("double", $tokens->getAndNext()); + $this->assertSame(")", $tokens->current()); + $this->assertTrue($tokens->isLast()); + $this->assertSame($tokens, $tokens->next()); } + } diff --git a/tests/cases/FenomTest.php b/tests/cases/FenomTest.php index e436088..1810718 100644 --- a/tests/cases/FenomTest.php +++ b/tests/cases/FenomTest.php @@ -10,7 +10,7 @@ class FenomTest extends \Fenom\TestCase { return array( array("disable_methods", Fenom::DENY_METHODS), - array("disable_native_funcs", Fenom::DENY_INLINE_FUNCS), + array("disable_native_funcs", Fenom::DENY_NATIVE_FUNCS), array("disable_cache", Fenom::DISABLE_CACHE), array("force_compile", Fenom::FORCE_COMPILE), array("auto_reload", Fenom::AUTO_RELOAD), @@ -20,6 +20,16 @@ class FenomTest extends \Fenom\TestCase ); } + public function testCreating() { + $time = $this->tpl('template1.tpl', 'Template 1 a'); + Fenom::factory(FENOM_RESOURCES . '/template', FENOM_RESOURCES . '/compile'); + $fenom = new Fenom($provider =new \Fenom\Provider(FENOM_RESOURCES . '/template'), FENOM_RESOURCES . '/compile', Fenom::AUTO_ESCAPE); + $this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('template1.tpl')); + $this->assertSame($provider, $tpl->getProvider()); + $this->assertSame('template1.tpl', $tpl->getBaseName()); + $this->assertSame($time, $tpl->getTime()); + } + public function testCompileFile() { $a = array( @@ -43,14 +53,19 @@ class FenomTest extends \Fenom\TestCase $this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array())); } + /** + * @group testCheckMTime + */ public function testCheckMTime() { $this->fenom->setOptions(Fenom::FORCE_COMPILE); $this->tpl('custom.tpl', 'Custom template'); $this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array())); - - sleep(1); + $tpl = $this->fenom->getTemplate('custom.tpl'); + $this->assertTrue($tpl->isValid()); + usleep(1.5e6); $this->tpl('custom.tpl', 'Custom template (new)'); + $this->assertFalse($tpl->isValid()); $this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array())); }