Skip to content

Commit

Permalink
Added Unit tests for php 8 union types.
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jun 18, 2020
1 parent 0700b7f commit 7b5c6a3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Extractor/ReflectionExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,15 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref

foreach ($reflectionType instanceof \ReflectionUnionType ? $reflectionType->getTypes() : [$reflectionType] as $type) {
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : (string) $type;
if ('null' === $phpTypeOrClass) {
continue;
}

if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
$types[] = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
} elseif ('void' === $phpTypeOrClass || 'null' === $phpTypeOrClass) {
} elseif ('void' === $phpTypeOrClass) {
$types[] = new Type(Type::BUILTIN_TYPE_NULL, $nullable);
} elseif ($reflectionType->isBuiltin()) {
} elseif ($type->isBuiltin()) {
$types[] = new Type($phpTypeOrClass, $nullable);
} else {
$types[] = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $reflectionMethod));
Expand Down
20 changes: 20 additions & 0 deletions Tests/Extractor/ReflectionExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ public function php71TypesProvider()
];
}

/**
* @dataProvider php80TypesProvider
* @requires PHP 8
*/
public function testExtractPhp80Type($property, array $type = null)
{
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy', $property, []));
}

public function php80TypesProvider()
{
return [
['foo', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]],
['bar', [new Type(Type::BUILTIN_TYPE_INT, true)]],
['timeout', [new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_FLOAT)]],
['optional', [new Type(Type::BUILTIN_TYPE_INT, true), new Type(Type::BUILTIN_TYPE_FLOAT, true)]],
['string', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Stringable'), new Type(Type::BUILTIN_TYPE_STRING)]],
];
}

/**
* @dataProvider getReadableProperties
*/
Expand Down
26 changes: 26 additions & 0 deletions Tests/Fixtures/Php80Dummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

class Php80Dummy
{
public function getFoo(): array|null
{
}

public function setBar(int|null $bar)
{
}

public function setTimeout(int|float $timeout)
{
}

public function getOptional(): int|float|null
{
}

public function setString(string|\Stringable $string)
{
}
}

0 comments on commit 7b5c6a3

Please sign in to comment.