Skip to content

Commit

Permalink
Fall back to complexType name when the element is a link to a complex…
Browse files Browse the repository at this point in the history
…Type
  • Loading branch information
veewee committed Sep 27, 2024
1 parent 59a5c10 commit 98caa9e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
14 changes: 14 additions & 0 deletions spec/Phpro/SoapClient/CodeGenerator/Model/ReturnTypeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Phpro/SoapClient/CodeGenerator/Model/ReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ private function createContext()
],
(new XsdType('MyType'))
->withXmlNamespace('http://my-namespace.com')
->withXmlTypeName('MyType'),
),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<CODE
namespace Vendor\MyNamespace;
use Phpro\SoapClient\Type\ResultInterface;
use Vendor\MyTypeNamespace;
use Phpro\SoapClient\Exception\SoapException;
use Phpro\SoapClient\Type\MultiArgumentRequest;
class MyClient
{
/**
* @return ResultInterface & MyTypeNamespace\ReturnType
* @throws SoapException
*/
public function functionName() : \Vendor\MyTypeNamespace\ReturnType
{
\$response = (\$this->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);
Expand Down

0 comments on commit 98caa9e

Please sign in to comment.