|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\Types\Tests\Unit\Value; |
| 6 | + |
| 7 | +use Flow\Types\Exception\InvalidArgumentException; |
| 8 | +use Flow\Types\Value\HTMLDocument; |
| 9 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +final class HTMLDocumentTest extends TestCase |
| 13 | +{ |
| 14 | + #[RequiresPhp('>= 8.4')] |
| 15 | + public function test_create_with_dom_document_html_on_newer() : void |
| 16 | + { |
| 17 | + $doc = \Dom\HTMLDocument::createFromString('<html><body><div><span>bar</span></div></body></html>'); |
| 18 | + |
| 19 | + $document = new HTMLDocument($doc); |
| 20 | + |
| 21 | + self::assertSame('<html><head></head><body><div><span>bar</span></div></body></html>', (string) $document); |
| 22 | + } |
| 23 | + |
| 24 | + #[RequiresPhp('<= 8.4')] |
| 25 | + public function test_create_with_dom_document_html_on_old() : void |
| 26 | + { |
| 27 | + $doc = new \DOMDocument(); |
| 28 | + $doc->loadHTML($html = '<html><body><div><span>bar</span></div></body></html>'); |
| 29 | + |
| 30 | + $document = new HTMLDocument($doc); |
| 31 | + |
| 32 | + self::assertSame($html, (string) $document); |
| 33 | + } |
| 34 | + |
| 35 | + #[RequiresPhp('>= 8.4')] |
| 36 | + public function test_create_with_invalid_html_on_newer() : void |
| 37 | + { |
| 38 | + $document = new HTMLDocument('invalid'); |
| 39 | + |
| 40 | + self::assertSame('invalid', (string) $document); |
| 41 | + } |
| 42 | + |
| 43 | + #[RequiresPhp('<= 8.4')] |
| 44 | + public function test_create_with_invalid_html_on_old() : void |
| 45 | + { |
| 46 | + $document = new HTMLDocument('invalid'); |
| 47 | + |
| 48 | + self::assertSame('<p>invalid</p>', (string) $document); |
| 49 | + } |
| 50 | + |
| 51 | + #[RequiresPhp('>= 8.4')] |
| 52 | + public function test_create_with_proper_html_on_newer() : void |
| 53 | + { |
| 54 | + $html = '<html><body><div><span>bar</span></div></body></html>'; |
| 55 | + $document = new HTMLDocument($html); |
| 56 | + |
| 57 | + self::assertSame($html, (string) $document); |
| 58 | + } |
| 59 | + |
| 60 | + #[RequiresPhp('<= 8.4')] |
| 61 | + public function test_create_with_proper_html_on_old() : void |
| 62 | + { |
| 63 | + $html = '<html><body><div><span>bar</span></div></body></html>'; |
| 64 | + $document = new HTMLDocument($html); |
| 65 | + |
| 66 | + self::assertSame($html, (string) $document); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_with_random_object() : void |
| 70 | + { |
| 71 | + $this->expectException(InvalidArgumentException::class); |
| 72 | + |
| 73 | + new HTMLDocument(new \stdClass()); |
| 74 | + } |
| 75 | +} |
0 commit comments