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
|
<?php
/**
* Smarty PHPunit tests register->modifier / unregister->modifier methods
*
* @author Uwe Tews
*/
/**
* class for register->modifier / unregister->modifier methods tests
*
*
*
*
*/
class RegisterModifierTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* test register->modifier method for function
*/
public function testRegisterModifier()
{
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier');
$this->assertEquals('mymodifier', $this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier')[0]);
$this->smarty->assign('foo', 'foo');
$this->smarty->assign('bar', 'bar');
$this->assertEquals('foo function blar bar', $this->smarty->fetch('eval:{$foo|testmodifier:blar:$bar}'));
}
/**
* test register->modifier method for classes
*/
public function testRegisterModifierClass()
{
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', array('mymodifierclass', 'static_method'));
$this->smarty->assign('foo', 'foo');
$this->smarty->assign('bar', 'bar');
$this->assertEquals('foo static blar bar', $this->smarty->fetch('eval:{$foo|testmodifier:blar:$bar}'));
}
/**
* test register->modifier method for objects
*/
public function testRegisterModifierObject()
{
$obj = new mymodifierclass;
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', array($obj, 'object_method'));
$this->smarty->assign('foo', 'foo');
$this->smarty->assign('bar', 'bar');
$this->assertEquals('foo object blar bar', $this->smarty->fetch('eval:{$foo|testmodifier:blar:$bar}'));
}
/**
* test unregister->modifier method
*/
public function testUnregisterModifier()
{
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier');
$this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertNull($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier'));
}
/**
* test unregister->modifier method not registered
*/
public function testUnregisterModifierNotRegistered()
{
$this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertNull($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier'));
}
/**
* test unregister->modifier method other registered
*/
public function testUnregisterModifierOtherRegistered()
{
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testmodifier', 'mymodifier');
$this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertIsArray($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testmodifier'));
}
/**
* test cannot call native PHP fuctions by default
* @dataProvider dataUnknownModifiers
*/
public function testNativePHPModifiers($template, $expectedValue)
{
$this->cleanDirs();
$this->expectException(\Smarty\CompilerException::class);
$this->expectExceptionMessage('unknown modifier');
$this->smarty->fetch('string:' . $template);
}
public function dataUnknownModifiers(): array {
return [
['{" blah"|ltrim:" "}', 'blah'],
['{"blah"|strrev}', 'halb'],
['{"blah"|ucfirst}', 'Blah'],
['{"blah"|md5}', md5('blah')],
];
}
/**
* test register wildcard modifier using extension
* @dataProvider dataUnknownModifiers
*/
public function testUnregisterModifiers($template, $expectedValue)
{
$this->cleanDirs();
$this->smarty->addExtension(new WildcardExtension());
$this->assertEquals($expectedValue, $this->smarty->fetch('string:' . $template));
}
/**
* test register wildcard modifier using setExtensions
* @dataProvider dataUnknownModifiers
*/
public function testSetExtensions($template, $expectedValue)
{
$this->cleanDirs();
$this->smarty->setExtensions([
new \Smarty\Extension\CoreExtension(),
new WildcardExtension()
]);
$this->assertEquals($expectedValue, $this->smarty->fetch('string:' . $template));
}
public function testRegisterNativePhpFuncAsString()
{
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'strrev', 'strrev');
$this->smarty->assign('myVar', 'andersom');
$this->assertEquals('mosredna', $this->smarty->fetch('string:{strrev($myVar)}'));
}
public function testRegisterNativePhpFuncUnderDifferentName()
{
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'k_xyz_a', 'strrev');
$this->smarty->assign('myVar', 'andersom');
$this->assertEquals('mosredna', $this->smarty->fetch('string:{k_xyz_a($myVar)}'));
}
}
class WildcardExtension extends \Smarty\Extension\Base {
public function getModifierCallback(string $modifierName) {
if (is_callable($modifierName)) {
return $modifierName;
}
return null;
}
}
function mymodifier($a, $b, $c)
{
return "$a function $b $c";
}
class mymodifierclass
{
static function static_method($a, $b, $c)
{
return "$a static $b $c";
}
public function object_method($a, $b, $c)
{
return "$a object $b $c";
}
}
|