Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to complexType name when the element is a link to a complexType #546

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading