diff --git a/src/Xmlns.php b/src/Xmlns.php new file mode 100644 index 0000000..fc7f47d --- /dev/null +++ b/src/Xmlns.php @@ -0,0 +1,30 @@ + $this->document->locate(root_namespace_uri()), - ]))($xpath); + $tns = $this->document->map(document_element())->getAttribute('targetNamespace'); + + return namespaces(filter( + merge( + [ + 'schema' => Xmlns::xsd()->value(), + 'soap' => Xmlns::soap()->value(), + 'soap12' => Xmlns::soap12()->value(), + 'wsdl' => Xmlns::wsdl()->value(), + ], + $tns ? ['tns' => $tns] : [] + ) + ))($xpath); } } diff --git a/tests/Unit/XmlnsTest.php b/tests/Unit/XmlnsTest.php new file mode 100644 index 0000000..90436d5 --- /dev/null +++ b/tests/Unit/XmlnsTest.php @@ -0,0 +1,42 @@ +value(), $uri); + } + + public function provideKnownXmlnses() + { + yield 'wsdl' => [ + static fn () => Xmlns::wsdl(), + 'http://schemas.xmlsoap.org/wsdl/' + ]; + yield 'soap' => [ + static fn () => Xmlns::soap(), + 'http://schemas.xmlsoap.org/wsdl/soap/' + ]; + yield 'soap12' => [ + static fn () => Xmlns::soap12(), + 'http://schemas.xmlsoap.org/wsdl/soap12/' + ]; + yield 'xsd' => [ + static fn () => Xmlns::xsd(), + 'http://www.w3.org/2001/XMLSchema' + ]; + } +} diff --git a/tests/Unit/Xpath/WsdlPresetTest.php b/tests/Unit/Xpath/WsdlPresetTest.php index b463a44..e69b3ec 100644 --- a/tests/Unit/Xpath/WsdlPresetTest.php +++ b/tests/Unit/Xpath/WsdlPresetTest.php @@ -10,7 +10,7 @@ final class WsdlPresetTest extends TestCase { - public function test_it_provides_a_wsdl_xpath_preset(): void + public function test_it_provides_a_wsdl_prefix_in_xpath_preset(): void { $doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl'); $xpath = $doc->xpath(new WsdlPreset($doc)); @@ -18,4 +18,40 @@ public function test_it_provides_a_wsdl_xpath_preset(): void $definitions = $xpath->querySingle('/wsdl:definitions'); static::assertTrue(is_element($definitions)); } + + public function test_it_provides_a_soap_prefix_in_xpath_preset(): void + { + $doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl'); + $xpath = $doc->xpath(new WsdlPreset($doc)); + + $address = $xpath->querySingle('//soap:address'); + static::assertTrue(is_element($address)); + } + + public function test_it_provides_a_soap12_prefix_in_xpath_preset(): void + { + $doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl'); + $xpath = $doc->xpath(new WsdlPreset($doc)); + + $address = $xpath->querySingle('//soap12:address'); + static::assertTrue(is_element($address)); + } + + public function test_it_provides_a_schema_prefix_in_xpath_preset(): void + { + $doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl'); + $xpath = $doc->xpath(new WsdlPreset($doc)); + + $schema = $xpath->querySingle('//schema:schema'); + static::assertTrue(is_element($schema)); + } + + public function test_it_provides_a_tns_prefix_in_xpath_preset(): void + { + $doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl'); + $xpath = $doc->xpath(new WsdlPreset($doc)); + + $definitions = $xpath->querySingle('//tns:hello'); + static::assertTrue(is_element($definitions)); + } } diff --git a/tests/fixtures/weather-ws.wsdl b/tests/fixtures/weather-ws.wsdl index 8e63500..f1a6f32 100644 --- a/tests/fixtures/weather-ws.wsdl +++ b/tests/fixtures/weather-ws.wsdl @@ -349,4 +349,5 @@ Cached version of: http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl + test