ContentInterpolationTest.php
4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id: ContentInterpolationTest.php 579 2009-04-25 23:14:46Z kornel $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
class ContentInterpolationTest extends PHPTAL_TestCase
{
public function testInterpol()
{
$src = <<<EOT
<span>\${foo}</span>
EOT;
$exp = <<<EOT
<span>foo value</span>
EOT;
$tpl = $this->newPHPTAL();
$tpl->setSource($src);
$tpl->foo = 'foo value';
$res = $tpl->execute();
$this->assertEquals($exp, $res);
}
public function testInterpol2()
{
$src = <<<EOT
<span>\${foo2} x \${structure foo} y \${foo}\${structure foo2}</span><img/>
EOT;
$exp = <<<EOT
<span>{foo2 <img />} x foo value y foo value{foo2 <img />}</span><img/>
EOT;
$tpl = $this->newPHPTAL();
$tpl->setSource($src);
$tpl->foo = 'foo value';
$tpl->foo2 = '{foo2 <img />}';
$res = $tpl->execute();
$this->assertEquals($exp, $res);
}
public function testInterpol3()
{
$src = <<<EOT
<span>\${foo}\${foo}</span>
EOT;
$exp = <<<EOT
<span>foo valuefoo value</span>
EOT;
$tpl = $this->newPHPTAL();
$tpl->setSource($src);
$tpl->foo = 'foo value';
$res = $tpl->execute();
$this->assertEquals($exp, $res);
}
public function testNoInterpol()
{
$src = <<<EOT
<span>$\${foo}</span>
EOT;
$exp = <<<EOT
<span>\${foo}</span>
EOT;
$tpl = $this->newPHPTAL();
$tpl->setSource($src);
$tpl->foo = 'foo value';
$res = $tpl->execute();
$this->assertEquals($exp, $res);
}
public function testNoInterpolAdv()
{
$src = <<<EOT
<span>$$\${foo}</span>
EOT;
$exp = <<<EOT
<span>$\${foo}</span>
EOT;
$tpl = $this->newPHPTAL();
$tpl->setSource($src);
$tpl->foo = 'foo value';
$res = $tpl->execute();
$this->assertEquals($exp, $res);
}
public function testPHPBlock()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>test<?php echo "<x>"; ?>test<?php print("&") ?>test</p>');
$this->assertEquals('<p>test<x>test&test</p>', $tpl->execute());
}
public function testPHPBlockShort()
{
ini_set('short_open_tag',1);
if (!ini_get('short_open_tag')) $this->markTestSkipped("PHP is buggy");
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>test<? print("<x>"); ?>test<?= "&" ?>test</p>');
$this->assertEquals('<p>test<x>test&test</p>', $tpl->execute());
ini_restore('short_open_tag');
}
public function testPHPBlockNoShort()
{
ini_set('short_open_tag', 0);
if (ini_get('short_open_tag')) $this->markTestSkipped("PHP is buggy");
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>test<? print("<x>"); ?>test<?= "&" ?>test</p>');
try
{
// unlike attributes, this isn't going to be escaped, because it gets parsed as a real processing instruction
$this->assertEquals('<p>test<? print("<x>"); ?>test<?= "&" ?>test</p>', $tpl->execute());
}
catch(PHPTAL_ParserException $e) {/* xml ill-formedness error is ok too */}
ini_restore('short_open_tag');
}
/**
* @expectedException PHPTAL_VariableNotFoundException
*/
function testErrorsThrow()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>${error}</p>');
$tpl->execute();
}
/**
* @expectedException PHPTAL_VariableNotFoundException
*/
function testErrorsThrow2()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>${error | error}</p>');
$tpl->execute();
}
function testErrorsSilenced()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<p>${error | nothing}</p>');
$this->assertEquals('<p></p>',$tpl->execute());
}
function testZeroIsNotEmpty()
{
$tpl = $this->newPHPTAL();
$tpl->zero = '0';
$tpl->setSource('<p>${zero | error}</p>');
$this->assertEquals('<p>0</p>',$tpl->execute());
}
}