Skip to content

Commit

Permalink
Merge pull request #8 from php-soap/auto-import-known-schemas
Browse files Browse the repository at this point in the history
Automatically import known SOAP related schemas
  • Loading branch information
veewee authored May 25, 2023
2 parents da8d1e3 + 6830ebe commit cb65f93
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.phpunit.cache
.php-cs-fixer.cache
/*.wsdl
composer.lock
composer.lock
.phpunit.result.cache
7 changes: 4 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" bootstrap="tests/bootstrap.php" executionOrder="random" beStrictAboutOutputDuringTests="false" failOnRisky="true" failOnWarning="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="tests/bootstrap.php" executionOrder="random" beStrictAboutOutputDuringTests="false" failOnRisky="true" failOnWarning="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="false">
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
Expand All @@ -11,9 +11,10 @@
<directory suffix="Test.php">tests/Integration</directory>
</testsuite>
</testsuites>
<coverage>
<coverage/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>
11 changes: 10 additions & 1 deletion src/Parser/Definitions/SchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Soap\WsdlReader\Parser\Context\ParserContext;
use Soap\Xml\Xpath\WsdlPreset;
use VeeWee\Xml\Dom\Document;
use function VeeWee\Xml\Dom\Locator\document_element;

final class SchemaParser
{
Expand All @@ -27,12 +28,20 @@ public static function tryParse(Document $wsdl, ParserContext $context): Schema
$xpath = $wsdl->xpath(new WsdlPreset($wsdl));
$reader = new SchemaReader();

// Make sure to register the known schema locations and to import them globally.
// This way, they can be used without expecting an explicit import from within the XSD.
// Since WSDLs don't require the soap specific schema's to be imported.
$globalSchema = $reader->getGlobalSchema();
foreach ($context->knownSchemas as $namespace => $location) {
$reader->addKnownNamespaceSchemaLocation($namespace, $location);
$globalSchema->addSchema(
$reader->readNode(Document::fromXmlFile($location)->locate(document_element()), $namespace)
);
}

return $reader->readNodes(
[...$xpath->query('/wsdl:definitions/wsdl:types/schema:schema')]
[...$xpath->query('/wsdl:definitions/wsdl:types/schema:schema')],
$wsdl->toUnsafeDocument()->documentURI
);
}
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ EOF;
test_schema($schema,'type="tns:testType"');
?>
--EXPECT--
FATAL (GoetasWebservices\XML\XSDReader\Exception\TypeException):Can't find type named {http://test-uri/}#testType2, at line 17 in some.wsdl
FATAL (GoetasWebservices\XML\XSDReader\Exception\TypeException):Can't find type named {http://test-uri/}#testType2, at line 13 in some.wsdl
5 changes: 0 additions & 5 deletions tests/PhpCompatibility/test_schema.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use Soap\Engine\Metadata\Model\Type;
use Soap\Wsdl\Loader\CallbackLoader;
use Soap\WsdlReader\Formatter\LongTypeFormatter;
use Soap\WsdlReader\Formatter\ShortMethodFormatter;
use Soap\WsdlReader\Formatter\ShortTypeFormatter;
use Soap\WsdlReader\Metadata\Wsdl1MetadataProvider;
use Soap\WsdlReader\Wsdl1Reader;

Expand All @@ -25,10 +24,6 @@ function test_schema($schema, $type, $style="rpc",$use="encoded", $attributeForm
>
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test-uri/" $attributeFormDefault>
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" /><!-- SOAP 1.1-->
<xsd:import namespace="http://www.w3.org/2003/05/soap-encoding" /><!-- SOAP 1.2 - 2003-05 -->
<xsd:import namespace="http://xml.apache.org/xml-soap" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
$schema
</schema>
</types>
Expand Down

0 comments on commit cb65f93

Please sign in to comment.