require_once 'PHPTAL.php'; class Renderer_PhpTalTest extends PHPUnit2_Framework_TestCase { protected $template; public function test_simple() { $template = self::createTemplate('test.phpTal.simple.tpl'); $template->set('name', 'sven'); $expected = '
sven
'; $result = self::stripWhitespace($template->execute()); $this->assertSameVerbose($expected, $result); } public function test_form_1() { $form = self::createForm(array('name' => array('type' => 'String'))); $template = $form->renderForm(array( 'tmplDir' => 'Renderer/res', 'tmplFile' => 'test.phpTal.form.tpl', )); $expected = ''; $result = $template->execute(); $result = self::stripAction($result); $result = self::stripElementId($result); $result = self::stripWhitespace($result); $this->assertSameVerbose($expected, $result); } public function test_form_2() { $template = self::createTemplate('test.phpTal.form.tpl'); $form = self::createForm(array('name' => array('type' => 'String'))); $template = $form->renderForm($template); $expected = ''; $result = $template->execute(); $result = self::stripAction($result); $result = self::stripElementId($result); $result = self::stripWhitespace($result); $this->assertSameVerbose($expected, $result); } static protected function createTemplate($name) { $template = new PHPTal(); $template->setTemplate($name); $template->setTemplateRepository('Renderer/res'); return $template; } protected function createForm($definition) { $renderer = patForms::createRenderer("PhpTal", $args); $form = patForms::createForm($definition, array('name' => 'test')); $form->setRenderer($renderer); $form->setSubmitted(true); return $form; } static protected function stripAction($str) { return preg_replace('!action="[^\"]*"!', '', $str); } protected function stripElementId($element) { return preg_replace('!id="pfo[a-zA-Z0-9]*" ?!', '', $element); } static protected function stripWhitespace($str) { return preg_replace('|[\n\r\t]*|', '', $str); } protected function assertSameVerbose($expected, $result) { try { $this->assertTrue($expected == $result); } catch(Exception $e) { $lf = php_sapi_name() == 'cli' ? "\n" : '