Skip to content

Commit

Permalink
Added more return type hints tests for ReflectionFunctionAbstract
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Dec 11, 2024
1 parent 793f1b3 commit f453c9d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 82 deletions.
13 changes: 0 additions & 13 deletions test/unit/Fixture/Php71NullableReturnTypeDeclarations.php

This file was deleted.

25 changes: 0 additions & 25 deletions test/unit/Fixture/Php7ReturnTypeDeclarations.php

This file was deleted.

45 changes: 45 additions & 0 deletions test/unit/Fixture/ReturnTypeDeclarations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

function returnsInt(): int
{
}

function returnsString(): string
{
}

function returnsObject(): \stdClass
{
}

function returnsNull(): null
{
}

function returnsNothing()
{
}

function returnsVoid(): void
{
}

function returnsNullableInt(): ?int
{
}

function returnsNullableString(): ?string
{
}

function returnsNullableObject(): ?\stdClass
{
}

function returnsUnion(): int|string
{
}

function returnsIntersection(): DateTime&DateTimeImmutable
{
}
64 changes: 20 additions & 44 deletions test/unit/Reflection/ReflectionFunctionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function testGetLocatedSource(): void
self::assertSame($locatedSource, $functionInfo->getLocatedSource());
}

/** @return list<array{0: string, 1: string|class-string}> */
/** @return list<array{0: string, 1: string|class-string|null}> */
public static function returnTypeFunctionProvider(): array
{
return [
Expand All @@ -480,46 +480,30 @@ public static function returnTypeFunctionProvider(): array
['returnsNull', 'null'],
['returnsObject', stdClass::class],
['returnsVoid', 'void'],
['returnsVoid', 'void'],
['returnsNothing', null],
['returnsUnion', 'int|string'],
['returnsIntersection', 'DateTime&DateTimeImmutable'],
];
}

#[DataProvider('returnTypeFunctionProvider')]
public function testGetReturnTypeWithDeclaredType(string $functionToReflect, string $expectedType): void
public function testGetReturnTypeWithDeclaredType(string $functionToReflect, string|null $expectedType): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction($functionToReflect);

$reflectionType = $functionInfo->getReturnType();
self::assertInstanceOf(ReflectionType::class, $reflectionType);
self::assertSame($expectedType, (string) $reflectionType);
}

public function testGetReturnTypeReturnsNullWhenTypeIsNotDeclared(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsNothing');

self::assertNull($functionInfo->getReturnType());
}

public function testHasReturnTypeWhenTypeDeclared(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsString');
if ($expectedType === null) {
self::assertFalse($functionInfo->hasReturnType());
self::assertNull($functionInfo->getReturnType());
} else {
self::assertTrue($functionInfo->hasReturnType());

self::assertTrue($functionInfo->hasReturnType());
}

public function testHasReturnTypeWhenTypeIsNotDeclared(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsNothing');

self::assertFalse($functionInfo->hasReturnType());
$reflectionType = $functionInfo->getReturnType();
self::assertInstanceOf(ReflectionType::class, $reflectionType);
self::assertSame($expectedType, (string) $reflectionType);
}
}

/** @return list<array{0: string, 1: string}> */
Expand All @@ -536,7 +520,7 @@ public static function nullableReturnTypeFunctionProvider(): array
public function testGetNullableReturnTypeWithDeclaredType(string $functionToReflect, string $expectedType): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php71NullableReturnTypeDeclarations.php', $this->astLocator),
new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction($functionToReflect);

$reflectionType = $functionInfo->getReturnType();
Expand All @@ -557,11 +541,13 @@ public function testHasTentativeReturnType(): void
public function testHasNotTentativeReturnType(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
new SingleFileSourceLocator(__DIR__ . '/../Fixture/ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsString');

self::assertFalse($functionInfo->hasTentativeReturnType());
self::assertNull($functionInfo->getTentativeReturnType());
self::assertTrue($functionInfo->hasReturnType());
self::assertNotNull($functionInfo->getReturnType());
}

public function testGetTentativeReturnType(): void
Expand All @@ -576,16 +562,6 @@ public function testGetTentativeReturnType(): void
self::assertNull($methodInfo->getReturnType());
}

public function testNoTentativeReturnType(): void
{
$functionInfo = (new DefaultReflector(
new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php7ReturnTypeDeclarations.php', $this->astLocator),
))->reflectFunction('returnsString');

self::assertNull($functionInfo->getTentativeReturnType());
self::assertNotNull($functionInfo->getReturnType());
}

#[DataProvider('deprecatedDocCommentsProvider')]
public function testFunctionsCanBeDeprecated(string $comment): void
{
Expand Down

0 comments on commit f453c9d

Please sign in to comment.