Skip to content
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
21 changes: 21 additions & 0 deletions src/Loader/FlatteningLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,32 @@ public function __construct(
*/
public function __invoke(string $location): string
{
$location = self::normalizeLocation($location);
$currentDoc = Document::fromXmlString(
non_empty_string()->assert(($this->loader)($location))
);
$context = FlatteningContext::forWsdl($location, $currentDoc, $this->loader);

return (new Flattener())($location, $currentDoc, $context);
}

/**
* Ensures the base location used for resolving imports is absolute.
* Why: league/uri >= 7.6 throws when resolving a relative reference against a non-absolute base.
*
* @throws UnloadableWsdlException
*/
private static function normalizeLocation(string $location): string
{
if (preg_match('#^[a-z][a-z0-9+.\-]*:#i', $location) === 1) {
return $location;
}

$absolute = realpath($location);
if ($absolute === false) {
throw UnloadableWsdlException::fromLocation($location);
}

return $absolute;
}
}
67 changes: 67 additions & 0 deletions tests/Unit/Loader/FlatteningLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psl\Ref;
use Soap\Wsdl\Exception\UnloadableWsdlException;
use Soap\Wsdl\Loader\FlatteningLoader;
use Soap\Wsdl\Loader\StreamWrapperLoader;
use Soap\Wsdl\Loader\WsdlLoader;
use VeeWee\Xml\Dom\Document;
use function VeeWee\Xml\Dom\Configurator\comparable;

Expand All @@ -28,6 +31,70 @@ public function test_it_can_load_flattened_imports(string $wsdl, Document $expec
static::assertSame($expected->toXmlString(), $flattened->toXmlString());
}

public function test_it_resolves_imports_when_given_a_relative_path(): void
{
$cwd = getcwd();
chdir(FIXTURE_DIR.'/flattening');
try {
$result = ($this->loader)('single-xsd.wsdl');
} finally {
chdir($cwd);
}

$flattened = Document::fromXmlString($result, comparable());
$expected = Document::fromXmlFile(FIXTURE_DIR.'/flattening/result/single-xsd-result.wsdl', comparable());

static::assertSame($expected->toXmlString(), $flattened->toXmlString());
}

public function test_it_leaves_uri_scheme_locations_untouched(): void
{
$result = ($this->loader)('file://'.FIXTURE_DIR.'/flattening/single-xsd.wsdl');
$flattened = Document::fromXmlString($result, comparable());
$expected = Document::fromXmlFile(FIXTURE_DIR.'/flattening/result/single-xsd-result.wsdl', comparable());

static::assertSame($expected->toXmlString(), $flattened->toXmlString());
}

public function test_it_throws_when_given_a_non_existing_relative_path(): void
{
$this->expectException(UnloadableWsdlException::class);
($this->loader)('does-not-exist.wsdl');
}

#[DataProvider('provideSchemeLocations')]
public function test_it_forwards_uri_scheme_locations_unchanged(string $location): void
{
/** @var Ref<?string> $captured */
$captured = new Ref(null);
$capturingLoader = new class($captured) implements WsdlLoader {
public function __construct(private Ref $captured)
{
}
public function __invoke(string $location): string
{
$this->captured->value = $location;
return '<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>';
}
};

(new FlatteningLoader($capturingLoader))($location);

static::assertSame($location, $captured->value);
}

public static function provideSchemeLocations(): iterable
{
yield 'http' => ['http://example.com/service.wsdl'];
yield 'https' => ['https://example.com/service.wsdl'];
yield 'file' => ['file:///tmp/service.wsdl'];
yield 'ftp' => ['ftp://example.com/service.wsdl'];
yield 'scheme-with-plus' => ['svn+ssh://example.com/service.wsdl'];
yield 'scheme-with-dot' => ['x.y://example.com/service.wsdl'];
yield 'scheme-with-dash' => ['x-y://example.com/service.wsdl'];
yield 'mixed-case-scheme' => ['HTTP://example.com/service.wsdl'];
}

public static function provideTestCases()
{
//
Expand Down