From c9b8700a142dda3770a9b028f83fc71bc30d56f3 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 3 Jan 2025 10:14:17 +0100 Subject: [PATCH] Find method by SOAP action --- src/Metadata/Collection/MethodCollection.php | 15 +++++++++++++++ .../Collection/MethodCollectionTest.php | 18 ++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Metadata/Collection/MethodCollection.php b/src/Metadata/Collection/MethodCollection.php index 12fbd34..1391e1c 100644 --- a/src/Metadata/Collection/MethodCollection.php +++ b/src/Metadata/Collection/MethodCollection.php @@ -64,4 +64,19 @@ public function fetchByName(string $name): Method throw MetadataException::methodNotFound($name); } + + /** + * @throws MetadataException + */ + public function fetchBySoapAction(string $soapAction): Method + { + foreach ($this->methods as $method) { + $meta = $method->getMeta(); + if ($meta->action()->isSome() && $soapAction === $meta->action()->unwrap()) { + return $method; + } + } + + throw MetadataException::methodNotFound($soapAction); + } } diff --git a/tests/Unit/Metadata/Collection/MethodCollectionTest.php b/tests/Unit/Metadata/Collection/MethodCollectionTest.php index c8f5cc5..ff5dd0d 100644 --- a/tests/Unit/Metadata/Collection/MethodCollectionTest.php +++ b/tests/Unit/Metadata/Collection/MethodCollectionTest.php @@ -9,6 +9,7 @@ use Soap\Engine\Metadata\Collection\MethodCollection; use Soap\Engine\Metadata\Collection\ParameterCollection; use Soap\Engine\Metadata\Model\Method; +use Soap\Engine\Metadata\Model\MethodMeta; use Soap\Engine\Metadata\Model\XsdType; final class MethodCollectionTest extends TestCase @@ -18,11 +19,12 @@ final class MethodCollectionTest extends TestCase protected function setUp(): void { $this->collection = new MethodCollection( - new Method('hello', new ParameterCollection(), XsdType::create('Response')) + (new Method('hello', new ParameterCollection(), XsdType::create('Response')))->withMeta( + static fn (MethodMeta $meta) => $meta->withAction('uri:hello') + ) ); } - public function test_it_can_iterate_over_methods(): void { static::assertCount(1, $this->collection); @@ -42,4 +44,16 @@ public function test_it_can_fail_fetching_by_name(): void $this->expectException(MetadataException::class); $this->collection->fetchByName('nope'); } + + public function test_it_can_fetch_by_soap_action(): void + { + $method = $this->collection->fetchBySoapAction('uri:hello'); + static::assertSame('hello', $method->getName()); + } + + public function test_it_can_fail_fetching_by_soap_action(): void + { + $this->expectException(MetadataException::class); + $this->collection->fetchBySoapAction('uri:nope'); + } }