-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from veewee/predicate-is-of-type
Introduce a predicate IsOfType that checks both current and extending…
- Loading branch information
Showing
4 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |