|
11 | 11 | use Html\Enum\TargetEnum; |
12 | 12 | use Html\TemplateGenerator\HTMLGenerator; |
13 | 13 |
|
14 | | -// uses(\Html\Trait\GlobalAttributesTrait::class); |
15 | | - |
16 | 14 | beforeEach(function () { |
17 | 15 | $this->document = HTMLDocumentDelegator::createEmpty(); |
18 | 16 | $this->delegator = Anchor::create($this->document); |
|
401 | 399 | expect($this->delegator->getOwnerDocument()) |
402 | 400 | ->toBe($this->document); |
403 | 401 | }); |
| 402 | + |
| 403 | +test('constructor with invalid renderer', function () { |
| 404 | + $mockRenderer = new class implements \Html\Interface\TemplateGeneratorInterface { |
| 405 | + public function getExtension(): string { |
| 406 | + return 'html'; |
| 407 | + } |
| 408 | + public function getNamePattern(): string { |
| 409 | + return '*.html'; |
| 410 | + } |
| 411 | + public function canRenderElements(): bool { |
| 412 | + return false; |
| 413 | + } |
| 414 | + public function canRenderDocuments(): bool { |
| 415 | + return true; |
| 416 | + } |
| 417 | + public function isTemplated(): bool { |
| 418 | + return false; |
| 419 | + } |
| 420 | + public function render($elementOrDocument): ?string { |
| 421 | + return ''; |
| 422 | + } |
| 423 | + }; |
| 424 | + |
| 425 | + $element = $this->document->createElement('div'); |
| 426 | + |
| 427 | + $this->expectException(InvalidArgumentException::class); |
| 428 | + $this->expectExceptionMessage('The given renderer cannot render elements.'); |
| 429 | + |
| 430 | + new HTMLElementDelegator($element->delegated, $mockRenderer); |
| 431 | +}); |
| 432 | + |
| 433 | +test('set with union type enum handling', function () { |
| 434 | + // This test should trigger the union type enum handling in __set |
| 435 | + // We need a property that has a union type with BackedEnum |
| 436 | + $element = Body::create($this->document); |
| 437 | + |
| 438 | + // Try to set a property that doesn't exist to trigger the union type handling |
| 439 | + $element->nonExistentProperty = 'test'; |
| 440 | + expect($element->getAttribute('nonexistentproperty')) |
| 441 | + ->toBe('test'); |
| 442 | +}); |
| 443 | + |
| 444 | +test('append child with different owner document throws exception', function () { |
| 445 | + $otherDocument = HTMLDocumentDelegator::createEmpty(); |
| 446 | + $child = Anchor::create($otherDocument); |
| 447 | + |
| 448 | + $this->expectException(InvalidArgumentException::class); |
| 449 | + $this->expectExceptionMessage('The child element must belong to the same document as the parent element.'); |
| 450 | + |
| 451 | + $this->delegator->appendChild($child); |
| 452 | +}); |
| 453 | + |
| 454 | +test('remove child with different owner document throws exception', function () { |
| 455 | + $otherDocument = HTMLDocumentDelegator::createEmpty(); |
| 456 | + $child = Anchor::create($otherDocument); |
| 457 | + |
| 458 | + $this->expectException(InvalidArgumentException::class); |
| 459 | + $this->expectExceptionMessage('The child element must belong to the same document as the parent element.'); |
| 460 | + |
| 461 | + $this->delegator->removeChild($child); |
| 462 | +}); |
| 463 | + |
| 464 | +test('remove child with DOM Text', function () { |
| 465 | + $textNode = $this->document->createTextNode('test text'); |
| 466 | + $element = $this->document->createElement('div'); |
| 467 | + $element->appendChild($textNode); |
| 468 | + |
| 469 | + expect($element->childNodes->length)->toBe(1); |
| 470 | + |
| 471 | + $element->removeChild($textNode); |
| 472 | + expect($element->childNodes->length)->toBe(0); |
| 473 | +}); |
| 474 | + |
| 475 | +test('replace child with different owner document throws exception', function () { |
| 476 | + $otherDocument = HTMLDocumentDelegator::createEmpty(); |
| 477 | + $child1 = Anchor::create($this->document); |
| 478 | + $child2 = Anchor::create($otherDocument); |
| 479 | + |
| 480 | + $this->delegator->appendChild($child1); |
| 481 | + |
| 482 | + $this->expectException(InvalidArgumentException::class); |
| 483 | + $this->expectExceptionMessage('The node element must belong to the same document as the parent element.'); |
| 484 | + |
| 485 | + $this->delegator->replaceChild($child2, $child1); |
| 486 | +}); |
0 commit comments