|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file contains the TemplateGeneratorTest.php |
| 5 | + * |
| 6 | + * @package PHPDraft\Out |
| 7 | + * @author Sean Molenaar<sean@seanmolenaar.eu> |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHPDraft\Out\Tests; |
| 11 | + |
| 12 | +use Lunr\Halo\LunrBaseTest; |
| 13 | +use PHPDraft\Out\HtmlTemplateRenderer; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class TemplateGeneratorTest |
| 17 | + * |
| 18 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer |
| 19 | + */ |
| 20 | +class HtmlTemplateRendererTest extends LunrBaseTest |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var HtmlTemplateRenderer |
| 24 | + */ |
| 25 | + protected HtmlTemplateRenderer $class; |
| 26 | + |
| 27 | + public static function parsableDataProvider(): array |
| 28 | + { |
| 29 | + $return = []; |
| 30 | + $expected_base = [ |
| 31 | + 'COLOR_1' => 'green', |
| 32 | + 'COLOR_2' => 'light_green', |
| 33 | + ]; |
| 34 | + |
| 35 | + |
| 36 | + $return['empty'] = [ ['content' => []], $expected_base ]; |
| 37 | + |
| 38 | + $return['only title'] = [ |
| 39 | + [ |
| 40 | + 'content' => [ |
| 41 | + [ |
| 42 | + 'meta' => [ |
| 43 | + 'title' => [ 'content' => 'Title' ], |
| 44 | + ], |
| 45 | + 'content' => [], |
| 46 | + ], |
| 47 | + |
| 48 | + ], |
| 49 | + ], |
| 50 | + $expected_base + ['TITLE' => 'Title'], |
| 51 | + ]; |
| 52 | + |
| 53 | + $return['title and metadata'] = [ |
| 54 | + [ |
| 55 | + 'content' => [ |
| 56 | + [ |
| 57 | + 'meta' => [ |
| 58 | + 'title' => [ 'content' => 'Title' ], |
| 59 | + ], |
| 60 | + 'attributes' => [ |
| 61 | + 'metadata' => [ |
| 62 | + 'content' => [ |
| 63 | + [ |
| 64 | + 'content' => [ |
| 65 | + 'key' => [ 'content' => 'Some_key' ], |
| 66 | + 'value' => [ 'content' => 'Value' ], |
| 67 | + ] |
| 68 | + ], |
| 69 | + [ |
| 70 | + 'content' => [ |
| 71 | + 'key' => [ 'content' => 'Some_key2' ], |
| 72 | + 'value' => [ 'content' => 'Value2' ], |
| 73 | + ] |
| 74 | + ] |
| 75 | + ] |
| 76 | + ] |
| 77 | + ], |
| 78 | + 'content' => [], |
| 79 | + ], |
| 80 | + |
| 81 | + ], |
| 82 | + ], |
| 83 | + $expected_base + ['TITLE' => 'Title', 'Some_key' => 'Value', 'Some_key2' => 'Value2'], |
| 84 | + ]; |
| 85 | + |
| 86 | + $return['title and metadata and description'] = [ |
| 87 | + [ |
| 88 | + 'content' => [ |
| 89 | + [ |
| 90 | + 'meta' => [ |
| 91 | + 'title' => [ 'content' => 'Title' ], |
| 92 | + ], |
| 93 | + 'attributes' => [ |
| 94 | + 'metadata' => [ |
| 95 | + 'content' => [ |
| 96 | + [ |
| 97 | + 'content' => [ |
| 98 | + 'key' => [ 'content' => 'Some_key' ], |
| 99 | + 'value' => [ 'content' => 'Value' ], |
| 100 | + ] |
| 101 | + ], |
| 102 | + [ |
| 103 | + 'content' => [ |
| 104 | + 'key' => [ 'content' => 'Some_key2' ], |
| 105 | + 'value' => [ 'content' => 'Value2' ], |
| 106 | + ] |
| 107 | + ] |
| 108 | + ] |
| 109 | + ] |
| 110 | + ], |
| 111 | + 'content' => [ |
| 112 | + [ |
| 113 | + 'element' => 'copy', |
| 114 | + 'content' => 'Some description', |
| 115 | + ] |
| 116 | + ], |
| 117 | + ], |
| 118 | + |
| 119 | + ], |
| 120 | + ], |
| 121 | + $expected_base + ['TITLE' => 'Title', 'Some_key' => 'Value', 'Some_key2' => 'Value2', 'DESC' => 'Some description'], |
| 122 | + ]; |
| 123 | + |
| 124 | + return $return; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Provide HTTP status codes |
| 129 | + * |
| 130 | + * @return array<int, array<int, int|string>> |
| 131 | + */ |
| 132 | + public static function responseStatusProvider(): array |
| 133 | + { |
| 134 | + $return = []; |
| 135 | + |
| 136 | + $return[] = [ 200, 'text-success' ]; |
| 137 | + $return[] = [ 204, 'text-success' ]; |
| 138 | + $return[] = [ 304, 'text-warning' ]; |
| 139 | + $return[] = [ 404, 'text-error' ]; |
| 140 | + $return[] = [ 501, 'text-error' ]; |
| 141 | + |
| 142 | + return $return; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Provide HTTP methods |
| 147 | + * |
| 148 | + * @return array<int, array<int, string>> |
| 149 | + */ |
| 150 | + public static function requestMethodProvider(): array |
| 151 | + { |
| 152 | + $return = []; |
| 153 | + |
| 154 | + $return[] = [ 'POST', 'fas POST fa-plus-square' ]; |
| 155 | + $return[] = [ 'post', 'fas POST fa-plus-square' ]; |
| 156 | + $return[] = [ 'get', 'fas GET fa-arrow-circle-down' ]; |
| 157 | + $return[] = [ 'put', 'fas PUT fa-pen-square' ]; |
| 158 | + $return[] = [ 'delete', 'fas DELETE fa-minus-square' ]; |
| 159 | + $return[] = [ 'head', 'fas HEAD fa-info' ]; |
| 160 | + $return[] = [ 'options', 'fas OPTIONS fa-sliders-h' ]; |
| 161 | + $return[] = [ 'PATCH', 'fas PATCH fa-band-aid' ]; |
| 162 | + $return[] = [ 'connect', 'fas CONNECT fa-ethernet' ]; |
| 163 | + $return[] = [ 'trace', 'fas TRACE fa-route' ]; |
| 164 | + $return[] = [ 'cow', 'fas COW' ]; |
| 165 | + |
| 166 | + return $return; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Set up tests |
| 171 | + * |
| 172 | + * @return void |
| 173 | + */ |
| 174 | + public function setUp(): void |
| 175 | + { |
| 176 | + $this->class = new HtmlTemplateRenderer('default', 'none'); |
| 177 | + $this->baseSetUp($this->class); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Test if the value the class is initialized with is correct |
| 182 | + * |
| 183 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer |
| 184 | + */ |
| 185 | + public function testSetupCorrectly(): void |
| 186 | + { |
| 187 | + $this->assertSame('default', $this->get_reflection_property_value('template')); |
| 188 | + $this->assertEquals('none', $this->get_reflection_property_value('image')); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Test if the value the class is initialized with is correct |
| 193 | + * |
| 194 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::strip_link_spaces |
| 195 | + */ |
| 196 | + public function testStripSpaces(): void |
| 197 | + { |
| 198 | + $return = $this->class->strip_link_spaces('hello world'); |
| 199 | + $this->assertEquals('hello-world', $return); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Test if the value the class is initialized with is correct |
| 204 | + * |
| 205 | + * @dataProvider responseStatusProvider |
| 206 | + * |
| 207 | + * @param int $code HTTP code |
| 208 | + * @param string $text Class to return |
| 209 | + * |
| 210 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::get_response_status |
| 211 | + */ |
| 212 | + public function testResponseStatus(int $code, string $text): void |
| 213 | + { |
| 214 | + $return = HtmlTemplateRenderer::get_response_status($code); |
| 215 | + $this->assertEquals($text, $return); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Test if the value the class is initialized with is correct |
| 220 | + * |
| 221 | + * @dataProvider requestMethodProvider |
| 222 | + * |
| 223 | + * @param string $method HTTP Method |
| 224 | + * @param string $text Class to return |
| 225 | + * |
| 226 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::get_method_icon |
| 227 | + */ |
| 228 | + public function testRequestMethod(string $method, string $text): void |
| 229 | + { |
| 230 | + $return = HtmlTemplateRenderer::get_method_icon($method); |
| 231 | + $this->assertEquals($text, $return); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Test if the value the class is initialized with is correct |
| 236 | + * |
| 237 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::find_include_file |
| 238 | + */ |
| 239 | + public function testIncludeFileDefault(): void |
| 240 | + { |
| 241 | + $return = $this->class->find_include_file('default'); |
| 242 | + $this->assertEquals('PHPDraft/Out/HTML/default/main.twig', $return); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Test if the value the class is initialized with is correct |
| 247 | + * |
| 248 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::find_include_file |
| 249 | + */ |
| 250 | + public function testIncludeFileFallback(): void |
| 251 | + { |
| 252 | + $return = $this->class->find_include_file('gfsdfdsf'); |
| 253 | + $this->assertEquals('PHPDraft/Out/HTML/default/main.twig', $return); |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Test if the value the class is initialized with is correct |
| 258 | + * |
| 259 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::find_include_file |
| 260 | + */ |
| 261 | + public function testIncludeFileNone(): void |
| 262 | + { |
| 263 | + $return = $this->class->find_include_file('gfsdfdsf', 'xyz'); |
| 264 | + $this->assertEquals(NULL, $return); |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Test if the value the class is initialized with is correct |
| 269 | + * |
| 270 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::find_include_file |
| 271 | + */ |
| 272 | + public function testIncludeFileSingle(): void |
| 273 | + { |
| 274 | + set_include_path(TEST_STATICS . '/include_single:' . get_include_path()); |
| 275 | + $return = $this->class->find_include_file('hello', 'txt'); |
| 276 | + $this->assertEquals('hello.txt', $return); |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * Test if the value the class is initialized with is correct |
| 281 | + * |
| 282 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::find_include_file |
| 283 | + */ |
| 284 | + public function testIncludeFileMultiple(): void |
| 285 | + { |
| 286 | + set_include_path(TEST_STATICS . '/include_folders:' . get_include_path()); |
| 287 | + $return = $this->class->find_include_file('hello', 'txt'); |
| 288 | + $this->assertEquals('hello/hello.txt', $return); |
| 289 | + |
| 290 | + $return = $this->class->find_include_file('test', 'txt'); |
| 291 | + $this->assertEquals('templates/test.txt', $return); |
| 292 | + |
| 293 | + $return = $this->class->find_include_file('text', 'txt'); |
| 294 | + $this->assertEquals('templates/text/text.txt', $return); |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::get |
| 299 | + */ |
| 300 | + public function testGetTemplateFailsEmpty(): void |
| 301 | + { |
| 302 | + $this->expectException('PHPDraft\Parse\ExecutionException'); |
| 303 | + $this->expectExceptionMessage('Couldn\'t find template \'cow\''); |
| 304 | + $this->set_reflection_property_value('template', 'cow'); |
| 305 | + $json = '{"content": [{"content": "hello"}]}'; |
| 306 | + |
| 307 | + $this->assertStringEqualsFile(TEST_STATICS . '/empty_html_template', $this->class->get(json_decode($json))); |
| 308 | + } |
| 309 | + |
| 310 | + /** |
| 311 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::get |
| 312 | + * @group twig |
| 313 | + */ |
| 314 | + public function testGetTemplate(): void |
| 315 | + { |
| 316 | + $json = '{"content": [{"content": "hello"}]}'; |
| 317 | + |
| 318 | + $this->assertStringEqualsFile(TEST_STATICS . '/empty_html_template', $this->class->get(json_decode($json))); |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::get |
| 323 | + * @group twig |
| 324 | + */ |
| 325 | + public function testGetTemplateSorting(): void |
| 326 | + { |
| 327 | + $this->set_reflection_property_value('sorting', 3); |
| 328 | + $json = '{"content": [{"content": "hello"}]}'; |
| 329 | + |
| 330 | + $this->assertStringEqualsFile(TEST_STATICS . '/empty_html_template', $this->class->get(json_decode($json))); |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::get |
| 335 | + * @group twig |
| 336 | + */ |
| 337 | + public function testGetTemplateMetaData(): void |
| 338 | + { |
| 339 | + $this->set_reflection_property_value('sorting', 3); |
| 340 | + $json = <<<'TAG' |
| 341 | +{"content": [{"content": [], "attributes": { |
| 342 | +"metadata": {"content": [ |
| 343 | +{"content":{"key": {"content": "key"}, "value": {"content": "value"}}} |
| 344 | +]}, |
| 345 | +"meta": {"title": {"content": "title"}} |
| 346 | +}}]} |
| 347 | +TAG; |
| 348 | + |
| 349 | + $this->assertStringEqualsFile(TEST_STATICS . '/basic_html_template', $this->class->get(json_decode($json))); |
| 350 | + } |
| 351 | + |
| 352 | + /** |
| 353 | + * @covers \PHPDraft\Out\HtmlTemplateRenderer::get |
| 354 | + * @group twig |
| 355 | + */ |
| 356 | + public function testGetTemplateCategories(): void |
| 357 | + { |
| 358 | + $this->set_reflection_property_value('sorting', 3); |
| 359 | + $json = <<<'TAG' |
| 360 | +{"content": [ |
| 361 | +{"content": [{"element": "copy", "content": "__desc__"}, {"element": "category", "content": []}], |
| 362 | + "attributes": { |
| 363 | +"metadata": {"content": [ |
| 364 | +{"content":{"key": {"content": "key"}, "value": {"content": "value"}}} |
| 365 | +]}, |
| 366 | +"meta": {"title": {"content": "title"}} |
| 367 | +}}]} |
| 368 | +TAG; |
| 369 | + |
| 370 | + $this->assertStringEqualsFile(TEST_STATICS . '/full_html_template', $this->class->get(json_decode($json))); |
| 371 | + } |
| 372 | + |
| 373 | + /** |
| 374 | + * @covers \PHPDraft\Out\BaseTemplateRenderer::parse_base_data |
| 375 | + * @dataProvider parsableDataProvider |
| 376 | + */ |
| 377 | + public function testParseBaseData(array $input, array $expected): void |
| 378 | + { |
| 379 | + $method = $this->get_reflection_method('parse_base_data'); |
| 380 | + $method->invokeArgs($this->class, [ json_decode(json_encode($input)) ]); |
| 381 | + |
| 382 | + $this->assertPropertyEquals('base_data', $expected); |
| 383 | + } |
| 384 | +} |
0 commit comments