diff --git a/Neos.Flow/Tests/Functional/Reflection/Fixtures/DummyClassWithUnionTypeAnnotations.php b/Neos.Flow/Tests/Functional/Reflection/Fixtures/DummyClassWithUnionTypeAnnotations.php new file mode 100644 index 0000000000..6fbfb9256b --- /dev/null +++ b/Neos.Flow/Tests/Functional/Reflection/Fixtures/DummyClassWithUnionTypeAnnotations.php @@ -0,0 +1,51 @@ +markTestSkipped('Only for PHP 8 with UnionTypes'); - } - $returnTypeA = $this->reflectionService->getMethodDeclaredReturnType(Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypeA'); - $returnTypeB = $this->reflectionService->getMethodDeclaredReturnType(Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesB'); - $returnTypeC = $this->reflectionService->getMethodDeclaredReturnType(Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesC'); + $returnTypes = [ + 'returnTypeA' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypeA'), + 'returnTypeB' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesB'), + 'returnTypeC' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesC'), + ]; + + self::assertEquals( + [ + 'returnTypeA' => 'string|false', + 'returnTypeB' => '\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false', + 'returnTypeC' => '?\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints', + ], + $returnTypes + ); + } + + /** + * @test + */ + public function unionParameterTypesWorkCorrectly() + { + $methodWithUnionParameters = $this->reflectionService->getMethodParameters(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionParameters'); + + $methodWithUnionParametersReduced = array_map( + fn (array $item) => [ + 'type' => $item['type'], + 'class' => $item['class'], + 'allowsNull' => $item['allowsNull'], + ], + $methodWithUnionParameters + ); + + self::assertEquals( + [ + 'parameterA' => [ + 'type' => 'string|false', + 'class' => 'string|false', + 'allowsNull' => false, + ], + 'parameterB' => [ + 'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false', + 'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false', + 'allowsNull' => false, + ], + 'parameterC' => [ + 'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints', + 'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints', + 'allowsNull' => true, + ], + ], + $methodWithUnionParametersReduced + ); + } - self::assertEquals('string|false', $returnTypeA); - self::assertEquals('\Neos\Flow\Tests\Functional\Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints|false', $returnTypeB); - self::assertEquals('?\Neos\Flow\Tests\Functional\Reflection\Fixtures\PHP8\DummyClassWithUnionTypeHints', $returnTypeC); + /** + * @test + */ + public function unionReturnTypeAnnotationsWorkCorrectly() + { + $returnTypes = [ + 'returnTypeA' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeAnnotations::class, 'methodWithUnionReturnTypeA'), + 'returnTypeB' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeAnnotations::class, 'methodWithUnionReturnTypesB'), + 'returnTypeC' => $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeAnnotations::class, 'methodWithUnionReturnTypesC'), + ]; + + self::assertEquals( + [ + 'returnTypeA' => 'string|false', + 'returnTypeB' => '\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false', + 'returnTypeC' => '?\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations', + ], + $returnTypes + ); + } + + /** + * @test + */ + public function unionParameterTypeAnnotationsWorkCorrectly() + { + $methodWithUnionParameters = $this->reflectionService->getMethodParameters(Fixtures\DummyClassWithUnionTypeAnnotations::class, 'methodWithUnionParameters'); + + $methodWithUnionParametersReduced = array_map( + fn (array $item) => [ + 'type' => $item['type'], + 'class' => $item['class'], + 'allowsNull' => $item['allowsNull'], + ], + $methodWithUnionParameters + ); + + self::assertEquals( + [ + 'parameterA' => [ + 'type' => 'string|false', + 'class' => 'string|false', + 'allowsNull' => false, + ], + 'parameterB' => [ + 'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false', + 'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false', + 'allowsNull' => false, + ], + 'parameterB1' => [ + 'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false', + 'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations|false', + 'allowsNull' => false, + ], + 'parameterC' => [ + 'type' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations', + 'class' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeAnnotations', + 'allowsNull' => true, + ], + ], + $methodWithUnionParametersReduced + ); } }