diff --git a/src/Metadata/Collection/TypeCollection.php b/src/Metadata/Collection/TypeCollection.php index 1a30c7d..de06922 100644 --- a/src/Metadata/Collection/TypeCollection.php +++ b/src/Metadata/Collection/TypeCollection.php @@ -9,7 +9,6 @@ use IteratorAggregate; use Soap\Engine\Exception\MetadataException; use Soap\Engine\Metadata\Model\Type; -use function array_key_exists; use function Psl\Dict\reindex; /** @@ -108,17 +107,21 @@ public function fetchFirstByName(string $name): Type throw MetadataException::typeNotFound($name); } + /** + * Convention: fetch* throws when the type is missing, find* returns null. + */ + public function findByNameAndXmlNamespace(string $name, string $namespace): ?Type + { + return $this->qualifiedLookup[self::createLookupKey($name, $namespace)] ?? null; + } + /** * @throws MetadataException */ public function fetchByNameAndXmlNamespace(string $name, string $namespace): Type { - $lookupKey = self::createLookupKey($name, $namespace); - if (!array_key_exists($lookupKey, $this->qualifiedLookup)) { - throw MetadataException::typeNotFound($name); - } - - return $this->qualifiedLookup[$lookupKey]; + return $this->findByNameAndXmlNamespace($name, $namespace) + ?? throw MetadataException::typeNotFound($name); } private static function createLookupKey(string $name, string $namespace): string diff --git a/tests/Unit/Metadata/Collection/TypeCollectionTest.php b/tests/Unit/Metadata/Collection/TypeCollectionTest.php index d680a66..1a050c5 100644 --- a/tests/Unit/Metadata/Collection/TypeCollectionTest.php +++ b/tests/Unit/Metadata/Collection/TypeCollectionTest.php @@ -58,6 +58,22 @@ public function test_it_can_fail_fetching_by_name_and_namespace_invalid_namespac $this->collection->fetchByNameAndXmlNamespace('Response', 'http://invalid'); } + public function test_it_can_find_by_name_and_namespace(): void + { + $type = $this->collection->findByNameAndXmlNamespace('Response', 'http://namesapce'); + static::assertSame('Response', $type?->getName()); + } + + public function test_it_finds_nothing_by_name_and_namespace_for_invalid_name(): void + { + static::assertNull($this->collection->findByNameAndXmlNamespace('nope', 'http://namesapce')); + } + + public function test_it_finds_nothing_by_name_and_namespace_for_other_namespace(): void + { + static::assertNull($this->collection->findByNameAndXmlNamespace('Response', 'http://invalid')); + } + public function test_it_can_reduce(): void { $result = $this->collection->reduce(static fn (int $i, Type $item) => $i + 1, 0);