|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @phpVersion 8.4 |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Tester\Assert; |
| 10 | +use Tester\DomQuery; |
| 11 | + |
| 12 | +require __DIR__ . '/../bootstrap.php'; |
| 13 | + |
| 14 | + |
| 15 | +test('fromHtml() creates DomQuery from HTML string', function () { |
| 16 | + $dom = DomQuery::fromHtml('<div class="test"><p>Hello</p></div>'); |
| 17 | + Assert::type(DomQuery::class, $dom); |
| 18 | + Assert::true($dom->has('div')); |
| 19 | +}); |
| 20 | + |
| 21 | +test('fromHtml() handles HTML without root element', function () { |
| 22 | + $dom = DomQuery::fromHtml('Hello world'); |
| 23 | + Assert::type(DomQuery::class, $dom); |
| 24 | + Assert::contains('Hello world', (string) $dom->find('body')[0]); |
| 25 | +}); |
| 26 | + |
| 27 | +test('fromHtml() handles void elements correctly', function () { |
| 28 | + $dom = DomQuery::fromHtml('<div><source src="test.mp3"><wbr>test</div>'); |
| 29 | + Assert::true($dom->has('source')); |
| 30 | + Assert::true($dom->has('wbr')); |
| 31 | +}); |
| 32 | + |
| 33 | +test('fromHtml() handles script tags with </ inside', function () { |
| 34 | + $dom = DomQuery::fromHtml('<script>if (a</b) { alert("test"); }</script>'); |
| 35 | + Assert::true($dom->has('script')); |
| 36 | +}); |
| 37 | + |
| 38 | +test('find() returns matching elements', function () { |
| 39 | + $dom = DomQuery::fromHtml(' |
| 40 | + <div class="container"> |
| 41 | + <p class="first">First paragraph</p> |
| 42 | + <p class="second">Second paragraph</p> |
| 43 | + <span>Test span</span> |
| 44 | + </div> |
| 45 | + '); |
| 46 | + |
| 47 | + $paragraphs = $dom->find('p'); |
| 48 | + Assert::count(2, $paragraphs); |
| 49 | + Assert::contains('First paragraph', (string) $paragraphs[0]); |
| 50 | + |
| 51 | + $spans = $dom->find('span'); |
| 52 | + Assert::count(1, $spans); |
| 53 | + Assert::contains('Test span', (string) $spans[0]); |
| 54 | +}); |
| 55 | + |
| 56 | +test('find() supports complex CSS selectors', function () { |
| 57 | + $dom = DomQuery::fromHtml(' |
| 58 | + <div class="container"> |
| 59 | + <p class="first">First</p> |
| 60 | + <div class="wrapper"> |
| 61 | + <p class="second">Second</p> |
| 62 | + <p class="third">Third</p> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + '); |
| 66 | + |
| 67 | + $results = $dom->find('div.wrapper p'); |
| 68 | + Assert::count(2, $results); |
| 69 | + Assert::contains('Second', (string) $results[0]); |
| 70 | + |
| 71 | + $results = $dom->find('p.first + div'); |
| 72 | + Assert::count(1, $results); |
| 73 | + Assert::true($results[0]->has('p.second')); |
| 74 | +}); |
| 75 | + |
| 76 | +test('has() checks for existence of elements', function () { |
| 77 | + $dom = DomQuery::fromHtml(' |
| 78 | + <div class="test"> |
| 79 | + <span class="inner">Test</span> |
| 80 | + </div> |
| 81 | + '); |
| 82 | + |
| 83 | + Assert::true($dom->has('span.inner')); |
| 84 | + Assert::true($dom->has('div.test')); |
| 85 | + Assert::false($dom->has('p')); |
| 86 | + Assert::false($dom->has('.nonexistent')); |
| 87 | +}); |
| 88 | + |
| 89 | +test('matches() checks if element matches selector', function () { |
| 90 | + $dom = DomQuery::fromHtml('<div class="test"><p class="para">Test</p></div>'); |
| 91 | + $para = $dom->find('p')[0]; |
| 92 | + |
| 93 | + Assert::true($para->matches('p')); |
| 94 | + Assert::true($para->matches('.para')); |
| 95 | + Assert::true($para->matches('p.para')); |
| 96 | + Assert::false($para->matches('div')); |
| 97 | + Assert::false($para->matches('.test')); |
| 98 | +}); |
| 99 | + |
| 100 | +test('find() returns empty array for no matches', function () { |
| 101 | + $dom = DomQuery::fromHtml('<div></div>'); |
| 102 | + Assert::same([], $dom->find('nonexistent')); |
| 103 | +}); |
| 104 | + |
| 105 | +test('handles malformed HTML gracefully', function () { |
| 106 | + Assert::error(function () use (&$dom) { |
| 107 | + $dom = DomQuery::fromHtml('<div><p>Unclosed paragraph<span>Test</div>'); |
| 108 | + }, E_USER_WARNING, 'Tester\DomQuery::fromHtml: tree error unexpected-element-in-open-elements-stack%a%'); |
| 109 | + Assert::true($dom->has('div')); |
| 110 | + Assert::true($dom->has('p')); |
| 111 | + Assert::true($dom->has('span')); |
| 112 | +}); |
| 113 | + |
| 114 | +test('handles HTML entities in attributes', function () { |
| 115 | + $dom = DomQuery::fromHtml('<div data-test=""quoted"">Test</div>'); |
| 116 | + Assert::true($dom->find('div')[0]->matches('[data-test="\\"quoted\\""]')); |
| 117 | +}); |
| 118 | + |
| 119 | +test('handles UTF-8', function () { |
| 120 | + $q = DomQuery::fromHtml('<p>žluťoučký</p>'); |
| 121 | + Assert::same('žluťoučký', (string) $q->find('p')[0]); |
| 122 | +}); |
0 commit comments