* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: TalesTest.php 997 2010-10-23 13:26:32Z kornel $ * @link http://phptal.org/ */ 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::compileToPHPExpressions($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::compileToPHPExpressions($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::compileToPHPExpressions($src); $this->assertEquals("!(foo())", $res); } function testNotVar() { $src = "not:foo"; $res = PHPTAL_Php_TalesInternal::compileToPHPExpressions($src); $this->assertEquals('!($ctx->foo)', $res); } function testNotPath() { $src = "not:foo/bar/baz"; $res = PHPTAL_Php_TalesInternal::compileToPHPExpressions($src); $this->assertEquals('!($ctx->path($ctx->foo, \'bar/baz\'))', $res); } function testTrue() { $tpl = $this->newPHPTAL('input/tales-true.html'); $tpl->isNotTrue = false; $tpl->isTrue = true; $res = $tpl->execute(); $this->assertEquals(normalize_html_file('output/tales-true.html'), normalize_html($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 testTaleNeverReturnsArray() { $this->assertType('string', phptal_tale('foo | bar | baz | nothing')); } function testTalesReturnsArray() { $this->assertType('array', phptal_tales('foo | bar | baz | nothing')); } function testInterpolate1() { $this->assertEquals('$ctx->{$ctx->path($ctx->some, \'path\')}', PHPTAL_Php_TalesInternal::compileToPHPExpressions('${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::compileToPHPExpressions('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::compileToPHPExpressions('_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(); } /** * @expectedException PHPTAL_ParserException */ function testForbidsStatementsInCustomModifiers() { $tpl = $this->newPHPTAL(); $tpl->setSource('')->execute(); } /** * @expectedException PHPTAL_ParserException */ function testThrowsInvalidPath() { phptal_tales("I am not valid expression"); } function testThrowsUnknownModifier() { try { phptal_tales('testidontexist:foo'); $this->fail(); } catch(PHPTAL_UnknownModifierException $e) { $this->assertEquals('testidontexist', $e->getModifierName()); } } function testNamespaceFunction() { if (version_compare(PHP_VERSION, '5.3', '<')) $this->markTestSkipped(); $this->assertEquals('\strlen($ctx->x)', phptal_tales('php:\strlen(x)')); $this->assertEquals('my\len($ctx->x)', phptal_tales('php:my\len(x)')); $this->assertEquals('my\subns\len($ctx->x)', phptal_tales('php:my\subns\len(x)')); } function testNamespaceClass() { if (version_compare(PHP_VERSION, '5.3', '<')) $this->markTestSkipped(); $this->assertEquals('\Foo::strlen($ctx->x)', phptal_tales('php:\Foo::strlen(x)')); $this->assertEquals('My\Foo::strlen($ctx->x)', phptal_tales('php:My\Foo::strlen(x)')); } function testNamespaceConstant() { if (version_compare(PHP_VERSION, '5.3', '<')) $this->markTestSkipped(); $this->assertEquals('My\Foo::TAU', phptal_tales('php:My\Foo::TAU')); $this->assertEquals('$ctx->date_filter->isFilterApplied(\My\Foo::TODAY)', phptal_tales("php: date_filter.isFilterApplied(\My\Foo::TODAY)")); } } function phptal_tales_testmodifier($expr, $nothrow) { return 'print("test");'; }