Skip to content

Commit

Permalink
TASK: Add testclass with union type annotations to prove that reflect…
Browse files Browse the repository at this point in the history
…ion handles union type annotations different
  • Loading branch information
mficzel committed Jan 23, 2025
1 parent 0a8733d commit 8d5438e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace Neos\Flow\Tests\Functional\Reflection\Fixtures;

/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

/**
* Dummy class for the Reflection tests
*
*/
class DummyClassWithUnionTypeAnnotations
{
/**
* @param string|false $parameterA
* @param false|DummyClassWithUnionTypeAnnotations $parameterB
* @param false | DummyClassWithUnionTypeAnnotations $parameterB1 Same as B but with spaces in between
* @param DummyClassWithUnionTypeAnnotations|null $parameterC
* @return void
*/
public function methodWithUnionParameters($parameterA, $parameterB, $parameterB1, $parameterC)
{
}

/**
* @return string|false
*/
public function methodWithUnionReturnTypeA()
{
}

/**
* @return false|DummyClassWithUnionTypeAnnotations
*/
public function methodWithUnionReturnTypesB()
{
}

/**
* @return DummyClassWithUnionTypeAnnotations|null
*/
public function methodWithUnionReturnTypesC()
{
}
}
64 changes: 64 additions & 0 deletions Neos.Flow/Tests/Functional/Reflection/ReflectionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,68 @@ public function unionParameterTypesWorkCorrectly()
$methodWithUnionParametersReduced
);
}

/**
* @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
);
}
}

0 comments on commit 8d5438e

Please sign in to comment.