From 0d8172a7553036d1e64670d63d255b2635ed920b Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 22 Sep 2023 14:19:41 +0200 Subject: [PATCH] Fix tests --- composer.json | 4 +- .../Assembler/ClientMethodAssembler.php | 7 ++- .../Assembler/ExtendingTypeAssembler.php | 2 +- .../Calculator/UnionTypesCalculator.php | 9 +++- .../Assembler/ClientMethodAssemblerTest.php | 49 ++++++++++++++++--- .../Calculator/UnionTypesCalculatorTest.php | 2 +- .../Predicate/IsConsideredScalarTypeTest.php | 10 ++-- 7 files changed, 62 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 95e60b33..e916eb74 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,10 @@ "php": "~8.1.0 || ~8.2.0", "azjezz/psl": "^2.1", "laminas/laminas-code": "^4.8.0", - "php-soap/engine": "^2.3", + "php-soap/engine": "^2.4", "php-soap/ext-soap-engine": "^1.4", "php-soap/psr18-transport": "^1.3", - "php-soap/wsdl-reader": "~0.3", + "php-soap/wsdl-reader": "~0.5", "psr/event-dispatcher": "^1.0", "psr/log": "^1.0 || ^2.0 || ^3.0", "symfony/console": "~5.4 || ~6.0", diff --git a/src/Phpro/SoapClient/CodeGenerator/Assembler/ClientMethodAssembler.php b/src/Phpro/SoapClient/CodeGenerator/Assembler/ClientMethodAssembler.php index 2ff0aad2..2f7ffd04 100644 --- a/src/Phpro/SoapClient/CodeGenerator/Assembler/ClientMethodAssembler.php +++ b/src/Phpro/SoapClient/CodeGenerator/Assembler/ClientMethodAssembler.php @@ -79,6 +79,9 @@ private function generateMethodBody( ClientMethod $method, $context ): string { + $assertInstanceOf = static fn (string $class): string => + '\\Psl\\Type\\instance_of(\\'.ltrim($class, '\\').'::class)->assert($response);'; + $code = [ sprintf( '$response = ($this->caller)(\'%s\', %s);', @@ -88,8 +91,8 @@ private function generateMethodBody( : '$'.$param->getName() ), '', - '\\assert($response instanceof \\'.ltrim($this->decideOnReturnType($context, true), '\\').');', - '\\assert($response instanceof \\'.ResultInterface::class.');', + $assertInstanceOf($this->decideOnReturnType($context, true)), + $assertInstanceOf(ResultInterface::class), '', 'return $response;', ]; diff --git a/src/Phpro/SoapClient/CodeGenerator/Assembler/ExtendingTypeAssembler.php b/src/Phpro/SoapClient/CodeGenerator/Assembler/ExtendingTypeAssembler.php index b3015895..df1edf6b 100644 --- a/src/Phpro/SoapClient/CodeGenerator/Assembler/ExtendingTypeAssembler.php +++ b/src/Phpro/SoapClient/CodeGenerator/Assembler/ExtendingTypeAssembler.php @@ -33,7 +33,7 @@ public function assemble(ContextInterface $context) $meta = $type->getMeta(); $extending = $meta->extends()->unwrapOr(null); - if (!$extending || $extending['isSimple']) { + if (!$extending || ($extending['isSimple'] ?? false)) { return; } diff --git a/src/Phpro/SoapClient/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculator.php b/src/Phpro/SoapClient/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculator.php index 218088cc..4513a865 100644 --- a/src/Phpro/SoapClient/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculator.php +++ b/src/Phpro/SoapClient/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculator.php @@ -29,7 +29,14 @@ public function __invoke(TypeMeta $meta): string */ static function (array $union): string { $type = $union['type']; - $type = Normalizer::isKnownType($type) ? $type : 'mixed'; // Todo : resolve bottom type of the selected simple type?? + + // The union type could be a nested simple type. + // If the normalizer does not know the type, + // this implementation falls back to 'mixed' in that case. + // + // A possible improvement here could be to parse and store the inferred bottom type + //as meta-info inside the union meta instead. + $type = Normalizer::isKnownType($type) ? $type : 'mixed'; return $union['isList'] ? 'list<'.$type.'>' : $type; } diff --git a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php index eb9eff0e..ce917ad4 100644 --- a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php +++ b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/ClientMethodAssemblerTest.php @@ -135,7 +135,12 @@ class MyClient */ public function functionName(\Vendor\MyTypeNamespace\ParamType \$param) : \Vendor\MyTypeNamespace\ReturnType { - return (\$this->caller)('functionName', \$param); + \$response = (\$this->caller)('functionName', \$param); + + \Psl\Type\instance_of(\Vendor\MyTypeNamespace\ReturnType::class)->assert(\$response); + \Psl\Type\instance_of(\Phpro\SoapClient\Type\ResultInterface::class)->assert(\$response); + + return \$response; } } @@ -178,7 +183,12 @@ class MyClient */ public function functionName(\Phpro\SoapClient\Type\MultiArgumentRequest \$multiArgumentRequest) : \Vendor\MyTypeNamespace\ReturnType { - return (\$this->caller)('functionName', \$multiArgumentRequest); + \$response = (\$this->caller)('functionName', \$multiArgumentRequest); + + \Psl\Type\instance_of(\Vendor\MyTypeNamespace\ReturnType::class)->assert(\$response); + \Psl\Type\instance_of(\Phpro\SoapClient\Type\ResultInterface::class)->assert(\$response); + + return \$response; } } @@ -212,7 +222,12 @@ class MyClient */ public function functionName() : \Vendor\MyTypeNamespace\ReturnType { - return (\$this->caller)('functionName', new MultiArgumentRequest([])); + \$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; } } @@ -260,7 +275,12 @@ class MyClient */ public function function_name(\Vendor\MyTypeNamespace\ParamType \$param) : \Vendor\MyTypeNamespace\ReturnType { - return (\$this->caller)('Function_name', \$param); + \$response = (\$this->caller)('Function_name', \$param); + + \Psl\Type\instance_of(\Vendor\MyTypeNamespace\ReturnType::class)->assert(\$response); + \Psl\Type\instance_of(\Phpro\SoapClient\Type\ResultInterface::class)->assert(\$response); + + return \$response; } } @@ -328,7 +348,12 @@ class MyClient */ public function function_name(\Phpro\SoapClient\Type\MultiArgumentRequest \$multiArgumentRequest) : \Vendor\MyTypeNamespace\ReturnType { - return (\$this->caller)('Function_name', \$multiArgumentRequest); + \$response = (\$this->caller)('Function_name', \$multiArgumentRequest); + + \Psl\Type\instance_of(\Vendor\MyTypeNamespace\ReturnType::class)->assert(\$response); + \Psl\Type\instance_of(\Phpro\SoapClient\Type\ResultInterface::class)->assert(\$response); + + return \$response; } } @@ -376,7 +401,12 @@ class MyClient */ public function functionName() : \Phpro\SoapClient\Type\MixedResult { - return (\$this->caller)('functionName', new MultiArgumentRequest([])); + \$response = (\$this->caller)('functionName', new MultiArgumentRequest([])); + + \Psl\Type\instance_of(\Phpro\SoapClient\Type\MixedResult::class)->assert(\$response); + \Psl\Type\instance_of(\Phpro\SoapClient\Type\ResultInterface::class)->assert(\$response); + + return \$response; } } @@ -433,7 +463,12 @@ class MyClient */ public function functionName(\Phpro\SoapClient\Type\MultiArgumentRequest \$multiArgumentRequest) : \Phpro\SoapClient\Type\MixedResult { - return (\$this->caller)('functionName', \$multiArgumentRequest); + \$response = (\$this->caller)('functionName', \$multiArgumentRequest); + + \Psl\Type\instance_of(\Phpro\SoapClient\Type\MixedResult::class)->assert(\$response); + \Psl\Type\instance_of(\Phpro\SoapClient\Type\ResultInterface::class)->assert(\$response); + + return \$response; } } diff --git a/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculatorTest.php b/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculatorTest.php index 1891886d..22851da8 100644 --- a/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculatorTest.php +++ b/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Calculator/UnionTypesCalculatorTest.php @@ -58,7 +58,7 @@ public function provideExpectations() ['type' => 'My_Type', 'isList' => false, 'namespace' => 'xx'], ['type' => 'Your_Type', 'isList' => true, 'namespace' => 'xx'], ]), - "MyType | list", + "mixed | list", ]; } } diff --git a/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Predicate/IsConsideredScalarTypeTest.php b/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Predicate/IsConsideredScalarTypeTest.php index 201dd242..9f4aefa4 100644 --- a/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Predicate/IsConsideredScalarTypeTest.php +++ b/test/PhproTest/SoapClient/Unit/CodeGenerator/TypeEnhancer/Predicate/IsConsideredScalarTypeTest.php @@ -24,15 +24,11 @@ public function provideTests() (new TypeMeta()), false, ]; - yield 'list' => [ - (new TypeMeta())->withIsList(true), - true, - ]; - yield 'nullable' => [ - (new TypeMeta())->withIsNullable(true), + yield 'simple' => [ + (new TypeMeta())->withIsSimple(true), true, ]; - yield 'simple' => [ + yield 'attribute' => [ (new TypeMeta())->withIsSimple(true), true, ];