Skip to content

Commit f3d0c5e

Browse files
committed
Add common xmlns namespaces and improve wsdl preset
1 parent f0f82e4 commit f3d0c5e

File tree

5 files changed

+127
-5
lines changed

5 files changed

+127
-5
lines changed

src/Xmlns.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Soap\Xml;
6+
7+
use VeeWee\Xml\Xmlns\Xmlns as XmlXmlns;
8+
9+
final class Xmlns
10+
{
11+
public static function wsdl(): XmlXmlns
12+
{
13+
return XmlXmlns::load('http://schemas.xmlsoap.org/wsdl/');
14+
}
15+
16+
public static function soap(): XmlXmlns
17+
{
18+
return XmlXmlns::load('http://schemas.xmlsoap.org/wsdl/soap/');
19+
}
20+
21+
public static function soap12(): XmlXmlns
22+
{
23+
return XmlXmlns::load('http://schemas.xmlsoap.org/wsdl/soap12/');
24+
}
25+
26+
public static function xsd(): XmlXmlns
27+
{
28+
return XmlXmlns::load('http://www.w3.org/2001/XMLSchema');
29+
}
30+
}

src/Xpath/WsdlPreset.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace Soap\Xml\Xpath;
66

77
use DOMXPath;
8+
use Soap\Xml\Xmlns;
89
use VeeWee\Xml\Dom\Document;
910
use VeeWee\Xml\Dom\Xpath\Configurator\Configurator;
10-
use function VeeWee\Xml\Dom\Locator\root_namespace_uri;
11+
use function Psl\Dict\filter;
12+
use function Psl\Dict\merge;
13+
use function VeeWee\Xml\Dom\Locator\document_element;
1114
use function VeeWee\Xml\Dom\Xpath\Configurator\namespaces;
1215

1316
final class WsdlPreset implements Configurator
@@ -21,8 +24,18 @@ public function __construct(Document $document)
2124

2225
public function __invoke(DOMXPath $xpath): DOMXPath
2326
{
24-
return namespaces(array_filter([
25-
'wsdl' => $this->document->locate(root_namespace_uri()),
26-
]))($xpath);
27+
$tns = $this->document->map(document_element())->getAttribute('targetNamespace');
28+
29+
return namespaces(filter(
30+
merge(
31+
[
32+
'schema' => Xmlns::xsd()->value(),
33+
'soap' => Xmlns::soap()->value(),
34+
'soap12' => Xmlns::soap12()->value(),
35+
'wsdl' => Xmlns::wsdl()->value(),
36+
],
37+
$tns ? ['tns' => $tns] : []
38+
)
39+
))($xpath);
2740
}
2841
}

tests/Unit/XmlnsTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SoapTest\Xml\Unit\Xpath;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Soap\Xml\Xmlns;
8+
use VeeWee\Xml\Xmlns\Xmlns as XmlXmlns;
9+
10+
final class XmlnsTest extends TestCase
11+
{
12+
/**
13+
* @param callable(): XmlXmlns
14+
* @dataProvider provideKnownXmlnses
15+
*/
16+
public function test_it_knows_some_xmlnses(callable $factory, string $uri): void
17+
{
18+
$xmlns = $factory();
19+
20+
static::assertSame($xmlns->value(), $uri);
21+
}
22+
23+
public function provideKnownXmlnses()
24+
{
25+
yield 'wsdl' => [
26+
static fn () => Xmlns::wsdl(),
27+
'http://schemas.xmlsoap.org/wsdl/'
28+
];
29+
yield 'soap' => [
30+
static fn () => Xmlns::soap(),
31+
'http://schemas.xmlsoap.org/wsdl/soap/'
32+
];
33+
yield 'soap12' => [
34+
static fn () => Xmlns::soap12(),
35+
'http://schemas.xmlsoap.org/wsdl/soap12/'
36+
];
37+
yield 'xsd' => [
38+
static fn () => Xmlns::xsd(),
39+
'http://www.w3.org/2001/XMLSchema'
40+
];
41+
}
42+
}

tests/Unit/Xpath/WsdlPresetTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,48 @@
1010

1111
final class WsdlPresetTest extends TestCase
1212
{
13-
public function test_it_provides_a_wsdl_xpath_preset(): void
13+
public function test_it_provides_a_wsdl_prefix_in_xpath_preset(): void
1414
{
1515
$doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl');
1616
$xpath = $doc->xpath(new WsdlPreset($doc));
1717

1818
$definitions = $xpath->querySingle('/wsdl:definitions');
1919
static::assertTrue(is_element($definitions));
2020
}
21+
22+
public function test_it_provides_a_soap_prefix_in_xpath_preset(): void
23+
{
24+
$doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl');
25+
$xpath = $doc->xpath(new WsdlPreset($doc));
26+
27+
$address = $xpath->querySingle('//soap:address');
28+
static::assertTrue(is_element($address));
29+
}
30+
31+
public function test_it_provides_a_soap12_prefix_in_xpath_preset(): void
32+
{
33+
$doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl');
34+
$xpath = $doc->xpath(new WsdlPreset($doc));
35+
36+
$address = $xpath->querySingle('//soap12:address');
37+
static::assertTrue(is_element($address));
38+
}
39+
40+
public function test_it_provides_a_schema_prefix_in_xpath_preset(): void
41+
{
42+
$doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl');
43+
$xpath = $doc->xpath(new WsdlPreset($doc));
44+
45+
$schema = $xpath->querySingle('//schema:schema');
46+
static::assertTrue(is_element($schema));
47+
}
48+
49+
public function test_it_provides_a_tns_prefix_in_xpath_preset(): void
50+
{
51+
$doc = Document::fromXmlFile(FIXTURE_DIR.'/weather-ws.wsdl');
52+
$xpath = $doc->xpath(new WsdlPreset($doc));
53+
54+
$definitions = $xpath->querySingle('//tns:hello');
55+
static::assertTrue(is_element($definitions));
56+
}
2157
}

tests/fixtures/weather-ws.wsdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,5 @@ Cached version of: http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl
349349
<http:address location="http://wsf.cdyne.com/WeatherWS/Weather.asmx" />
350350
</wsdl:port>
351351
</wsdl:service>
352+
<tns:hello>test</tns:hello>
352353
</wsdl:definitions>

0 commit comments

Comments
 (0)