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
|
<?php
/**
* Smarty PHPunit tests register->resource
*
* @author Uwe Tews
*/
use Smarty\Resource\CustomPlugin;
/**
* class for register->resource tests
*
*
*
*
*/
class RegisteredResourceTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->registerResource("rr", new RegisteredResourceTest_Resource1Plugin());
}
/**
* test resource plugin rendering
*/
public function testResourcePlugin()
{
$this->assertEquals('hello world', $this->smarty->fetch('rr:test'));
}
public function testClearCompiledResourcePlugin()
{
$this->assertEquals(1, $this->smarty->clearCompiledTemplate('rr:test'));
}
/**
* test resource plugin timesatmp
*/
public function testResourcePluginTimestamp()
{
$tpl = $this->smarty->createTemplate('rr:test');
$this->assertTrue(is_integer($tpl->getSource()->getTimeStamp()));
$this->assertEquals(10, strlen($tpl->getSource()->getTimeStamp()));
}
/**
* test compile_id change
*/
public function testResourceCompileIdChange()
{
$this->smarty->registerResource('myresource', new RegisteredResourceTest_Resource2Plugin());
$this->smarty->setCompileId('a');
$this->assertEquals('this is template 1', $this->smarty->fetch('myresource:some'));
$this->assertEquals('this is template 1', $this->smarty->fetch('myresource:some'));
$this->smarty->setCompileId('b');
$this->assertEquals('this is template 2', $this->smarty->fetch('myresource:some'));
$this->assertEquals('this is template 2', $this->smarty->fetch('myresource:some'));
}
/**
* test {$smarty.template}
*
*/
public function testSmartyTemplate() {
$this->smarty->registerResource('mytpl', new RegisteredResourceTest_Resource3Plugin());
$this->assertEquals('template = mytpl:foo', $this->smarty->fetch('mytpl:foo'));
}
/**
* test {$smarty.current_dir}
*
*/
public function testSmartyCurrentDir() {
$this->smarty->registerResource('mytpl', new RegisteredResourceTest_Resource4Plugin());
$this->assertEquals('current_dir = .', $this->smarty->fetch('mytpl:bar'));
}
}
class RegisteredResourceTest_Resource1Plugin extends CustomPlugin {
protected function fetch($name, &$source, &$mtime) {
$source = '{$x="hello world"}{$x}';
$mtime = 1000000000;
}
}
class RegisteredResourceTest_Resource2Plugin extends CustomPlugin {
protected function fetch($name, &$source, &$mtime) {
// we update a counter, so that we return a new source for every call
static $counter = 0;
$counter ++;
// construct a new source
$source = "this is template $counter";
$mtime = 1000000000;
}
protected function fetchTimestamp($name)
{
return 1000000000;
}
}
class RegisteredResourceTest_Resource3Plugin extends CustomPlugin {
protected function fetch($name, &$source, &$mtime) {
$source = 'template = {$smarty.template}';
$mtime = 1000000000;
}
}
class RegisteredResourceTest_Resource4Plugin extends CustomPlugin {
protected function fetch($name, &$source, &$mtime) {
$source = 'current_dir = {$smarty.current_dir}';
$mtime = 1000000000;
}
}
|