* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: MetalMacroTest.php 888 2010-06-08 09:48:33Z kornel $ * @link http://phptal.org/ */ class MetalMacroTest extends PHPTAL_TestCase { function testSimple() { $tpl = $this->newPHPTAL('input/metal-macro.01.html'); $res = normalize_html($tpl->execute()); $exp = normalize_html_file('output/metal-macro.01.html'); $this->assertEquals($exp, $res); } function testExternalMacro() { $tpl = $this->newPHPTAL('input/metal-macro.02.html'); $res = normalize_html($tpl->execute()); $exp = normalize_html_file('output/metal-macro.02.html'); $this->assertEquals($exp, $res); } function testBlock() { $tpl = $this->newPHPTAL('input/metal-macro.03.html'); $res = normalize_html($tpl->execute()); $exp = normalize_html_file('output/metal-macro.03.html'); $this->assertEquals($exp, $res); } function testMacroInsideMacro() { $tpl = $this->newPHPTAL('input/metal-macro.04.html'); $res = normalize_html($tpl->execute()); $exp = normalize_html_file('output/metal-macro.04.html'); $this->assertEquals($exp, $res); } function testEvaluatedMacroName() { $call = new stdClass(); $call->first = 1; $call->second = 2; $tpl = $this->newPHPTAL('input/metal-macro.05.html'); $tpl->call = $call; $res = normalize_html($tpl->execute()); $exp = normalize_html_file('output/metal-macro.05.html'); $this->assertEquals($exp, $res); } function testEvaluatedMacroNameTalesPHP() { $call = new stdClass(); $call->first = 1; $call->second = 2; $tpl = $this->newPHPTAL('input/metal-macro.06.html'); $tpl->call = $call; $res = normalize_html($tpl->execute()); $exp = normalize_html_file('output/metal-macro.06.html'); $this->assertEquals($exp, $res); } function testInheritedMacroSlots() { $tpl = $this->newPHPTAL('input/metal-macro.07.html'); $res = normalize_html($tpl->execute()); $exp = normalize_html_file('output/metal-macro.07.html'); $this->assertEquals($exp, $res); } /** * @expectedException PHPTAL_ParserException */ function testBadMacroNameException() { $tpl = $this->newPHPTAL('input/metal-macro.08.html'); $res = $tpl->execute(); $this->fail('Bad macro name exception not thrown'); } /** * @expectedException PHPTAL_MacroMissingException */ function testExternalMacroMissingException() { $tpl = $this->newPHPTAL(); $tpl->setSource(''); $res = $tpl->execute(); $this->fail('Bad macro name exception not thrown'); } /** * @expectedException PHPTAL_MacroMissingException */ function testMacroMissingException() { $tpl = $this->newPHPTAL(); $tpl->setSource(''); $res = $tpl->execute(); $this->fail('Bad macro name exception not thrown'); } function testMixedCallerDefiner() { $tpl = $this->newPHPTAL(); $tpl->defined_later_var = 'defined_later'; $tpl->ok_var = '??'; // fallback in case test fails $tpl->setSource(''); $res = $tpl->execute(); $this->assertEquals('Call OK OK', trim(preg_replace('/\s+/', ' ', $res))); } /** * @expectedException PHPTAL_Exception */ function testMacroRedefinitionIsGraceful() { $tpl = $this->newPHPTAL(); $tpl->setSource( '

bar

'); $tpl->execute(); $this->fail("Allowed duplicate macro"); } function testSameMacroCanBeDefinedInDifferentTemplates() { $tpl = $this->newPHPTAL(); $tpl->setSource('1'); $tpl->execute(); $tpl = $this->newPHPTAL(); $tpl->setSource('2'); $tpl->execute(); } /** * @expectedException PHPTAL_ParserException */ function testExternalTemplateThrowsError() { $tpl = $this->newPHPTAL(); $tpl->setSource(''); $tpl->execute(); } function testOnErrorCapturesErorrInExternalMacro() { $tpl = $this->newPHPTAL(); $tpl->setSource(''); $this->assertEquals('ok', $tpl->execute()); } function testGlobalDefineInExternalMacro() { $tpl = $this->newPHPTAL(); $tpl->setSource(' ${test} '); $this->assertEquals('ok', trim($tpl->execute())); } }