Skip to content

Commit

Permalink
Merge pull request #34 from veewee/predicate-is-of-type
Browse files Browse the repository at this point in the history
Introduce a predicate IsOfType that checks both current and extending…
  • Loading branch information
veewee authored Jun 11, 2024
2 parents 328dfcc + 513cb72 commit ea80ce1
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"ext-dom": "*",
"goetas-webservices/xsd-reader": "^0.4.6",
"php-soap/engine": "^2.9",
"php-soap/engine": "^2.10.1",
"php-soap/wsdl": "^1.4",
"php-soap/xml": "^1.6.0",
"veewee/xml": "^3.0",
Expand Down
5 changes: 2 additions & 3 deletions src/Metadata/Converter/Wsdl1ToMethodsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ private function parseMethod(Wsdl1SelectedService $service, BindingOperation $bi
[]
);

$void = XsdType::guess('void');
$returnType = $outputMessage->map($convertMessageToTypesDict)->mapOr(
static fn (array $types): XsdType => match (count($types)) {
0 => $void,
0 => XsdType::void(),
1 => first($types),
default => XsdType::guess('array')
},
$void
XsdType::void()
);

$configure = pipe(
Expand Down
40 changes: 40 additions & 0 deletions src/Metadata/Predicate/IsOfType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Metadata\Predicate;

use Soap\Engine\Metadata\Model\XsdType;

final class IsOfType
{
/**
* @param non-empty-string $namespace
* @param non-empty-string $name
*/
public function __construct(
private readonly string $namespace,
private readonly string $name
) {
}

public function __invoke(XsdType $type): bool
{
$normalize = mb_strtolower(...);
$expectedName = $normalize($this->name);
$expectedNamespace = $normalize($this->namespace);

if ($normalize($type->getXmlTypeName()) === $expectedName && $normalize($type->getXmlNamespace()) === $expectedNamespace) {
return true;
}

$extends = $type->getMeta()
->extends()
->filter(static fn (array $extends): bool => $normalize($extends['type']) === $expectedName
&& $normalize($extends['namespace']) === $expectedNamespace);
if ($extends->isSome()) {
return true;
}

return false;
}
}
120 changes: 120 additions & 0 deletions tests/Unit/Metadata/Predicate/IsOfTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Test\Unit\Metadata\Predicate;

use PHPUnit\Framework\TestCase;
use Soap\Engine\Metadata\Model\TypeMeta;
use Soap\Engine\Metadata\Model\XsdType;
use Soap\WsdlReader\Metadata\Predicate\IsOfType;

final class IsOfTypeTest extends TestCase
{

/**
* @dataProvider provideTests
*
*/
public function test_it_knows_if_a_type_is_considered_nullable(
string $namespace,
string $name,
XsdType $type,
bool $expected
): void {
static::assertSame($expected, (new IsOfType($namespace, $name))($type));
}

public static function provideTests()
{
yield 'empty' => [
'https://test',
'test',
(new XsdType('')),
false,
];
yield 'invalid-type' => [
'https://test',
'test',
(new XsdType('test'))
->withXmlTypeName('invalid')
->withXmlNamespace('https://test'),
false,
];
yield 'invalid-ns' => [
'https://test',
'test',
(new XsdType('test'))
->withXmlTypeName('test')
->withXmlNamespace('invalid'),
false,
];
yield 'valid' => [
'https://test',
'test',
(new XsdType('test'))
->withXmlTypeName('test')
->withXmlNamespace('https://test'),
true,
];
yield 'valid-case-insensitive' => [
'https://TEST',
'TEST',
(new XsdType('test'))
->withXmlTypeName('test')
->withXmlNamespace('https://test'),
true,
];
yield 'invalid-extend-type' => [
'https://test',
'test',
(new XsdType(''))
->withMeta(
static fn (TypeMeta $meta) => $meta->withExtends([
'type' => 'invalid',
'namespace' => 'https://test',
'isSimple' => false,
])
),
false,
];
yield 'invalid-extend-ns' => [
'https://test',
'test',
(new XsdType(''))
->withMeta(
static fn (TypeMeta $meta) => $meta->withExtends([
'type' => 'test',
'namespace' => 'invalid',
'isSimple' => false,
])
),
false,
];
yield 'valid-extend' => [
'https://test',
'test',
(new XsdType(''))
->withMeta(
static fn (TypeMeta $meta) => $meta->withExtends([
'type' => 'test',
'namespace' => 'https://test',
'isSimple' => false,
])
),
true,
];
yield 'valid-extend-case-insensitive' => [
'https://TEST',
'TEST',
(new XsdType(''))
->withMeta(
static fn (TypeMeta $meta) => $meta->withExtends([
'type' => 'test',
'namespace' => 'https://test',
'isSimple' => false,
])
),
true,
];
}
}

0 comments on commit ea80ce1

Please sign in to comment.