|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: comments HTML test |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Latte\ContentType; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | +require __DIR__ . '/../bootstrap.php'; |
| 13 | + |
| 14 | + |
| 15 | +class ContentTypeLoader implements Latte\Loader |
| 16 | +{ |
| 17 | + private $contents = [ |
| 18 | + 'file.js' => '// JavaScript <img> {=latte} <script></script>', |
| 19 | + 'file.txt' => '// text <img> {=latte} <script></script>', |
| 20 | + 'file.html' => '// HTML <img> {=latte} <script></script>', |
| 21 | + ]; |
| 22 | + |
| 23 | + |
| 24 | + public function load(string $name): Latte\LoadedContent |
| 25 | + { |
| 26 | + if (!isset($this->contents[$name])) { |
| 27 | + return new Latte\LoadedContent($name); |
| 28 | + } |
| 29 | + [$contentType, $static] = (new Latte\Loaders\FileLoader)->detectContentType($name); |
| 30 | + return new Latte\LoadedContent( |
| 31 | + $this->contents[$name], |
| 32 | + contentType: $contentType, |
| 33 | + static: $static, |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public function getReferredName(string $name, string $referringName): string |
| 39 | + { |
| 40 | + return $name; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public function getUniqueId(string $name): string |
| 45 | + { |
| 46 | + return $name; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +$latte = new Latte\Engine; |
| 52 | +$latte->setLoader(new ContentTypeLoader); |
| 53 | +$latte->setAutoRefresh(); |
| 54 | + |
| 55 | +Assert::same( |
| 56 | + '// JavaScript <img> {=latte} <script></script>', |
| 57 | + $latte->renderToString('{include file.js}'), |
| 58 | +); |
| 59 | + |
| 60 | +Assert::same( |
| 61 | + '<div> // HTML <img> {=latte} <script></script> </div>', |
| 62 | + $latte->renderToString('<div> {include file.html} </div>'), |
| 63 | +); |
| 64 | + |
| 65 | +Assert::same( |
| 66 | + '<div title="// HTML {=latte} "></div>', |
| 67 | + $latte->renderToString('<div title="{include file.html}"></div>'), |
| 68 | +); |
| 69 | + |
| 70 | +// <script> |
| 71 | +Assert::same( |
| 72 | + '<script> // JavaScript <img> {=latte} <script><\/script> </script>', |
| 73 | + $latte->renderToString('<script> {include file.js} </script>'), |
| 74 | +); |
| 75 | + |
| 76 | +Assert::exception( |
| 77 | + fn() => $latte->renderToString('<script> {include file.txt} </script>'), |
| 78 | + Latte\RuntimeException::class, |
| 79 | + "Including 'file.txt' with content type TEXT into incompatible type HTML/RAW/JS.", |
| 80 | +); |
| 81 | + |
| 82 | +Assert::exception( |
| 83 | + fn() => $latte->renderToString('<script> {include file.html} </script>'), |
| 84 | + Latte\RuntimeException::class, |
| 85 | + "Including 'file.html' with content type HTML into incompatible type HTML/RAW/JS.", |
| 86 | +); |
| 87 | + |
| 88 | +// HTML to Text |
| 89 | +$latte->setContentType(ContentType::Text); |
| 90 | +Assert::same( |
| 91 | + '* // HTML {=latte} *', |
| 92 | + $latte->renderToString('* {include file.html} *'), |
| 93 | +); |
0 commit comments