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
|
<?php
/**
* Smarty PHPunit tests of modifier
*
* @author Uwe Tews
*/
/**
* class for modifier tests
*
*
*
*
*/
class ModifierTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* Test modifier
*
* @not runInSeparateProcess
*
* @dataProvider dataTestModifier
*/
public function testModifier($code, $result, $testName, $testNumber)
{
$name = empty($testName) ? $testNumber : $testName;
$file = "testModifier_{$name}.tpl";
$this->makeTemplateFile($file, $code);
$this->smarty->assignGlobal('file', $file);
$this->smarty->assign('bar', 'buh');
$this->assertEquals($result, $this->smarty->fetch($file),
"testModifier - {$code} - {$name}");
}
/*
* Data provider für testModifier
*/
public function dataTestModifier()
{
$i = 1;
/*
* Code
* result
* test name
*/
return array(array('{"hello world"|strlen}', '11', 'OnString', $i ++),
array('{$foo ="hello world"}{$foo|strlen}', '11', 'OnVar', $i ++),
array('{"hello world"|truncate:6}', 'hel...', 'TruncatePlugin', $i ++),
array('{$foo=7}{"hello world"|truncate:$foo}', 'hell...', 'TruncatePluginLengthVar', $i ++),
array('{$foo=10}{$bar=\'<>\'}{"hello world"|truncate:$foo:$bar}', 'hello<>', 'TruncatePluginAllVar', $i ++),
array('{"hello world"|truncate:6|strlen}', '6', 'Chain', $i ++),
array('{"hello world"|truncate:6:"xx"|cat:"Smarty"}', 'hellxxSmarty', 'ChainVar', $i ++),
array('{"hello world"|truncate:6|strlen}', '6', 'Chain', $i ++),
array('{if "hello world"|truncate:6|strlen == 6}okay{/if}', 'okay', 'InIF', $i ++),
array('{"hello world"|truncate:6|strlen + ("hello world"|truncate:8|strlen)}', '14', 'Expression', $i ++),
array('{1.1*7.1|round}', '7.7', 'InExpression', $i ++),
array('{counter|truncate:5 start=100000}', '10...', 'PluginOutput', $i ++),
array('{1 + [1,2,3]|count}', '4', 'SumExpression', $i ++),
);
}
/**
* test registered modifier static class
*/
public function testModifierRegisteredStaticClass()
{
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', array('testmodifierclass', 'staticcall'));
$this->smarty->assign('foo', 1);
$this->assertEquals("mymodifier static 1", $this->smarty->fetch('testModifier_RegisteredStatic.tpl'));
}
/**
* test registered modifier method call
*/
public function testModifierRegisteredMethodCall()
{
$obj = new testmodifierclass();
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', array($obj, 'method'));
$this->smarty->assign('foo', 3);
$this->assertEquals("mymodifier method 3", $this->smarty->fetch('testModifier_RegisteredMethod.tpl'));
}
/**
* test unknown modifier error
*/
public function testUnknownModifier()
{
$this->expectException(\Smarty\CompilerException::class);
$this->expectExceptionMessage('unknown modifier \'unknown\'');
$this->smarty->fetch('eval:{"hello world"|unknown}');
}
/**
* test default modifier
*/
public function testDefaultModifier()
{
$this->smarty->setDefaultModifiers(array('escape'));
$this->smarty->assign('foo', '<bar>');
$this->assertEquals('<bar><bar>', $this->smarty->fetch('testModifier_Default.tpl'));
}
}
function testmodifier($value)
{
return "mymodifier function $value";
}
class testmodifierclass
{
static function staticcall($value)
{
return "mymodifier static $value";
}
public function method($value)
{
return "mymodifier method $value";
}
}
|