From 98caa9e5972290144e5d4e24b7e197e313b164df Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 27 Sep 2024 11:19:57 +0200 Subject: [PATCH] Fall back to complexType name when the element is a link to a complexType --- .../CodeGenerator/Model/ReturnTypeSpec.php | 14 +++++ .../Assembler/ClassMapAssembler.php | 2 +- .../CodeGenerator/Model/ReturnType.php | 3 ++ .../Assembler/ClassMapAssemblerTest.php | 1 - .../Assembler/ClientMethodAssemblerTest.php | 51 +++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) diff --git a/spec/Phpro/SoapClient/CodeGenerator/Model/ReturnTypeSpec.php b/spec/Phpro/SoapClient/CodeGenerator/Model/ReturnTypeSpec.php index fa026b29..38aca94f 100644 --- a/spec/Phpro/SoapClient/CodeGenerator/Model/ReturnTypeSpec.php +++ b/spec/Phpro/SoapClient/CodeGenerator/Model/ReturnTypeSpec.php @@ -35,4 +35,18 @@ public function it_has_type_meta(): void { $this->getMeta()->shouldBeLike(new TypeMeta()); } + + public function it_falls_back_to_complex_type_if_its_an_element_referencing_this_type(): void + { + $this->beConstructedThrough( + 'fromMetaData', + [ + 'My\Namespace', + XsdType::create('ElementType') + ->withXmlTypeName('ComplexType') + ] + ); + + $this->getType()->shouldReturn('\\My\\Namespace\\ComplexType'); + } } diff --git a/src/Phpro/SoapClient/CodeGenerator/Assembler/ClassMapAssembler.php b/src/Phpro/SoapClient/CodeGenerator/Assembler/ClassMapAssembler.php index abffae01..db051e25 100644 --- a/src/Phpro/SoapClient/CodeGenerator/Assembler/ClassMapAssembler.php +++ b/src/Phpro/SoapClient/CodeGenerator/Assembler/ClassMapAssembler.php @@ -80,7 +80,7 @@ private function assembleClassMap(TypeMap $typeMap, string $linefeed, string $in '%snew ClassMap(\'%s\', \'%s\', %s::class),', $indentation, $type->getXsdType()->getXmlNamespace(), - $type->getXsdType()->getXmlTypeName(), + $type->getXsdType()->getName(), 'Type\\'.$type->getName() ); } diff --git a/src/Phpro/SoapClient/CodeGenerator/Model/ReturnType.php b/src/Phpro/SoapClient/CodeGenerator/Model/ReturnType.php index 619504c8..c9a3e1ed 100644 --- a/src/Phpro/SoapClient/CodeGenerator/Model/ReturnType.php +++ b/src/Phpro/SoapClient/CodeGenerator/Model/ReturnType.php @@ -45,6 +45,9 @@ public function __construct(string $type, string $namespace, XsdType $xsdType) */ public static function fromMetaData(string $namespace, XsdType $returnType): self { + // Element types that are referencing complex types, should result in the complexType according to ext-soap: + $returnType = $returnType->copy($returnType->getXmlTypeName() ?: $returnType->getName()); + $typeName = (new TypeNameCalculator())($returnType); return new self( diff --git a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClassMapAssemblerTest.php b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClassMapAssemblerTest.php index c6979702..7d0c8190 100644 --- a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClassMapAssemblerTest.php +++ b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClassMapAssemblerTest.php @@ -92,7 +92,6 @@ private function createContext() ], (new XsdType('MyType')) ->withXmlNamespace('http://my-namespace.com') - ->withXmlTypeName('MyType'), ), ]); diff --git a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php index 6bdd2678..fd5c461c 100644 --- a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php +++ b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php @@ -472,6 +472,57 @@ public function functionName(\Phpro\SoapClient\Type\MultiArgumentRequest \$multi } } +CODE; + + $this->assertEquals($expected, $code); + } + + /** + * @test + */ + function it_can_deal_with_elements_referencing_complex_types_return_types() + { + $assembler = new ClientMethodAssembler(); + $class = new ClassGenerator(); + $class->setName('Vendor\\MyNamespace\\MyClient'); + $typeNamespace = 'Vendor\\MyTypeNamespace'; + $method = new ClientMethod( + 'functionName', + [], + ReturnType::fromMetaData($typeNamespace, XsdType::create('ReturnTypeElement')->withXmlTypeName('ReturnType')), + $typeNamespace, + new MethodMeta() + ); + + $context = new ClientMethodContext($class, $method); + $assembler->assemble($context); + + $code = $context->getClass()->generate(); + $expected = <<caller)('functionName', new MultiArgumentRequest([])); + + \Psl\Type\instance_of(\Vendor\MyTypeNamespace\ReturnType::class)->assert(\$response); + \Psl\Type\instance_of(\Phpro\SoapClient\Type\ResultInterface::class)->assert(\$response); + + return \$response; + } +} + CODE; $this->assertEquals($expected, $code);