* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: TalDefineTest.php 888 2010-06-08 09:48:33Z kornel $ * @link http://phptal.org/ */ class DummyDefinePhpNode extends PHPTAL_Dom_Element { function __construct() {} function generateCode(PHPTAL_Php_CodeWriter $codewriter) {} } class TalDefineTest extends PHPTAL_TestCase { function testExpressionParser() { $att = new PHPTAL_Php_Attribute_Tal_Define(new DummyDefinePhpNode(), 'a b'); list($defineScope, $defineVar, $expression) = $att->parseExpression('local a_234z b'); $this->assertEquals('local', $defineScope); $this->assertEquals('a_234z', $defineVar); $this->assertEquals('b', $expression); list($defineScope, $defineVar, $expression) = $att->parseExpression('global a_234z b'); $this->assertEquals('global', $defineScope); $this->assertEquals('a_234z', $defineVar); $this->assertEquals('b', $expression); list($defineScope, $defineVar, $expression) = $att->parseExpression('a_234Z b'); $this->assertEquals(false, $defineScope); $this->assertEquals('a_234Z', $defineVar); $this->assertEquals('b', $expression); list($defineScope, $defineVar, $expression) = $att->parseExpression('a'); $this->assertEquals(false, $defineScope); $this->assertEquals('a', $defineVar); $this->assertEquals(null, $expression); list($defineScope, $defineVar, $expression) = $att->parseExpression('global a string: foo; bar; baz'); $this->assertEquals('global', $defineScope); $this->assertEquals('a', $defineVar); $this->assertEquals('string: foo; bar; baz', $expression); list($defineScope, $defineVar, $expression) = $att->parseExpression('foo this != other'); $this->assertEquals(false, $defineScope); $this->assertEquals('foo', $defineVar); $this->assertEquals('this != other', $expression); list($defineScope, $defineVar, $expression) = $att->parseExpression('x exists: a | not: b | path: c | 128'); $this->assertEquals(false, $defineScope); $this->assertEquals('x', $defineVar); $this->assertEquals('exists: a | not: b | path: c | 128', $expression); } function testMulti() { $tpl = $this->newPHPTAL('input/tal-define.01.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.01.html'); $this->assertEquals($exp, $res); } function testBuffered() { $tpl = $this->newPHPTAL('input/tal-define.02.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.02.html'); $this->assertEquals($exp, $res); } function testMultiChained() { $tpl = $this->newPHPTAL('input/tal-define.03.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.03.html'); $this->assertEquals($exp, $res); } function testDefineZero() { $tpl = $this->newPHPTAL('input/tal-define.04.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.04.html'); $this->assertEquals($exp, $res); } function testDefineZeroTalesPHP() { $tpl = $this->newPHPTAL('input/tal-define.05.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.05.html'); $this->assertEquals($exp, $res); } function testDefineInMacro() { $tpl = $this->newPHPTAL('input/tal-define.06.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.06.html'); $this->assertEquals($exp, $res); } function testDefineDoNotStealOutput() { $tpl = $this->newPHPTAL('input/tal-define.07.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.07.html'); $this->assertEquals($exp, $res); } function testDefineWithRepeatAndContent() { $tpl = $this->newPHPTAL('input/tal-define.08.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.08.html'); $this->assertEquals($exp, $res); } function testDefineWithUseMacro() { $tpl = $this->newPHPTAL('input/tal-define.09.html'); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.09.html'); $this->assertEquals($exp, $res); } function testDefineAndPrint() { $tpl = $this->newPHPTAL('input/tal-define.10.html'); $tpl->fname = 'Roger'; $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.10.html'); $this->assertEquals($exp, $res); } function testDefineContent() { $tpl = $this->newPHPTAL('input/tal-define.11.html'); $tpl->setOutputMode(PHPTAL::XML); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.11.html'); $this->assertEquals($exp, $res); } function testDefineAndAttributes() { $tpl = $this->newPHPTAL('input/tal-define.12.html'); $tpl->setOutputMode(PHPTAL::XML); $res = $tpl->execute(); $res = normalize_html($res); $exp = normalize_html_file('output/tal-define.12.html'); $this->assertEquals($exp, $res); } function testDefineGlobal() { $exp = normalize_html_file('output/tal-define.13.html'); $tpl = $this->newPHPTAL('input/tal-define.13.html'); $res = normalize_html($tpl->execute()); $this->assertEquals($exp, $res); } function testDefineAlter() { $exp = normalize_html_file('output/tal-define.14.html'); $tpl = $this->newPHPTAL('input/tal-define.14.html'); $res = normalize_html($tpl->execute()); $this->assertEquals($exp, $res); } function testDefineSemicolon() { $tpl = $this->newPHPTAL(); $tpl->setSource('

${one}-${two}-${three}

'); $this->assertEquals('

;-;;-;;;

', $tpl->execute()); } function testEmpty() { $tal = $this->newPHPTAL(); $tal->setSource('
'); $tal->relative = array('book'=>1); $this->assertEquals($tal->execute(), '
'); } function testGlobalDefineEmptySpan() { $tpl = $this->newPHPTAL(); $tpl->setSource('
${x}
'); $res = normalize_html($tpl->execute()); $this->assertEquals(normalize_html('
ok
'), $res); } function testGlobalDefineEmptySpan2() { $tpl = $this->newPHPTAL(); $tpl->setSource('
${x}
'); $res = normalize_html($tpl->execute()); $this->assertEquals(normalize_html('
ok
'), $res); } function testGlobalDefineNonEmptySpan() { $tpl = $this->newPHPTAL(); $tpl->setOutputMode(PHPTAL::XML); $tpl->setSource('
${x}
'); $res = normalize_html($tpl->execute()); $this->assertEquals(normalize_html('
ok
'), $res); } function testGlobalDefineNonEmptySpan2() { $tpl = $this->newPHPTAL(); $tpl->setOutputMode(PHPTAL::XML); $tpl->setSource('
${x}
'); $res = normalize_html($tpl->execute()); $this->assertEquals(normalize_html('
ok
'), $res); } function testDefineTALESInterpolated() { $tpl = $this->newPHPTAL(); $tpl->varvar = 'ok'; $tpl->varname = 'varvar'; $tpl->setSource('
${test}
'); $this->assertEquals('
ok
', $tpl->execute()); } function testDefinePHPInterpolated() { $tpl = $this->newPHPTAL(); $tpl->varvar = 'ok'; $tpl->varname = 'varvar'; $tpl->setSource('
${test}
'); $this->assertEquals('
ok
', $tpl->execute()); } const VARNAME = 'varvar'; function testDefinePHPConstInterpolated() { $tpl = $this->newPHPTAL(); $tpl->varvar = 'ok'; $tpl->varname = 'varvar'; $tpl->setSource('
${test}
'); $this->assertEquals('
ok
', $tpl->execute()); } function testRedefineSelf() { $tpl = $this->newPHPTAL(); $tpl->label = 'label var'; $tpl->fail = 'not an array'; $tpl->setSource(''); $this->assertEquals('label var', $tpl->execute()); } function testRedefineSelf2() { $tpl = $this->newPHPTAL(); $tpl->label = 'label var'; $tpl->fail = 'not an array'; $tpl->setSource(''); $this->assertEquals('label var', $tpl->execute()); } /** * @expectedException PHPTAL_Exception */ function testRejectsInvalidExpression() { $tpl = $this->newPHPTAL(); $tpl->setSource(''); $tpl->execute(); } function testHasRealContent() { $tpl = $this->newPHPTAL(); $tpl->setSource(' test '); $tpl->execute(); } function testHasRealCDATAContent() { $tpl = $this->newPHPTAL(); $tpl->setSource(''); $tpl->execute(); } function testDefineAndAttributesOnSameElement() { $tpl = $this->newPHPTAL(); $tpl->team = 'zzz'; $tpl->row = 'zzz'; $tpl->event_name = 'zzz'; $tpl->setSource(' ${event/player/surname} '); $tpl->execute(); } }