* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: TalesTest.php 657 2009-06-30 16:48:20Z kornel $ * @link http://phptal.org/ */ require_once dirname(__FILE__)."/config.php"; PHPTAL::setIncludePath(); require_once 'PHPTAL/Tales.php'; PHPTAL::restoreIncludePath(); function phptal_tales_custom($src,$nothrow) { return 'sprintf("%01.2f", '.PHPTAL_Php_TalesInternal::path($src, $nothrow).')'; } class MyTalesClass implements PHPTAL_Tales { public static function reverse($exp,$nothrow){ return 'strrev('.phptal_tales($exp,$nothrow).')'; } } class TalesTest extends PHPTAL_TestCase { function testString() { $src = 'string:foo bar baz'; $res = PHPTAL_Php_TalesInternal::compileToPHPStatements($src); $this->assertEquals("'foo bar baz'", $res); $src = "'foo bar baz'"; $res = phptal_tales($src); $this->assertEquals("'foo bar baz'", $res); } function testPhp() { $src = 'php: foo.x[10].doBar()'; $res = PHPTAL_Php_TalesInternal::compileToPHPStatements($src); $this->assertEquals('$ctx->foo->x[10]->doBar()', $res); } function testPath() { $src = 'foo/x/y'; $res = phptal_tales($src); $this->assertEquals("\$ctx->path(\$ctx->foo, 'x/y')", $res); } function testNot() { $src = "not: php: foo()"; $res = PHPTAL_Php_TalesInternal::compileToPHPStatements($src); $this->assertEquals("!(foo())", $res); } function testTrue() { $tpl = $this->newPHPTAL('input/tales-true.html'); $tpl->isNotTrue = false; $tpl->isTrue = true; $res = $tpl->execute(); $this->assertEquals(trim_file('output/tales-true.html'), trim_string($res)); } function testCustom() { $src = 'custom: some/path'; $this->assertEquals('sprintf("%01.2f", $ctx->path($ctx->some, \'path\'))', phptal_tales($src)); } function testCustomClass() { $src = 'MyTalesClass.reverse: some'; $this->assertEquals('strrev($ctx->some)', phptal_tales($src)); } function testInterpolate1() { $this->assertEquals('$ctx->{$ctx->path($ctx->some, \'path\')}',PHPTAL_Php_TalesInternal::compileToPHPStatements('${some/path}')); } function testInterpolate2() { $this->assertEquals('$ctx->path($ctx->{$ctx->path($ctx->some, \'path\')}, \'meh\')',phptal_tales('${some/path}/meh')); } function testInterpolate3() { $this->assertEquals('$ctx->path($ctx->meh, $ctx->path($ctx->some, \'path\'))',PHPTAL_Php_TalesInternal::compileToPHPStatements('meh/${some/path}')); } function testInterpolate4() { $this->assertEquals('$ctx->path($ctx->{$ctx->meh}, $ctx->blah)',phptal_tales('${meh}/${blah}')); } function testSuperglobals() { $this->assertEquals('$ctx->path($ctx->{\'_GET\'}, \'a\')',PHPTAL_Php_TalesInternal::compileToPHPStatements('_GET/a')); } function testInterpolatedPHP1() { $tpl = $this->newPHPTAL(); $tpl->setSource('
'); $this->assertEquals('
foobarb$$a$z
',$tpl->execute()); } function testInterpolatedTALES() { $tpl = $this->newPHPTAL(); $tpl->var = 'ba'; $tpl->setSource('
'); $this->assertEquals('
foobarbaz
',$tpl->execute()); } function testInterpolatedPHP2() { $tpl = $this->newPHPTAL(); $tpl->somearray = array(1=>9,9,9); $tpl->setSource('
'); $this->assertEquals('
1
2
3
',$tpl->execute()); } function testStringWithLongVarName() { $tpl = $this->newPHPTAL(); $tpl->aaaaaaaaaaaaaaaaaaaaa = 'ok'; $tpl->bbb = 'ok'; $tpl->setSource(''); $tpl->execute(); } }