|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\ETL\Tests\Integration\Function; |
| 6 | + |
| 7 | +use function Flow\ETL\DSL\{df, from_rows, html_entry, ref, row, rows}; |
| 8 | +use Dom\Element; |
| 9 | +use Flow\Types\Value\HTMLDocument; |
| 10 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +final class HTMLQuerySelectorAllTest extends TestCase |
| 14 | +{ |
| 15 | + private HTMLDocument $html; |
| 16 | + |
| 17 | + protected function setUp() : void |
| 18 | + { |
| 19 | + $this->html = new HTMLDocument('<!DOCTYPE html><html><head></head><body><div><span>foobar</span></div></body></html>'); |
| 20 | + } |
| 21 | + |
| 22 | + #[RequiresPhp('<= 8.4')] |
| 23 | + public function test_fails_with_older_php() : void |
| 24 | + { |
| 25 | + $this->expectException(\RuntimeException::class); |
| 26 | + |
| 27 | + df() |
| 28 | + ->read(from_rows(rows(row(html_entry('html_raw', $this->html))))) |
| 29 | + ->withEntry('html', ref('html_raw')->htmlQuerySelectorAll('body div span')) |
| 30 | + ->fetch(); |
| 31 | + } |
| 32 | + |
| 33 | + #[RequiresPhp('>= 8.4')] |
| 34 | + public function test_invalid_query_all_on_html_document() : void |
| 35 | + { |
| 36 | + $rows = df() |
| 37 | + ->read(from_rows(rows(row(html_entry('html_raw', $this->html))))) |
| 38 | + ->withEntry('html', ref('html_raw')->htmlQuerySelectorAll('body div p')) |
| 39 | + ->drop('html_raw') |
| 40 | + ->fetch(); |
| 41 | + |
| 42 | + $results = $rows->toArray()[0]['html'] ?? []; |
| 43 | + |
| 44 | + /* @phpstan-ignore-next-line */ |
| 45 | + self::assertCount(0, $results); |
| 46 | + } |
| 47 | + |
| 48 | + #[RequiresPhp('>= 8.4')] |
| 49 | + public function test_valid_query_all_on_html_document() : void |
| 50 | + { |
| 51 | + $rows = df() |
| 52 | + ->read(from_rows(rows(row(html_entry('html_raw', $this->html))))) |
| 53 | + ->withEntry('html', ref('html_raw')->htmlQuerySelectorAll('body div span')) |
| 54 | + ->drop('html_raw') |
| 55 | + ->fetch(); |
| 56 | + |
| 57 | + $results = $rows->toArray()[0]['html'] ?? []; |
| 58 | + |
| 59 | + /* @phpstan-ignore-next-line */ |
| 60 | + self::assertCount(1, $results); |
| 61 | + /* @phpstan-ignore-next-line */ |
| 62 | + self::assertContainsOnlyInstancesOf(Element::class, $results); |
| 63 | + } |
| 64 | +} |
0 commit comments