Skip to content

Commit

Permalink
TASK: Add test to document union type parameter handling in type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
mficzel committed Jan 23, 2025
1 parent 8df5aa8 commit 493078a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
*/
class DummyClassWithUnionTypeHints
{
public function methodWithUnionParameters(
string|false $parameterA,
false|DummyClassWithUnionTypeHints $parameterB,
null|DummyClassWithUnionTypeHints $parameterC
): void {
}

public function methodWithUnionReturnTypeA(): string|false
{
}
Expand Down
75 changes: 69 additions & 6 deletions Neos.Flow/Tests/Functional/Reflection/ReflectionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,75 @@ public function annotatedArrayTypeHintsWorkCorrectly()
*/
public function unionReturnTypesWorkCorrectly()
{
$returnTypeA = $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypeA');
$returnTypeB = $this->reflectionService->getMethodDeclaredReturnType(Fixtures\DummyClassWithUnionTypeHints::class, 'methodWithUnionReturnTypesB');
$returnTypeC = $this->reflectionService->getMethodDeclaredReturnType(Fixtures\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
);
}

self::assertEquals('string|false', $returnTypeA);
self::assertEquals('\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false', $returnTypeB);
self::assertEquals('?\Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints', $returnTypeC);
/**
* @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(
[
'parameterA' => 'string|false',
'parameterB' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints|false',
'parameterC' => 'Neos\Flow\Tests\Functional\Reflection\Fixtures\DummyClassWithUnionTypeHints'
],
array_map(fn(array $item)=>$item['class'], $methodWithUnionParameters)
);

self::assertEquals(
[
'parameterA' => false,
'parameterB' => false,
'parameterC' => true
],
array_map(fn(array $item)=>$item['allowsNull'], $methodWithUnionParameters)
);
}
}

0 comments on commit 493078a

Please sign in to comment.