/
TagEngineTest.php
386 lines (360 loc) · 11.7 KB
/
TagEngineTest.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
declare(strict_types=1);
namespace LordSimal\CustomHtmlElements\Test;
use FilesystemIterator;
use LordSimal\CustomHtmlElements\TagEngine;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* @see \LordSimal\CustomHtmlElements\TagEngine
*/
class TagEngineTest extends TestCase
{
protected const CACHE_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
protected function setUp(): void
{
if (!file_exists(self::CACHE_DIR)) {
mkdir(self::CACHE_DIR);
}
}
protected function tearDown(): void
{
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(self::CACHE_DIR, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileInfo) {
$todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileInfo->getRealPath());
}
rmdir(self::CACHE_DIR);
}
/**
* Test a tab with a simple attribute
*
* @return void
*/
public function testTagWithAttribute(): void
{
$element = '<c-youtube src="RLdsCL4RDf8"></c-youtube>';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
HTML;
$this->assertSame($expected, $result);
}
/**
* Test self-closing tag variant
*
* @return void
*/
public function testTagWithAttributeSelfClosing(): void
{
$element = '<c-youtube src="RLdsCL4RDf8" />';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
HTML;
$this->assertSame($expected, $result);
}
/**
* Test multiple self-closing tag variants
*
* @return void
*/
public function testMultipleTagsWithAttributeSelfClosing(): void
{
$element = '<c-youtube src="RLdsCL4RDf8" /><c-youtube src="RLdsCL4RDf8" />';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe> <iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
HTML;
$this->assertSame($expected, $result);
}
/**
* Test tags work with multiple attributes
*
* @return void
*/
public function testTagWithMultipleAttributes(): void
{
$element = '<c-button type="primary" text="Click me" url="/something/stupid" />';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<a href="/something/stupid" class="c-button c-button--primary">Click me</a>
HTML;
$this->assertSame($expected, $result);
}
/**
* Test tags work in other custom directories
*
* @return void
*/
public function testTagInSubFolder(): void
{
$element = '<c-github></c-github>';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
This is a render from a plugin tag
HTML;
$this->assertSame($expected, $result);
}
/**
* Test inner content is passed down to the tag and can be outputted
*
* @return void
*/
public function testTagWithInnerContent(): void
{
$element = '<c-github>Inner Content</c-github>';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
This is a render from a plugin tag
Inner Content
HTML;
$this->assertSame($expected, $result);
}
/**
* Test nested tags
*
* @return void
*/
public function testTagsNested(): void
{
$element = '<c-github><c-github></c-github></c-github>';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
This is a render from a plugin tag
<c-github/>
HTML;
$this->assertSame($expected, $result);
}
/**
* Make sure nested tags don't get rendered by default
*
* @return void
*/
public function testNestedContentDoesntRenderByDefault(): void
{
$element = '<c-nested />';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<c-github></c-github>
HTML;
$this->assertSame($expected, $result);
}
/**
* Test that sub-tags will also be rendered if the config
*
* @return void
*/
public function testNestedContentRendersWithConfig(): void
{
$element = '<c-nested />';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
'sniff_for_nested_tags' => true,
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
This is a render from a plugin tag
HTML;
$this->assertSame($expected, $result);
}
/**
* Test that sub-tags will trigger cache correctly
*
* @return void
*/
public function testNestedContentRendersWithCache(): void
{
$element = '<c-nested />';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
'sniff_for_nested_tags' => true,
'cache_tags' => true,
'cache_directory' => self::CACHE_DIR,
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
This is a render from a plugin tag
HTML;
$this->assertSame($expected, $result);
$result = $tagEngine->parse($element);
$this->assertSame($expected, $result);
}
/**
* Test tag variant and normal HTML
*
* @return void
*/
public function testTagWithAttributeAndNormalHTML(): void
{
$element = '<c-youtube src="RLdsCL4RDf8"></c-youtube><div>Test</div>';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe><div>Test</div>
HTML;
$this->assertSame($expected, $result);
}
/**
* Test self-closing tag variant and normal HTML
*
* @return void
*/
public function testTagWithAttributeSelfClosingAndNormalHTML(): void
{
$element = '<c-youtube src="RLdsCL4RDf8" /><div>Test</div><input type="text"/>';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe><div>Test</div><input type="text"/>
HTML;
$this->assertSame($expected, $result);
}
/**
* Test disabled tags
*
* @return void
*/
public function testDisabledTag(): void
{
$element = '<c-disabled />';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = '';
$this->assertSame($expected, $result);
}
/**
* Test no custom Tag
*
* @return void
*/
public function testNoCustomTag(): void
{
$element = '<div>This is a Test</div>';
$tagEngine = new TagEngine([
'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
]);
$result = $tagEngine->parse($element);
$expected = '<div>This is a Test</div>';
$this->assertSame($expected, $result);
}
/**
* Test output buffered
*
* @return void
*/
public function testOutputBuffered(): void
{
$element = '<c-github></c-github>';
$tagEngine = new TagEngine([
'tag_directories' => [
__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
__DIR__ . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
]);
ob_start();
echo $element;
$result = $tagEngine->parse();
$expected = <<<HTML
This is a render from a plugin tag
HTML;
$this->assertSame($expected, $result);
}
// public function testWithDivWrapped(): void
// {
// $element = '<div>
// <c-youtube src="RLdsCL4RDf8"/>
// </div>';
// $tagEngine = new TagEngine([
// 'tag_directories' => [__DIR__ . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR],
// 'sniff_for_nested_tags' => true,
// ]);
// $result = $tagEngine->parse($element);
// $expected = <<<HTML
//<div>
// <iframe width="560" height="315"
// src="https://www.youtube.com/embed/RLdsCL4RDf8"
// allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
// allowfullscreen>
// </iframe>
//</div>
//HTML;
// $this->assertSame($expected, $result);
// }
}