From 7b5c6a3791182d359dbf3211f388687b262c6fe1 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 18 Jun 2020 22:18:19 +0200 Subject: [PATCH] Added Unit tests for php 8 union types. --- Extractor/ReflectionExtractor.php | 7 ++++-- Tests/Extractor/ReflectionExtractorTest.php | 20 ++++++++++++++++ Tests/Fixtures/Php80Dummy.php | 26 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 Tests/Fixtures/Php80Dummy.php diff --git a/Extractor/ReflectionExtractor.php b/Extractor/ReflectionExtractor.php index cbcb349..59af875 100644 --- a/Extractor/ReflectionExtractor.php +++ b/Extractor/ReflectionExtractor.php @@ -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)); diff --git a/Tests/Extractor/ReflectionExtractorTest.php b/Tests/Extractor/ReflectionExtractorTest.php index 36fc9d5..bac036a 100644 --- a/Tests/Extractor/ReflectionExtractorTest.php +++ b/Tests/Extractor/ReflectionExtractorTest.php @@ -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 */ diff --git a/Tests/Fixtures/Php80Dummy.php b/Tests/Fixtures/Php80Dummy.php new file mode 100644 index 0000000..484498f --- /dev/null +++ b/Tests/Fixtures/Php80Dummy.php @@ -0,0 +1,26 @@ +