Skip to content

Add common xmlns namespaces and improve wsdl preset #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Xmlns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Soap\Xml;

use VeeWee\Xml\Xmlns\Xmlns as XmlXmlns;

final class Xmlns
{
public static function wsdl(): XmlXmlns
{
return XmlXmlns::load('http://schemas.xmlsoap.org/wsdl/');
}

public static function soap(): XmlXmlns
{
return XmlXmlns::load('http://schemas.xmlsoap.org/wsdl/soap/');
}

public static function soap12(): XmlXmlns
{
return XmlXmlns::load('http://schemas.xmlsoap.org/wsdl/soap12/');
}

public static function xsd(): XmlXmlns
{
return XmlXmlns::load('http://www.w3.org/2001/XMLSchema');
}
}
21 changes: 17 additions & 4 deletions src/Xpath/WsdlPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Soap\Xml\Xpath;

use DOMXPath;
use Soap\Xml\Xmlns;
use VeeWee\Xml\Dom\Document;
use VeeWee\Xml\Dom\Xpath\Configurator\Configurator;
use function VeeWee\Xml\Dom\Locator\root_namespace_uri;
use function Psl\Dict\filter;
use function Psl\Dict\merge;
use function VeeWee\Xml\Dom\Locator\document_element;
use function VeeWee\Xml\Dom\Xpath\Configurator\namespaces;

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

public function __invoke(DOMXPath $xpath): DOMXPath
{
return namespaces(array_filter([
'wsdl' => $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);
}
}
42 changes: 42 additions & 0 deletions tests/Unit/XmlnsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

namespace SoapTest\Xml\Unit\Xpath;

use PHPUnit\Framework\TestCase;
use Soap\Xml\Xmlns;
use VeeWee\Xml\Xmlns\Xmlns as XmlXmlns;

final class XmlnsTest extends TestCase
{
/**
* @param callable(): XmlXmlns
* @dataProvider provideKnownXmlnses
*/
public function test_it_knows_some_xmlnses(callable $factory, string $uri): void
{
$xmlns = $factory();

static::assertSame($xmlns->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'
];
}
}
38 changes: 37 additions & 1 deletion tests/Unit/Xpath/WsdlPresetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,48 @@

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));

$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));
}
}
1 change: 1 addition & 0 deletions tests/fixtures/weather-ws.wsdl
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,5 @@ Cached version of: http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl
<http:address location="http://wsf.cdyne.com/WeatherWS/Weather.asmx" />
</wsdl:port>
</wsdl:service>
<tns:hello>test</tns:hello>
</wsdl:definitions>