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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
<?php
/**
* Smarty PHPunit tests deault plugin handler
*
* @author Uwe Tews
*/
/**
* class for plugin handler tests
*
*
*
*
*/
class DefaultPluginHandlerTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->setForceCompile(true);
$this->smarty->disableSecurity();
$this->smarty->registerDefaultPluginHandler('my_plugin_handler');
}
public function testInit()
{
$this->cleanDirs();
}
public function testDefaultFunctionScript()
{
$this->assertEquals("scriptfunction foo bar", $this->smarty->fetch('test_default_function_script.tpl'));
}
public function testDefaultFunctionScriptNotCachable1()
{
$this->smarty->assign('foo', 'foo');
$this->smarty->caching = 1;
$this->assertEquals("scriptfunction foo", $this->smarty->fetch('test_default_function_script_notcachable.tpl'));
}
public function testDefaultFunctionScriptNotCachable2()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->caching = 1;
$this->assertEquals("scriptfunction bar", $this->smarty->fetch('test_default_function_script_notcachable.tpl'));
}
public function testDefaultFunctionLocal()
{
$this->assertEquals("localfunction foo bar", $this->smarty->fetch('test_default_function_local.tpl'));
}
public function testDefaultCompilerFunctionScript()
{
$this->assertEquals("echo 'scriptcompilerfunction '.'foo bar';", $this->smarty->fetch('test_default_compiler_function_script.tpl'));
}
public function testDefaultCompilerObject()
{
$this->assertEquals('Public World', $this->smarty->fetch('test_default_compiler_object.tpl'));
}
public function testDefaultBlockScript()
{
$this->assertEquals("scriptblock foo bar", $this->smarty->fetch('test_default_block_script.tpl'));
}
public function testDefaultModifierScript()
{
$this->smarty->assign('foo', 'bar');
$this->assertEquals("scriptmodifier default bar", $this->smarty->fetch('test_default_modifier_script.tpl'));
}
public function testDefaultModifier()
{
$this->smarty->assign('foo', 'bar');
$this->assertEquals("localmodifier bar", $this->smarty->fetch('test_default_modifier.tpl'));
}
public function testDefaultModifierStaticClassMethodCaching1()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->caching = 1;
$this->assertEquals("staticmodifier bar", $this->smarty->fetch('test_default_static_modifier.tpl'));
}
public function testDefaultModifierStaticClassMethodCaching2()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->caching = 1;
$this->assertEquals("staticmodifier bar", $this->smarty->fetch('test_default_static_modifier.tpl'));
}
}
function my_plugin_handler($tag, $type, $template, &$callback, &$script, &$cachable)
{
switch ($type) {
case \Smarty\Smarty::PLUGIN_FUNCTION:
switch ($tag) {
case 'scriptfunction':
$script = './scripts/script_function_tag.php';
$callback = 'default_script_function_tag';
return true;
case 'scriptfunctionnotcachable':
$script = './scripts/script_function_tag.php';
$callback = 'default_script_function_tag';
$cachable = false;
return true;
case 'localfunction':
$callback = 'default_local_function_tag';
return true;
default:
return false;
}
case \Smarty\Smarty::PLUGIN_COMPILER:
switch ($tag) {
case 'scriptcompilerfunction':
$script = './scripts/script_compiler_function_tag.php';
$callback = 'default_script_compiler_function_tag';
return true;
case 'compilerobject':
$callback = array(new CompilerDefaultPluginClass, 'compile');
return true;
default:
return false;
}
case \Smarty\Smarty::PLUGIN_BLOCK:
switch ($tag) {
case 'scriptblock':
$script = './scripts/script_block_tag.php';
$callback = 'default_script_block_tag';
return true;
default:
return false;
}
case \Smarty\Smarty::PLUGIN_MODIFIER:
switch ($tag) {
case 'scriptmodifier':
$script = './scripts/script_modifier.php';
$callback = 'default_script_modifier';
return true;
case 'mydefaultmodifier':
$callback = 'default_local_modifier';
return true;
case 'mydefaultstaticmodifier':
$script = './scripts/script_default_static_modifier.php';
$callback = array('DefModifier', 'default_static_modifier');
return true;
default:
return false;
}
default:
return false;
}
}
function default_local_function_tag($params, $template)
{
return 'localfunction ' . $params['value'];
}
function default_local_modifier($input)
{
return 'localmodifier ' . $input;
}
class CompilerDefaultPluginClass
{
static function statCompile ($params, $compiler) {
return '<?php echo \'Static World\';?>';
}
public function compile ($params, $compiler) {
return '<?php echo \'Public World\';?>';
}
}
|