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
|
<?php
/**
* Smarty PHPunit tests register->compilerFunction / unregister->compilerFunction methods
*
* @author Uwe Tews
*/
use Smarty\Smarty;
/**
* class for register->compilerFunction / unregister->compilerFunction methods tests
*
*
*
*
*/
class RegisterCompilerFunctionTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testInit()
{
$this->cleanDirs();
}
/**
* test register->compilerFunction method for function
*/
public function testRegisterCompilerFunction()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction');
$this->assertEquals('mycompilerfunction', $this->smarty->getRegisteredPlugin('compiler', 'testcompilerfunction')[0]);
$this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testcompilerfunction var=1}'));
}
/**
* test register->compilerFunction method for blocks
*/
public function testRegisterCompilerFunctionBlock()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'foo', 'mycompilerfunctionopen');
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'fooclose', 'mycompilerfunctionclose');
$result = $this->smarty->fetch('eval:{foo} hallo {/foo}');
$this->assertEquals('open tag hallo close tag', $result);
}
/**
* test register->compilerFunction method for static class
*/
public function testRegisterCompilerFunctionClass()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array('mycompilerfunctionclass', 'execute'));
$this->assertEquals('hello world 2', $this->smarty->fetch('eval:{testcompilerfunction var1=2}'));
}
/**
* test register->compilerFunction method for objects
*/
public function testRegisterCompilerFunctionObject()
{
$obj = new mycompilerfunctionclass;
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array($obj, 'compile'));
$this->assertEquals('hello world 3', $this->smarty->fetch('eval:{testcompilerfunction var2=3}'));
}
/**
* test unregister->compilerFunction method
*/
public function testUnregisterCompilerFunction()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction');
$this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction');
$this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction'));
}
/**
* test unregister->compilerFunction method not registered
*/
public function testUnregisterCompilerFunctionNotRegistered()
{
$this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction');
$this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction'));
}
/**
* test unregister->compilerFunction method other registered
*/
public function testUnregisterCompilerFunctionOtherRegistered()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testcompilerfunction', 'mycompilerfunction');
$this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction');
$this->assertIsArray($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testcompilerfunction'));
}
}
function mycompilerfunction($params, $smarty)
{
return "<?php echo 'hello world {$params['var']}';?>";
}
function mycompilerfunctionopen($params, $smarty)
{
return "<?php echo 'open tag';?>";
}
function mycompilerfunctionclose($params, $smarty)
{
return "<?php echo 'close tag';?>";
}
class mycompilerfunctionclass
{
static function execute($params, $smarty)
{
return "<?php echo 'hello world {$params['var1']}';?>";
}
public function compile($params, $smarty)
{
return "<?php echo 'hello world {$params['var2']}';?>";
}
}
|