Skip to content

Commit 90ccc80

Browse files
stloydnorberttech
authored andcommitted
Add a new DOMElementParent function
1 parent f536522 commit 90ccc80

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/core/etl/src/Flow/ETL/Function/DOMElementParent.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,14 @@ public function eval(Row $row, FlowContext $context) : \DOMNode|HTMLElement|null
3636
$node = $node->documentElement;
3737
}
3838

39-
if (\is_array($node) && \count($node)) {
40-
$node = \reset($node);
41-
}
42-
4339
if ($node === null) {
44-
return $context->functions()->invalidResult(new InvalidArgumentException('DOMElementParent requires non-null DOMNode'));
40+
return $context->functions()->invalidResult(new InvalidArgumentException('DOMElementParent requires non-null DOMNode or HTMLElement.'));
4541
}
4642

4743
if ($node instanceof HTMLElement) {
4844
return $node->parentElement;
4945
}
5046

51-
if ($node instanceof \DOMNode) {
52-
return $node->parentNode;
53-
}
54-
55-
return null;
47+
return $node->parentNode;
5648
}
5749
}

src/core/etl/tests/Flow/ETL/Tests/Unit/Function/DOMElementParentTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,27 @@
66

77
use function Flow\ETL\DSL\{config, flow_context, ref, row};
88
use Dom\HTMLDocument;
9+
use Flow\ETL\Exception\InvalidArgumentException;
10+
use Flow\ETL\Function\ExecutionMode;
911
use PHPUnit\Framework\Attributes\RequiresPhp;
1012
use PHPUnit\Framework\TestCase;
1113

1214
final class DOMElementParentTest extends TestCase
1315
{
16+
#[RequiresPhp('>= 8.4')]
17+
public function test_html_fails_when_parent_not_available_in_strict_mode() : void
18+
{
19+
$element = HTMLDocument::createFromString('<span>bar</span>', \LIBXML_HTML_NOIMPLIED | \LIBXML_NOERROR);
20+
21+
$context = flow_context(config());
22+
$context->functions()->setMode(ExecutionMode::STRICT);
23+
24+
$this->expectException(InvalidArgumentException::class);
25+
$this->expectExceptionMessage('DOMElementParent requires non-null DOMNode or HTMLElement.');
26+
27+
ref('value')->domElementParent()->eval(row($context->entryFactory()->create('value', $element->documentElement->parentElement)), $context);
28+
}
29+
1430
#[RequiresPhp('>= 8.4')]
1531
public function test_html_getting_parent_element() : void
1632
{
@@ -32,6 +48,23 @@ public function test_html_getting_parent_element_when_not_available() : void
3248
);
3349
}
3450

51+
public function test_xml_fails_when_parent_not_available_in_strict_mode() : void
52+
{
53+
$xml = new \DOMDocument();
54+
$xml->loadXML('<root>foobar</root>');
55+
56+
$context = flow_context(config());
57+
$context->functions()->setMode(ExecutionMode::STRICT);
58+
59+
$this->expectException(InvalidArgumentException::class);
60+
$this->expectExceptionMessage('DOMElementParent requires non-null DOMNode or HTMLElement.');
61+
62+
self::assertEquals(
63+
$xml,
64+
ref('value')->domElementParent()->eval(row($context->entryFactory()->create('value', $xml->parentNode)), $context)
65+
);
66+
}
67+
3568
public function test_xml_getting_parent_element() : void
3669
{
3770
$xml = new \DOMDocument();
@@ -54,4 +87,15 @@ public function test_xml_getting_parent_element_when_not_available() : void
5487
ref('value')->domElementParent()->eval(row(flow_context(config())->entryFactory()->create('value', $xml->documentElement)), flow_context())
5588
);
5689
}
90+
91+
public function test_xml_getting_parent_element_when_passing_document() : void
92+
{
93+
$xml = new \DOMDocument();
94+
$xml->loadXML('<root>foobar</root>');
95+
96+
self::assertEquals(
97+
$xml,
98+
ref('value')->domElementParent()->eval(row(flow_context(config())->entryFactory()->create('value', $xml)), flow_context())
99+
);
100+
}
57101
}

0 commit comments

Comments
 (0)