Repository navigation
Expand file tree
/
Copy pathDataProvider.php
More file actions
142 lines (124 loc) · 4.64 KB
/
Copy pathDataProvider.php
File metadata and controls
142 lines (124 loc) · 4.64 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
<?php
declare(strict_types=1);
namespace Codeception\Test;
use Codeception\Exception\InvalidTestException;
use Codeception\Exception\TestParseException;
use Codeception\Util\Annotation;
use Codeception\Util\ReflectionHelper;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use function sprintf;
class DataProvider
{
public static function getDataForMethod(ReflectionMethod $method, ?ReflectionClass $class = null): ?iterable
{
$testClass = self::getTestClass($method, $class);
$testClassName = $testClass->getName();
$methodName = $method->getName();
// example annotation
$rawExamples = array_values(
Annotation::forMethod($testClassName, $methodName)->fetchAll('example'),
);
if ($rawExamples !== []) {
$rawExample = reset($rawExamples);
if (is_string($rawExample)) {
$result = array_map(
static fn ($v): ?array => Annotation::arrayValue($v),
$rawExamples
);
} else {
$result = $rawExamples;
}
} else {
$result = [];
}
// dataProvider annotation
$dataProviderAnnotations = Annotation::forMethod($testClassName, $methodName)->fetchAll('dataProvider');
// lowercase for back compatible
if (empty($dataProviderAnnotations)) {
$dataProviderAnnotations = Annotation::forMethod($testClassName, $methodName)->fetchAll('dataprovider');
}
if ($result === [] && $dataProviderAnnotations === []) {
return null;
}
foreach ($dataProviderAnnotations as $dataProviderAnnotation) {
[$dataProviderClassName, $dataProviderMethodName] = self::parseDataProviderAnnotation(
$dataProviderAnnotation,
$testClassName,
$methodName,
);
try {
$dataProviderMethod = new ReflectionMethod($dataProviderClassName, $dataProviderMethodName);
if ($dataProviderMethod->isStatic()) {
$dataProviderResult = call_user_func([$dataProviderClassName, $dataProviderMethodName]);
} else {
$testInstance = new $dataProviderClassName($dataProviderMethodName);
if ($dataProviderMethod->isPublic()) {
$dataProviderResult = $testInstance->$dataProviderMethodName();
} else {
$dataProviderResult = ReflectionHelper::invokePrivateMethod(
$testInstance,
$dataProviderMethodName,
);
}
}
foreach ($dataProviderResult as $key => $value) {
if (is_int($key)) {
$result [] = $value;
} else {
$result[$key] = $value;
}
}
} catch (ReflectionException) {
throw new InvalidTestException(sprintf(
"DataProvider '%s' for %s::%s is invalid or not callable",
$dataProviderAnnotation,
$testClassName,
$methodName
));
}
}
return $result;
}
/**
* @return string[]
* @throws TestParseException
*/
public static function parseDataProviderAnnotation(
string $annotation,
string $testClassName,
string $testMethodName,
): array {
$parts = explode('::', $annotation);
if (count($parts) > 2) {
throw new InvalidTestException(
sprintf(
'Data provider "%s" specified for %s::%s is invalid',
$annotation,
$testClassName,
$testMethodName,
)
);
}
if (count($parts) === 2) {
return $parts;
}
return [
$testClassName,
$annotation,
];
}
/**
* Retrieves actual test class for dataProvider.
*/
private static function getTestClass(ReflectionMethod $dataProviderMethod, ?ReflectionClass $testClass): ReflectionClass
{
$dataProviderDeclaringClass = $dataProviderMethod->getDeclaringClass();
// data provider in abstract class?
if ($dataProviderDeclaringClass->isAbstract() && null !== $testClass && $dataProviderDeclaringClass->name !== $testClass->name) {
$dataProviderDeclaringClass = $testClass;
}
return $dataProviderDeclaringClass;
}
}