66
77use function Flow \ETL \DSL \{config , flow_context , ref , row };
88use Dom \HTMLDocument ;
9+ use Flow \ETL \Exception \InvalidArgumentException ;
10+ use Flow \ETL \Function \ExecutionMode ;
911use PHPUnit \Framework \Attributes \RequiresPhp ;
1012use PHPUnit \Framework \TestCase ;
1113
1214final 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