-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathReflectionFunctionTest.php
More file actions
123 lines (111 loc) · 4.77 KB
/
Copy pathReflectionFunctionTest.php
File metadata and controls
123 lines (111 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Go\ParserReflection;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class ReflectionFunctionTest extends AbstractTestCase
{
protected static string $reflectionClassToTest = \ReflectionFunction::class;
protected const DEFAULT_STUB_FILENAME = '/Stub/FileWithFunctions55.php';
public const STUB_FILE70 = '/Stub/FileWithFunctions70.php';
/**
* Performs method-by-method comparison with original reflection
*
* @param ReflectionFunction $parsedFunction Parsed function
* @param \ReflectionFunction $refFunction Original function
* @param string $getterName Name of the reflection method to test
*/
#[DataProvider('reflectionGetterDataProvider')]
public function testReflectionGetterParity(
ReflectionFunction $parsedFunction,
\ReflectionFunction $refFunction,
string $getterName
): void {
$functionName = $refFunction->getName();
$expectedValue = $refFunction->$getterName();
$actualValue = $parsedFunction->$getterName();
// I would like to completely stop maintaining the __toString method
if ($expectedValue !== $actualValue && $getterName === '__toString') {
$this->markTestSkipped("__toString for function {$functionName}() is not equal:\n{$expectedValue}{$actualValue}");
}
$this->assertSame(
$expectedValue,
$actualValue,
"$getterName() for function {$functionName}() should be equal"
);
}
public function testGetClosureMethod(): void
{
$fileNamespace = $this->parsedRefFile->getFileNamespace('Go\ParserReflection\Stub');
$refFunc = $fileNamespace->getFunction('noGeneratorFunc');
$closure = $refFunc->getClosure();
$this->assertInstanceOf(\Closure::class, $closure);
$retValue = $closure();
$this->assertSame(100, $retValue);
}
public function testInvokeMethod(): void
{
$fileNamespace = $this->parsedRefFile->getFileNamespace('Go\ParserReflection\Stub');
$refFunc = $fileNamespace->getFunction('funcWithReturnArgs');
$retValue = $refFunc->invoke(1, 2, 3);
$this->assertSame([1, 2, 3], $retValue);
}
public function testInvokeArgsMethod(): void
{
$fileNamespace = $this->parsedRefFile->getFileNamespace('Go\ParserReflection\Stub');
$refFunc = $fileNamespace->getFunction('funcWithReturnArgs');
$retValue = $refFunc->invokeArgs([1, 2, 3]);
$this->assertSame([1, 2, 3], $retValue);
}
#[DataProvider('functionsDataProvider')]
public function testGetReturnTypeMethod(
ReflectionFunction $parsedRefFunction, \ReflectionFunction $originalRefFunction
): void {
$functionName = $parsedRefFunction->getName();
$hasReturnType = $originalRefFunction->hasReturnType();
$this->assertSame(
$originalRefFunction->hasReturnType(),
$parsedRefFunction->hasReturnType(),
"Presence of return type for function {$functionName} should be equal"
);
if ($hasReturnType) {
$parsedReturnType = $parsedRefFunction->getReturnType();
$originalReturnType = $originalRefFunction->getReturnType();
$this->assertSame($originalReturnType->allowsNull(), $parsedReturnType->allowsNull());
$this->assertSame($originalReturnType->__toString(), $parsedReturnType->__toString());
} else {
$this->assertSame(
$originalRefFunction->getReturnType(),
$parsedRefFunction->getReturnType()
);
}
}
/**
* Provides full test-case list in the form [ReflectionFunction, \ReflectionFunction, getter name to check]
*/
public static function reflectionGetterDataProvider(): \Generator
{
$allNameGetters = self::getGettersToCheck();
foreach (self::functionsDataProvider() as $prefix => [$parsedFunction, $refFunction]) {
foreach ($allNameGetters as $getterName) {
yield $prefix . ', ' . $getterName => [
$parsedFunction,
$refFunction,
$getterName
];
}
}
}
/**
* @inheritDoc
*/
static protected function getGettersToCheck(): array
{
return [
'getStartLine', 'getEndLine', 'getDocComment', 'getExtension', 'getExtensionName',
'getName', 'getNamespaceName', 'getShortName', 'inNamespace', 'getStaticVariables',
'getNumberOfParameters', 'getNumberOfRequiredParameters',
'returnsReference', 'getClosureScopeClass', 'getClosureThis', 'hasReturnType', '__toString'
];
}
}