|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\HtmlTemplate\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Mezon\HtmlTemplate\HtmlTemplate; |
|
5
|
|
|
use Mezon\HtmlTemplate\TemplateResources; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class HtmlTemplateUnitTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
const PATH_TO_TEST_DATA = __DIR__ . '/TestData/'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Data provider for constructor tests |
|
15
|
|
|
* |
|
16
|
|
|
* @return array |
|
17
|
|
|
*/ |
|
18
|
|
|
public function constructorDataProvider(): array |
|
19
|
|
|
{ |
|
20
|
|
|
return [ |
|
21
|
|
|
[ |
|
22
|
|
|
HtmlTemplateUnitTest::PATH_TO_TEST_DATA, |
|
23
|
|
|
'index' |
|
24
|
|
|
], |
|
25
|
|
|
[ |
|
26
|
|
|
HtmlTemplateUnitTest::PATH_TO_TEST_DATA . 'Res/', |
|
27
|
|
|
'index2' |
|
28
|
|
|
], |
|
29
|
|
|
[ |
|
30
|
|
|
[ |
|
31
|
|
|
HtmlTemplateUnitTest::PATH_TO_TEST_DATA, |
|
32
|
|
|
HtmlTemplateUnitTest::PATH_TO_TEST_DATA . 'Res/' |
|
33
|
|
|
], |
|
34
|
|
|
'index2' |
|
35
|
|
|
] |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Testing construction with default path |
|
41
|
|
|
* |
|
42
|
|
|
* @param string|array $path |
|
43
|
|
|
* paths to content |
|
44
|
|
|
* @param string $template |
|
45
|
|
|
* template's name |
|
46
|
|
|
* @dataProvider constructorDataProvider |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testConstructor($path, string $template) |
|
49
|
|
|
{ |
|
50
|
|
|
// setup and test body |
|
51
|
|
|
$resources = new TemplateResources(); |
|
52
|
|
|
$resources->addCssFile('./some.css'); |
|
53
|
|
|
$resources->addJsFile('./some.js'); |
|
54
|
|
|
|
|
55
|
|
|
$template = new HtmlTemplate($path, $template, [ |
|
56
|
|
|
'main' |
|
57
|
|
|
]); |
|
58
|
|
|
$template->setResources($resources); |
|
59
|
|
|
|
|
60
|
|
|
$content = $template->compile(); |
|
61
|
|
|
|
|
62
|
|
|
// assertions |
|
63
|
|
|
$this->assertStringContainsString('<body>', $content, 'Layout was not setup'); |
|
64
|
|
|
$this->assertStringContainsString('<section>', $content, 'Block was not setup'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Data provider for constructor tests |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function invalidConstructorDataProvider(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return [ |
|
75
|
|
|
[ |
|
76
|
|
|
__DIR__, |
|
77
|
|
|
'index3' |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
|
|
false, |
|
81
|
|
|
'index4' |
|
82
|
|
|
] |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Testing invalid construction |
|
88
|
|
|
* |
|
89
|
|
|
* @param string|array $path |
|
90
|
|
|
* paths to content |
|
91
|
|
|
* @param string $template |
|
92
|
|
|
* template's name |
|
93
|
|
|
* @dataProvider invalidConstructorDataProvider |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testInvalidConstructor($path, string $template) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->expectException(\Exception::class); |
|
98
|
|
|
|
|
99
|
|
|
// setup and test body |
|
100
|
|
|
$template = new HtmlTemplate($path, $template, [ |
|
101
|
|
|
'main' |
|
102
|
|
|
]); |
|
103
|
|
|
|
|
104
|
|
|
// debug if the exception was not thrown |
|
105
|
|
|
var_dump($template); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Testing that all unused place holders will be removed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testCompile() |
|
112
|
|
|
{ |
|
113
|
|
|
// setup |
|
114
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, 'index', [ |
|
115
|
|
|
'main' |
|
116
|
|
|
]); |
|
117
|
|
|
$_SERVER['HTTP_HOST'] = 'host'; |
|
118
|
|
|
|
|
119
|
|
|
// test body |
|
120
|
|
|
$result = $template->compile(); |
|
121
|
|
|
|
|
122
|
|
|
// assertions |
|
123
|
|
|
$this->assertStringNotContainsStringIgnoringCase('{title}', $result); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Testing unexisting block |
|
128
|
|
|
*/ |
|
129
|
|
|
public function testGetUnexistingBlock() |
|
130
|
|
|
{ |
|
131
|
|
|
// setup and test body |
|
132
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, 'index', [ |
|
133
|
|
|
'main' |
|
134
|
|
|
]); |
|
135
|
|
|
|
|
136
|
|
|
$this->expectException(\Exception::class); |
|
137
|
|
|
|
|
138
|
|
|
// test body |
|
139
|
|
|
$template->getBlock('unexisting'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Test existing var fetch |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testGetExistingVar(): void |
|
146
|
|
|
{ |
|
147
|
|
|
// setup |
|
148
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
149
|
|
|
$template->setPageVar('existing-var', 'existing value'); |
|
150
|
|
|
|
|
151
|
|
|
// test body and assertions |
|
152
|
|
|
$this->assertEquals('existing value', $template->getPageVar('existing-var')); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Test unexisting var fetch |
|
157
|
|
|
*/ |
|
158
|
|
|
public function testGetUnExistingVar(): void |
|
159
|
|
|
{ |
|
160
|
|
|
// setup |
|
161
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
162
|
|
|
|
|
163
|
|
|
// assertions |
|
164
|
|
|
$this->expectException(\Exception::class); |
|
165
|
|
|
|
|
166
|
|
|
// test body |
|
167
|
|
|
$template->getPageVar('unexisting-var'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Testing setPageVars method |
|
172
|
|
|
*/ |
|
173
|
|
|
public function testSetPageVars(): void |
|
174
|
|
|
{ |
|
175
|
|
|
// setup |
|
176
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
177
|
|
|
|
|
178
|
|
|
// test body |
|
179
|
|
|
$template->setPageVars([ |
|
180
|
|
|
'title' => 'stitle', |
|
181
|
|
|
'resources' => 'sresources', |
|
182
|
|
|
'main' => 'smain' |
|
183
|
|
|
]); |
|
184
|
|
|
|
|
185
|
|
|
// assertions |
|
186
|
|
|
$this->assertEquals('stitle', $template->getPageVar('title')); |
|
187
|
|
|
$this->assertEquals('sresources', $template->getPageVar('resources')); |
|
188
|
|
|
$this->assertEquals('smain', $template->getPageVar('main')); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Testing method setPageVarFromFile |
|
193
|
|
|
*/ |
|
194
|
|
|
public function testSetPageVarFromFile(): void |
|
195
|
|
|
{ |
|
196
|
|
|
// setup |
|
197
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
198
|
|
|
|
|
199
|
|
|
// test body |
|
200
|
|
|
$template->setPageVarFromFile('title', HtmlTemplateUnitTest::PATH_TO_TEST_DATA . '/Res/var.txt'); |
|
201
|
|
|
|
|
202
|
|
|
// assertions |
|
203
|
|
|
$this->assertEquals('some var from file', $template->getPageVar('title')); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Testing method setPageVarFromBlock |
|
208
|
|
|
*/ |
|
209
|
|
|
public function testSetPageVarFromBlock(): void |
|
210
|
|
|
{ |
|
211
|
|
|
// setup |
|
212
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
213
|
|
|
|
|
214
|
|
|
// test body |
|
215
|
|
|
$template->setPageVarFromBlock('block-var', 'block3'); |
|
216
|
|
|
|
|
217
|
|
|
// assertions |
|
218
|
|
|
$this->assertEquals('block3', $template->getPageVar('block-var')); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Testing methods addPaths, setPaths, getPaths |
|
223
|
|
|
*/ |
|
224
|
|
|
public function testPathsManipulations(): void |
|
225
|
|
|
{ |
|
226
|
|
|
// setup |
|
227
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
228
|
|
|
|
|
229
|
|
|
// assertions |
|
230
|
|
|
$this->assertContains(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, $template->getPaths()); |
|
231
|
|
|
|
|
232
|
|
|
// test body |
|
233
|
|
|
$template->addPaths([ |
|
234
|
|
|
'some-path' |
|
235
|
|
|
]); |
|
236
|
|
|
|
|
237
|
|
|
// assertions |
|
238
|
|
|
$this->assertContains(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, $template->getPaths()); |
|
239
|
|
|
$this->assertContains('some-path', $template->getPaths()); |
|
240
|
|
|
|
|
241
|
|
|
// test body |
|
242
|
|
|
$template->setPaths([ |
|
243
|
|
|
'some-path' |
|
244
|
|
|
]); |
|
245
|
|
|
|
|
246
|
|
|
// asssertions |
|
247
|
|
|
$this->assertNotContains(HtmlTemplateUnitTest::PATH_TO_TEST_DATA, $template->getPaths()); |
|
248
|
|
|
$this->assertContains('some-path', $template->getPaths()); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Testing method |
|
253
|
|
|
*/ |
|
254
|
|
|
public function testBlockExists(): void |
|
255
|
|
|
{ |
|
256
|
|
|
// setup |
|
257
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
258
|
|
|
|
|
259
|
|
|
// test body and assertions |
|
260
|
|
|
$this->assertTrue($template->blockExists('block1')); |
|
261
|
|
|
$this->assertTrue($template->blockExists('block2')); |
|
262
|
|
|
$this->assertFalse($template->blockExists('block4')); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Testing method getFile |
|
267
|
|
|
*/ |
|
268
|
|
|
public function testGetFile(): void |
|
269
|
|
|
{ |
|
270
|
|
|
// setup |
|
271
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
272
|
|
|
|
|
273
|
|
|
// test body |
|
274
|
|
|
$result = $template->getFile('/Blocks/block3.tpl'); |
|
275
|
|
|
|
|
276
|
|
|
// assertions |
|
277
|
|
|
$this->assertEquals('block3', $result); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Testing exception |
|
282
|
|
|
*/ |
|
283
|
|
|
public function testGetFileException(): void |
|
284
|
|
|
{ |
|
285
|
|
|
// assertions |
|
286
|
|
|
$this->expectException(\Exception::class); |
|
287
|
|
|
|
|
288
|
|
|
// setup |
|
289
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
290
|
|
|
|
|
291
|
|
|
// test body |
|
292
|
|
|
$template->getFile('/Blocks/unsexisting.tpl'); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Testing method |
|
297
|
|
|
*/ |
|
298
|
|
|
public function testRecursiveVars(): void |
|
299
|
|
|
{ |
|
300
|
|
|
// setup |
|
301
|
|
|
$template = new HtmlTemplate(HtmlTemplateUnitTest::PATH_TO_TEST_DATA); |
|
302
|
|
|
$template->setPageVar('var-rec1', 'var1-was-substituted'); |
|
303
|
|
|
$template->setPageVar('var-rec2', '{var-rec1}'); |
|
304
|
|
|
$template->setPageVar('title', '{var-rec2}'); |
|
305
|
|
|
|
|
306
|
|
|
// test body |
|
307
|
|
|
$content = $template->compile(); |
|
308
|
|
|
|
|
309
|
|
|
// assertions |
|
310
|
|
|
$this->assertStringContainsString('var1-was-substituted', $content); |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|