|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Esi\SimpleTpl. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2006 - 2024 Eric Sizemore <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* This file is licensed under The MIT License. For the full copyright and |
|
11
|
|
|
* license information, please view the LICENSE.md file that was distributed |
|
12
|
|
|
* with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Esi\SimpleTpl\Tests; |
|
16
|
|
|
|
|
17
|
|
|
use Esi\SimpleTpl\Template; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
use LogicException; |
|
20
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
|
21
|
|
|
use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily; |
|
22
|
|
|
use PHPUnit\Framework\TestCase; |
|
23
|
|
|
use RuntimeException; |
|
24
|
|
|
use Symfony\Component\Cache\Adapter\NullAdapter; |
|
25
|
|
|
|
|
26
|
|
|
use function chmod; |
|
27
|
|
|
use function ob_get_clean; |
|
28
|
|
|
use function ob_start; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @internal |
|
32
|
|
|
*/ |
|
33
|
|
|
#[CoversClass(Template::class)] |
|
34
|
|
|
final class TemplateTest extends TestCase |
|
35
|
|
|
{ |
|
36
|
|
|
private static string $fixtureDir; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array<string> |
|
40
|
|
|
*/ |
|
41
|
|
|
private static array $fixtureFiles; |
|
42
|
|
|
|
|
43
|
|
|
public static function setUpBeforeClass(): void |
|
44
|
|
|
{ |
|
45
|
|
|
self::$fixtureDir = \dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'fixtures'; |
|
46
|
|
|
self::$fixtureFiles = [ |
|
47
|
|
|
'empty' => self::$fixtureDir . \DIRECTORY_SEPARATOR . 'empty.tpl', |
|
48
|
|
|
'no_cache_test' => self::$fixtureDir . \DIRECTORY_SEPARATOR . 'no_cache_test.tpl', |
|
49
|
|
|
'refresh_cache' => self::$fixtureDir . \DIRECTORY_SEPARATOR . 'refresh_cache.tpl', |
|
50
|
|
|
'unreadable' => self::$fixtureDir . \DIRECTORY_SEPARATOR . 'unreadable.tpl', |
|
51
|
|
|
'valid' => self::$fixtureDir . \DIRECTORY_SEPARATOR . 'valid.tpl', |
|
52
|
|
|
'valid_parsed' => self::$fixtureDir . \DIRECTORY_SEPARATOR . 'valid_parsed.tpl', |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function tearDownAfterClass(): void |
|
57
|
|
|
{ |
|
58
|
|
|
self::$fixtureDir = ''; |
|
59
|
|
|
self::$fixtureFiles = []; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testDisplayDoesDisplay(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$template = new Template(); |
|
65
|
|
|
|
|
66
|
|
|
$template->setTplVars([ |
|
67
|
|
|
'title' => 'Simple Template Engine Test', |
|
68
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
ob_start(); |
|
72
|
|
|
$template->display(self::$fixtureFiles['valid']); |
|
73
|
|
|
$data = ob_get_clean(); |
|
74
|
|
|
|
|
75
|
|
|
self::assertIsString($data); |
|
76
|
|
|
self::assertStringEqualsFile(self::$fixtureFiles['valid_parsed'], $data); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testGetTplVars(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$template = new Template(); |
|
82
|
|
|
|
|
83
|
|
|
$template->setTplVars([ |
|
84
|
|
|
'title' => 'Simple Template Engine Test', |
|
85
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
86
|
|
|
]); |
|
87
|
|
|
|
|
88
|
|
|
$tplVars = $template->getTplVars(); |
|
89
|
|
|
|
|
90
|
|
|
self::assertSame([ |
|
91
|
|
|
'title' => 'Simple Template Engine Test', |
|
92
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
93
|
|
|
], $tplVars); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testParseEmptyTemplateFile(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$template = new Template(); |
|
99
|
|
|
|
|
100
|
|
|
$this->expectException(RuntimeException::class); |
|
101
|
|
|
|
|
102
|
|
|
$template->parse(self::$fixtureFiles['empty']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testParseInvalidTemplateFile(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$template = new Template(); |
|
108
|
|
|
|
|
109
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
110
|
|
|
|
|
111
|
|
|
$template->parse('/this/should/not/exist.tpl'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testParseTemplateIsNotCached(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$template = new Template(new NullAdapter()); |
|
117
|
|
|
|
|
118
|
|
|
$template->setTplVars([ |
|
119
|
|
|
'title' => 'Simple Template Engine Test', |
|
120
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
121
|
|
|
]); |
|
122
|
|
|
|
|
123
|
|
|
$data = $template->parse(self::$fixtureFiles['valid']); |
|
124
|
|
|
self::assertStringEqualsFile(self::$fixtureFiles['valid_parsed'], $data); |
|
125
|
|
|
|
|
126
|
|
|
// If things are working properly, with no cache, this should return the changed vars. |
|
127
|
|
|
$template->setTplVars([ |
|
128
|
|
|
'title' => 'Simple Template Engine Test - No Cache', |
|
129
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
130
|
|
|
]); |
|
131
|
|
|
|
|
132
|
|
|
$data = $template->parse(self::$fixtureFiles['valid']); |
|
133
|
|
|
self::assertStringEqualsFile(self::$fixtureFiles['no_cache_test'], $data); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
#[RequiresOperatingSystemFamily('Linux')] |
|
137
|
|
|
public function testParseUnreadableTemplateFile(): void |
|
138
|
|
|
{ |
|
139
|
|
|
$template = new Template(); |
|
140
|
|
|
|
|
141
|
|
|
chmod(self::$fixtureFiles['unreadable'], 0o000); |
|
142
|
|
|
|
|
143
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
144
|
|
|
|
|
145
|
|
|
$template->parse(self::$fixtureFiles['unreadable']); |
|
146
|
|
|
|
|
147
|
|
|
chmod(self::$fixtureFiles['unreadable'], 0o644); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testParseWithCachedTemplate(): void |
|
151
|
|
|
{ |
|
152
|
|
|
$template = new Template(); |
|
153
|
|
|
|
|
154
|
|
|
$template->setTplVars([ |
|
155
|
|
|
'title' => 'Simple Template Engine Test', |
|
156
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
157
|
|
|
]); |
|
158
|
|
|
|
|
159
|
|
|
$data = $template->parse(self::$fixtureFiles['valid']); |
|
160
|
|
|
self::assertStringEqualsFile(self::$fixtureFiles['valid_parsed'], $data); |
|
161
|
|
|
|
|
162
|
|
|
$template->setTplVars([ |
|
163
|
|
|
'title' => 'Simple Template Engine Test - Is it cached?', |
|
164
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
165
|
|
|
]); |
|
166
|
|
|
|
|
167
|
|
|
$data = $template->parse(self::$fixtureFiles['valid']); |
|
168
|
|
|
self::assertStringEqualsFile(self::$fixtureFiles['valid_parsed'], $data); |
|
169
|
|
|
|
|
170
|
|
|
self::assertTrue($template->clearCache()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function testParseWithDirectoryNotFile(): void |
|
174
|
|
|
{ |
|
175
|
|
|
$template = new Template(); |
|
176
|
|
|
|
|
177
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
178
|
|
|
|
|
179
|
|
|
$template->parse(self::$fixtureDir); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function testParseWithoutTplVars(): void |
|
183
|
|
|
{ |
|
184
|
|
|
$template = new Template(); |
|
185
|
|
|
|
|
186
|
|
|
$this->expectException(LogicException::class); |
|
187
|
|
|
|
|
188
|
|
|
$template->setTplVars([]); |
|
189
|
|
|
$template->parse(self::$fixtureFiles['valid']); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testRefreshCache(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$template = new Template(); |
|
195
|
|
|
|
|
196
|
|
|
$template->setTplVars([ |
|
197
|
|
|
'title' => 'Simple Template Engine Test', |
|
198
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
199
|
|
|
]); |
|
200
|
|
|
|
|
201
|
|
|
$data = $template->parse(self::$fixtureFiles['refresh_cache']); |
|
202
|
|
|
self::assertStringEqualsFile(self::$fixtureFiles['valid_parsed'], $data); |
|
203
|
|
|
|
|
204
|
|
|
$isRefreshed = $template->refreshCache(self::$fixtureFiles['refresh_cache']); |
|
205
|
|
|
|
|
206
|
|
|
if ($isRefreshed) { |
|
207
|
|
|
$template->setTplVars([ |
|
208
|
|
|
'title' => 'Simple Template Engine Test - Refresh Cache', |
|
209
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
210
|
|
|
]); |
|
211
|
|
|
|
|
212
|
|
|
$data = $template->parse(self::$fixtureFiles['refresh_cache']); |
|
213
|
|
|
self::assertStringNotEqualsFile(self::$fixtureFiles['valid_parsed'], $data); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function testSetLeftDelimiter(): void |
|
218
|
|
|
{ |
|
219
|
|
|
$template = new Template(); |
|
220
|
|
|
|
|
221
|
|
|
$template->setLeftDelimiter('{{'); |
|
222
|
|
|
self::assertSame('{{', $template->getLeftDelimiter()); |
|
223
|
|
|
|
|
224
|
|
|
$template->setLeftDelimiter('{'); |
|
225
|
|
|
self::assertSame('{', $template->getLeftDelimiter()); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function testSetRightDelimiter(): void |
|
229
|
|
|
{ |
|
230
|
|
|
$template = new Template(); |
|
231
|
|
|
|
|
232
|
|
|
$template->setRightDelimiter('}}'); |
|
233
|
|
|
self::assertSame('}}', $template->getRightDelimiter()); |
|
234
|
|
|
|
|
235
|
|
|
$template->setRightDelimiter('}'); |
|
236
|
|
|
self::assertSame('}', $template->getRightDelimiter()); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function testSetTplVars(): void |
|
240
|
|
|
{ |
|
241
|
|
|
$template = new Template(); |
|
242
|
|
|
|
|
243
|
|
|
$template->setTplVars([ |
|
244
|
|
|
'title' => 'Simple Template Engine Test', |
|
245
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
246
|
|
|
]); |
|
247
|
|
|
|
|
248
|
|
|
self::assertSame([ |
|
249
|
|
|
'title' => 'Simple Template Engine Test', |
|
250
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
251
|
|
|
], $template->getTplVars()); |
|
252
|
|
|
|
|
253
|
|
|
$template->setTplVars([]); |
|
254
|
|
|
$tplVars = $template->getTplVars(); |
|
255
|
|
|
|
|
256
|
|
|
self::assertSame([], $tplVars); |
|
257
|
|
|
|
|
258
|
|
|
$template->setTplVars([ |
|
259
|
|
|
'title' => 'Simple Template Engine Test', |
|
260
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
261
|
|
|
]); |
|
262
|
|
|
|
|
263
|
|
|
$tplVars = $template->getTplVars(); |
|
264
|
|
|
self::assertSame([ |
|
265
|
|
|
'title' => 'Simple Template Engine Test', |
|
266
|
|
|
'content' => 'This is a test of the Simple Template Engine class by Eric Sizemore.', |
|
267
|
|
|
], $tplVars); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|