-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathAbstractTestCase.php
More file actions
185 lines (163 loc) · 6.78 KB
/
AbstractTestCase.php
File metadata and controls
185 lines (163 loc) · 6.78 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
/**
* Parser Reflection API
*
* @copyright Copyright 2016, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Go\ParserReflection;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use Go\ParserReflection\Stub\SimplePhp50AbstractClassWithMethods;
abstract class AbstractTestCase extends TestCase
{
protected const DEFAULT_STUB_FILENAME = '/Stub/FileWithClasses55.php';
protected ReflectionFile $parsedRefFile;
protected ReflectionFileNamespace $parsedRefFileNamespace;
protected ?ReflectionClass $parsedRefClass = null;
/**
* Name of the class to compare
*/
protected static string $reflectionClassToTest = \Reflection::class;
/**
* Name of the class to load for default tests
*/
protected static string $defaultClassToLoad = SimplePhp50AbstractClassWithMethods::class;
#[DoesNotPerformAssertions]
final public function testCoverAllMethods(): void
{
$allInternalMethods = get_class_methods(static::$reflectionClassToTest);
$allMissedMethods = [];
foreach ($allInternalMethods as $internalMethodName) {
$refMethod = new \ReflectionMethod(__NAMESPACE__ . '\\' . static::$reflectionClassToTest, $internalMethodName);
$definerClass = $refMethod->getDeclaringClass()->getName();
if (!str_starts_with($definerClass, __NAMESPACE__)) {
$allMissedMethods[] = $internalMethodName;
}
}
if (count($allMissedMethods) > 0) {
$this->markTestIncomplete('Methods ' . join(', ', $allMissedMethods) . ' are not implemented for ' . static::$reflectionClassToTest);
}
}
/**
* Provides a list of files for analysis
*/
public static function getFilesToAnalyze(): \Generator
{
yield 'PHP5.5' => [
__DIR__ . '/Stub/FileWithClasses55.php',
__DIR__ . '/Stub/FileWithFunctions55.php',
__DIR__ . '/Stub/FileWithParameters55.php',
];
yield 'PHP5.6' => [
__DIR__ . '/Stub/FileWithClasses56.php',
__DIR__ . '/Stub/FileWithParameters56.php'
];
yield 'PHP7.0' => [
__DIR__ . '/Stub/FileWithClasses70.php',
__DIR__ . '/Stub/FileWithParameters70.php',
__DIR__ . '/Stub/FileWithFunctions70.php',
];
yield 'PHP7.1' => [__DIR__ . '/Stub/FileWithClasses71.php'];
yield 'PHP7.2' => [__DIR__ . '/Stub/FileWithClasses72.php'];
yield 'PHP7.4' => [__DIR__ . '/Stub/FileWithClasses74.php'];
yield 'PHP8.0' => [
__DIR__ . '/Stub/FileWithClasses80.php',
__DIR__ . '/Stub/FileWithParameters80.php',
__DIR__ . '/Stub/FileWithFunction80.php',
];
yield 'PHP8.1' => [__DIR__ . '/Stub/FileWithClasses81.php'];
yield 'PHP8.2' => [__DIR__ . '/Stub/FileWithClasses82.php'];
if (PHP_VERSION_ID >= 80300) {
yield 'PHP8.3' => [__DIR__ . '/Stub/FileWithClasses83.php'];
}
if (PHP_VERSION_ID >= 80400) {
yield 'PHP8.4' => [__DIR__ . '/Stub/FileWithClasses84.php'];
}
}
/**
* Provides generator list in the form [ParsedFile, ParsedFileNamespace]
*/
public static function getFileNamespacesToAnalyze(): \Generator
{
foreach (static::getFilesToAnalyze() as $prefix => $fileList) {
foreach ($fileList as $fileName) {
$fileName = stream_resolve_include_path($fileName);
$fileNode = ReflectionEngine::parseFile($fileName);
$reflectionFile = new ReflectionFile($fileName, $fileNode);
include_once $fileName;
foreach ($reflectionFile->getFileNamespaces() as $fileNamespace) {
yield $prefix => [$reflectionFile, $fileNamespace];
}
}
}
}
/**
* Provides generator list in the form [ParsedClass, ReflectionClass]
*/
public static function classesDataProvider(): \Generator
{
foreach (static::getFileNamespacesToAnalyze() as $prefix => [$reflectionFile, $fileNamespace]) {
foreach ($fileNamespace->getClasses() as $parsedClass) {
$refClass = new \ReflectionClass($parsedClass->getName());
yield $prefix . ' ' . $refClass->getName() => [$parsedClass, $refClass];
}
}
}
/**
* Provides full test-case list in the form [ParsedClass, ReflectionMethod, \ReflectionMethod to check]
*/
public static function methodsDataProvider(): \Generator
{
foreach (self::classesDataProvider() as $prefix => [$parsedClass, $refClass]) {
foreach ($refClass->getMethods() as $classMethod) {
$parsedMethod = $parsedClass->getMethod($classMethod->getName());
$fullMethodName = $parsedClass->getName() . '->' . $classMethod->getName() . '()';
yield $prefix . ' ' . $fullMethodName => [
$parsedClass,
$parsedMethod,
$classMethod
];
}
}
}
/**
* Provides generator list in the form [ParsedFunction, ReflectionFunction]
*/
public static function functionsDataProvider(): \Generator
{
foreach (static::getFileNamespacesToAnalyze() as $prefix => [$reflectionFile, $fileNamespace]) {
foreach ($fileNamespace->getFunctions() as $parsedFunction) {
$refFunction = new \ReflectionFunction($parsedFunction->getName());
yield $prefix . ' ' . $refFunction ->getName() => [$parsedFunction, $refFunction];
}
}
}
/**
* Returns list of ReflectionMethod getters that be checked directly without additional arguments
*/
abstract static protected function getGettersToCheck(): array;
/**
* Setups file for parsing
*/
protected function setUpFile(string $fileName): void
{
$fileName = stream_resolve_include_path($fileName);
$fileNode = ReflectionEngine::parseFile($fileName);
$reflectionFile = new ReflectionFile($fileName, $fileNode);
$this->parsedRefFile = $reflectionFile;
$parsedFileNamespace = $reflectionFile->getFileNamespace('Go\ParserReflection\Stub');
$this->parsedRefFileNamespace = $parsedFileNamespace;
if ($parsedFileNamespace->hasClass(static::$defaultClassToLoad)) {
$this->parsedRefClass = $parsedFileNamespace->getClass(static::$defaultClassToLoad);
}
include_once $fileName;
}
protected function setUp(): void
{
$this->setUpFile(__DIR__ . static::DEFAULT_STUB_FILENAME);
}
}