|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\ETL\Tests\Unit\Function; |
| 6 | + |
| 7 | +use function Flow\ETL\DSL\{config, flow_context, ref, row}; |
| 8 | +use Dom\{Element, HTMLDocument}; |
| 9 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +final class HTMLQuerySelectorAllTest extends TestCase |
| 13 | +{ |
| 14 | + #[RequiresPhp('>= 8.4')] |
| 15 | + public function test_getting_elements_for_given_path() : void |
| 16 | + { |
| 17 | + $html = HTMLDocument::createFromString('<!DOCTYPE html><html><head></head><body><div><span>foobar</span></div></body></html>'); |
| 18 | + |
| 19 | + /** @var array<mixed> $result */ |
| 20 | + $result = ref('value')->htmlQuerySelectorAll('body div span')->eval(row(flow_context(config())->entryFactory()->create('value', $html))); |
| 21 | + |
| 22 | + self::assertCount(1, $result); |
| 23 | + |
| 24 | + /* @phpstan-ignore-next-line */ |
| 25 | + self::assertInstanceOf(Element::class, $result[0]); |
| 26 | + } |
| 27 | + |
| 28 | + #[RequiresPhp('< 8.4')] |
| 29 | + public function test_getting_null_for_older_versions() : void |
| 30 | + { |
| 31 | + $this->expectException(\RuntimeException::class); |
| 32 | + |
| 33 | + ref('value')->htmlQuerySelectorAll('body div p')->eval(row(flow_context(config())->entryFactory()->create('value', ''))); |
| 34 | + } |
| 35 | + |
| 36 | + #[RequiresPhp('>= 8.4')] |
| 37 | + public function test_getting_null_when_nothing_found() : void |
| 38 | + { |
| 39 | + $html = HTMLDocument::createFromString('<!DOCTYPE html><html><head></head><body><div><span>foobar</span></div></body></html>'); |
| 40 | + |
| 41 | + $result = ref('value')->htmlQuerySelectorAll('body div p')->eval(row(flow_context(config())->entryFactory()->create('value', $html))); |
| 42 | + |
| 43 | + self::assertNull($result); |
| 44 | + } |
| 45 | + |
| 46 | + #[RequiresPhp('>= 8.4')] |
| 47 | + public function test_invalid_value() : void |
| 48 | + { |
| 49 | + $result = ref('value')->htmlQuerySelectorAll('body div span')->eval(row(flow_context(config())->entryFactory()->create('value', ''))); |
| 50 | + |
| 51 | + self::assertNull($result); |
| 52 | + } |
| 53 | +} |
0 commit comments