| 1 |
lars |
1 |
<?php
|
|
|
2 |
require_once 'HTML/Template/ITX.php';
|
|
|
3 |
require_once 'PHPUnit/Framework/TestCase.php';
|
|
|
4 |
|
|
|
5 |
require_once 'ITTest.php';
|
|
|
6 |
|
|
|
7 |
function _uppercaseCallback($ary)
|
|
|
8 |
{
|
|
|
9 |
return strtoupper($ary[0]);
|
|
|
10 |
}
|
|
|
11 |
|
|
|
12 |
class Callbacks
|
|
|
13 |
{
|
|
|
14 |
function _lowercaseCallback($ary)
|
|
|
15 |
{
|
|
|
16 |
return strtolower($ary[0]);
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
function _numberFormatCallback($float, $decimals)
|
|
|
20 |
{
|
|
|
21 |
return number_format($float, $decimals);
|
|
|
22 |
}
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
class ITXTest extends ITTest
|
|
|
26 |
{
|
|
|
27 |
function setUp()
|
|
|
28 |
{
|
|
|
29 |
$this->tpl = new HTML_Template_ITX(dirname(__FILE__) . '/templates');
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
function testPlaceholderExists()
|
|
|
33 |
{
|
|
|
34 |
$this->tpl->setTemplate('{var}');
|
|
|
35 |
$this->assertSame("__global__", $this->tpl->placeholderExists('var'), 'Existing placeholder \'var\' reported as nonexistant');
|
|
|
36 |
$this->assertSame("", $this->tpl->placeholderExists('foobar'), 'Nonexistant placeholder \'foobar\' reported as existing');
|
|
|
37 |
$this->assertSame("__global__", $this->tpl->placeholderExists('var', '__global__'), 'Existing in block \'__global__\' placeholder \'var\' reported as nonexistant');
|
|
|
38 |
$this->assertSame("", $this->tpl->placeholderExists('foobar', '__global__'), 'Nonexistant in block \'__global__\' placeholder \'foobar\' reported as existing');
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
function testBlockExists()
|
|
|
42 |
{
|
|
|
43 |
$this->tpl->setTemplate('{var}');
|
|
|
44 |
$this->assertTrue($this->tpl->blockExists('__global__'), 'Existing block \'__global__\' reported as nonexistant');
|
|
|
45 |
$this->assertTrue(!$this->tpl->blockExists('foobar'), 'Nonexistant block \'foobar\' reported as existing');
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
function testAddBlock()
|
|
|
49 |
{
|
|
|
50 |
$result = $this->tpl->loadTemplatefile('blocks.html', true, true);
|
|
|
51 |
if (PEAR::isError($result)) {
|
|
|
52 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
53 |
}
|
|
|
54 |
$this->tpl->addBlock('var', 'added', 'added:{new_var}');
|
|
|
55 |
$this->assertTrue($this->tpl->blockExists('added'), 'The new block seems to be missing');
|
|
|
56 |
$this->assertTrue(!$this->tpl->placeholderExists('var'), 'The old variable seems to be still present in the template');
|
|
|
57 |
$this->tpl->setVariable('new_var', 'new_value');
|
|
|
58 |
$this->assertEquals('added:new_value', $this->_stripWhitespace($this->tpl->get()));
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
function testAddBlockfile()
|
|
|
62 |
{
|
|
|
63 |
$result = $this->tpl->loadTemplatefile('blocks.html', true, true);
|
|
|
64 |
if (PEAR::isError($result)) {
|
|
|
65 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
66 |
}
|
|
|
67 |
$result = $this->tpl->addBlockfile('var', 'added', 'addblock.html');
|
|
|
68 |
if (PEAR::isError($result)) {
|
|
|
69 |
$this->assertTrue(false, 'Error adding block from file: '. $result->getMessage());
|
|
|
70 |
}
|
|
|
71 |
$this->assertTrue($this->tpl->blockExists('added'), 'The new block seems to be missing');
|
|
|
72 |
$this->assertTrue(!$this->tpl->placeholderExists('var'), 'The old variable seems to be still present in the template');
|
|
|
73 |
$this->tpl->setVariable('new_var', 'new_value');
|
|
|
74 |
$this->assertEquals('added:new_value', $this->_stripWhitespace($this->tpl->get()));
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
function testReplaceBlock()
|
|
|
78 |
{
|
|
|
79 |
$result = $this->tpl->loadTemplatefile('blocks.html', true, true);
|
|
|
80 |
if (PEAR::isError($result)) {
|
|
|
81 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
82 |
}
|
|
|
83 |
$this->tpl->setVariable('old_var', 'old_value');
|
|
|
84 |
$this->tpl->parse('old_block');
|
|
|
85 |
// old_block's contents should be discarded
|
|
|
86 |
$this->tpl->replaceBlock('old_block', 'replaced:{replaced_var}#', false);
|
|
|
87 |
$this->assertTrue(!$this->tpl->blockExists('old_inner_block') && !$this->tpl->placeholderExists('old_var'),
|
|
|
88 |
'The replaced block\'s contents seem to be still present');
|
|
|
89 |
$this->tpl->setVariable('replaced_var', 'replaced_value');
|
|
|
90 |
$this->tpl->parse('old_block');
|
|
|
91 |
// this time old_block's contents should be preserved
|
|
|
92 |
$this->tpl->replaceBlock('old_block', 'replaced_again:{brand_new_var}', true);
|
|
|
93 |
$this->tpl->setVariable('brand_new_var', 'brand_new_value');
|
|
|
94 |
$this->assertEquals('replaced:replaced_value#replaced_again:brand_new_value', $this->_stripWhitespace($this->tpl->get()));
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
function testReplaceBlockfile()
|
|
|
98 |
{
|
|
|
99 |
$result = $this->tpl->loadTemplatefile('blocks.html', true, true);
|
|
|
100 |
if (PEAR::isError($result)) {
|
|
|
101 |
$this->assertTrue(false, 'Error loading template file: '. $result->getMessage());
|
|
|
102 |
}
|
|
|
103 |
$this->tpl->setVariable('old_var', 'old_value');
|
|
|
104 |
$this->tpl->parse('old_block');
|
|
|
105 |
// old_block's contents should be discarded
|
|
|
106 |
$result = $this->tpl->replaceBlockfile('old_block', 'replaceblock.html', false);
|
|
|
107 |
if (PEAR::isError($result)) {
|
|
|
108 |
$this->assertTrue(false, 'Error replacing block from file: '. $result->getMessage());
|
|
|
109 |
}
|
|
|
110 |
$this->assertTrue(!$this->tpl->blockExists('old_inner_block') && !$this->tpl->placeholderExists('old_var'),
|
|
|
111 |
'The replaced block\'s contents seem to be still present');
|
|
|
112 |
$this->tpl->setVariable(array(
|
|
|
113 |
'replaced_var' => 'replaced_value',
|
|
|
114 |
'replaced_inner_var' => 'inner_value'
|
|
|
115 |
));
|
|
|
116 |
$this->tpl->parse('old_block');
|
|
|
117 |
// this time old_block's contents should be preserved
|
|
|
118 |
$result = $this->tpl->replaceBlockfile('old_block', 'addblock.html', true);
|
|
|
119 |
if (PEAR::isError($result)) {
|
|
|
120 |
$this->assertTrue(false, 'Error replacing block from file: '. $result->getMessage());
|
|
|
121 |
}
|
|
|
122 |
$this->tpl->setVariable('new_var', 'again');
|
|
|
123 |
$this->assertEquals('replaced:replaced_value|inner_value#added:again', $this->_stripWhitespace($this->tpl->get()));
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
function testCallback()
|
|
|
127 |
{
|
|
|
128 |
$this->tpl->setTemplate('callback:func_uppercase(word)');
|
|
|
129 |
$this->tpl->setCallbackFunction('uppercase', '_uppercaseCallback');
|
|
|
130 |
$res = $this->tpl->performCallback();
|
|
|
131 |
if (PEAR::isError($res)) {
|
|
|
132 |
$this->assertTrue(false, 'Error performing callback: '. $res->getMessage());
|
|
|
133 |
}
|
|
|
134 |
$this->assertEquals('callback:WORD', $this->tpl->get());
|
|
|
135 |
|
|
|
136 |
$this->tpl->setTemplate('callback:func_lowercase(Word)');
|
|
|
137 |
$this->tpl->setCallbackFunction('lowercase', array('Callbacks','_lowercaseCallback'));
|
|
|
138 |
$res = $this->tpl->performCallback();
|
|
|
139 |
if (PEAR::isError($res)) {
|
|
|
140 |
$this->assertTrue(false, 'Error performing callback: '. $res->getMessage());
|
|
|
141 |
}
|
|
|
142 |
$this->assertEquals('callback:word', $this->tpl->get());
|
|
|
143 |
|
|
|
144 |
$this->tpl->setTemplate('callback:func_lowercase(Word)');
|
|
|
145 |
$this->tpl->setCallbackFunction('lowercase', array(new Callbacks,'_lowercaseCallback'));
|
|
|
146 |
$res = $this->tpl->performCallback();
|
|
|
147 |
if (PEAR::isError($res)) {
|
|
|
148 |
$this->assertTrue(false, 'Error performing callback: '. $res->getMessage());
|
|
|
149 |
}
|
|
|
150 |
$this->assertEquals('callback:word', $this->tpl->get());
|
|
|
151 |
|
|
|
152 |
$this->tpl->setTemplate('callback:func_numberFormat(1.5, 2)');
|
|
|
153 |
$this->tpl->setCallbackFunction('numberFormat', array('Callbacks', '_numberFormatCallback'), '', true);
|
|
|
154 |
$res = $this->tpl->performCallback();
|
|
|
155 |
if (PEAR::isError($res)) {
|
|
|
156 |
$this->assertTrue(false, 'Error performing callback: '. $res->getMessage());
|
|
|
157 |
}
|
|
|
158 |
$this->assertEquals('callback:1.50', $this->tpl->get());
|
|
|
159 |
|
|
|
160 |
$this->tpl->setTemplate('callback:func_numberFormat(1.5, 2)');
|
|
|
161 |
$GLOBALS['obj'] = new Callbacks;
|
|
|
162 |
$this->tpl->setCallbackFunction('numberFormat', '_numberFormatCallback', 'obj', true);
|
|
|
163 |
$res = $this->tpl->performCallback();
|
|
|
164 |
if (PEAR::isError($res)) {
|
|
|
165 |
$this->assertTrue(false, 'Error performing callback: '. $res->getMessage());
|
|
|
166 |
}
|
|
|
167 |
$this->assertEquals('callback:1.50', $this->tpl->get());
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
?>
|