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