root/trunk/tests/Renderer/PhpTalTest.php

Revision 279, 2.7 kB (checked in by sfuchs, 3 years ago)

added Renderer_Flexy, Renderer_Savant3, Renderer_Smarty (including unit tests)
fixes docblock in Renderer_PhpTal

Line 
1 <?
2     require_once 'PHPTAL.php';
3
4     class Renderer_PhpTalTest extends PHPUnit2_Framework_TestCase {
5
6         protected $template;
7
8         public function test_simple() {
9
10             $template = self::createTemplate('test.phpTal.simple.tpl');
11             $template->set('name', 'sven');
12
13             $expected = '<html><body><p>sven</p></body></html>';
14             $result = self::stripWhitespace($template->execute());
15             $this->assertSameVerbose($expected, $result);
16         }
17
18         public function test_form_1() {
19
20             $form = self::createForm(array('name' => array('type' => 'String')));
21             $template = $form->renderForm(array(
22                 'tmplDir' => 'Renderer/res',
23                 'tmplFile' => 'test.phpTal.form.tpl',
24             ));
25
26             $expected = '<form name="test" method="post" ><input name="name" type="text" value="" /></form>';
27             $result = $template->execute();
28
29             $result = self::stripAction($result);
30             $result = self::stripElementId($result);
31             $result = self::stripWhitespace($result);
32
33             $this->assertSameVerbose($expected, $result);
34         }
35
36         public function test_form_2() {
37
38             $template = self::createTemplate('test.phpTal.form.tpl');
39             $form = self::createForm(array('name' => array('type' => 'String')));
40             $template = $form->renderForm($template);
41
42             $expected = '<form name="test" method="post" ><input name="name" type="text" value="" /></form>';
43             $result = $template->execute();
44
45             $result = self::stripAction($result);
46             $result = self::stripElementId($result);
47             $result = self::stripWhitespace($result);
48
49             $this->assertSameVerbose($expected, $result);
50         }
51
52         static protected function createTemplate($name) {
53
54             $template = new PHPTal();
55             $template->setTemplate($name);
56             $template->setTemplateRepository('Renderer/res');
57
58             return $template;
59         }
60
61         protected function createForm($definition) {
62
63             $renderer = patForms::createRenderer("PhpTal", $args);
64
65             $form = patForms::createForm($definition, array('name' => 'test'));
66             $form->setRenderer($renderer);
67             $form->setSubmitted(true);
68
69             return $form;
70         }
71
72         static protected function stripAction($str) {
73
74             return preg_replace('!action="[^\"]*"!', '', $str);
75         }
76
77         protected function stripElementId($element) {
78
79             return preg_replace('!id="pfo[a-zA-Z0-9]*" ?!', '', $element);
80         }
81
82         static protected function stripWhitespace($str) {
83
84             return preg_replace('|[\n\r\t]*|', '', $str);
85         }
86
87         protected function assertSameVerbose($expected, $result) {
88
89             try {
90                 $this->assertTrue($expected == $result);
91             } catch(Exception $e) {
92                 $lf = php_sapi_name() == 'cli' ? "\n" : '<br>';
93                 echo $lf . $lf;
94                 echo 'expected was: ' . htmlentities($expected) . $lf;
95                 echo 'result was: ' . htmlentities($result) . $lf . $lf;
96                 throw($e);
97             }
98         }
99     }
100
101 ?>
Note: See TracBrowser for help on using the browser.