-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathDataProvider.php
278 lines (236 loc) · 8.86 KB
/
DataProvider.php
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Metadata\Api;
use function array_key_exists;
use function array_merge;
use function assert;
use function explode;
use function is_array;
use function is_int;
use function json_decode;
use function json_last_error;
use function json_last_error_msg;
use function preg_match;
use function preg_replace;
use function rtrim;
use function sprintf;
use function str_replace;
use function strlen;
use function substr;
use function trim;
use PHPUnit\Event;
use PHPUnit\Framework\InvalidDataProviderException;
use PHPUnit\Metadata\DataProvider as DataProviderMetadata;
use PHPUnit\Metadata\MetadataCollection;
use PHPUnit\Metadata\Parser\Registry as MetadataRegistry;
use PHPUnit\Metadata\TestWith;
use ReflectionClass;
use ReflectionMethod;
use Throwable;
use Traversable;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class DataProvider
{
/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws InvalidDataProviderException
*/
public function providedData(string $className, string $methodName): ?array
{
$dataProvider = MetadataRegistry::parser()->forMethod($className, $methodName)->isDataProvider();
$testWith = MetadataRegistry::parser()->forMethod($className, $methodName)->isTestWith();
if ($dataProvider->isEmpty() && $testWith->isEmpty()) {
return $this->dataProvidedByTestWithAnnotation($className, $methodName);
}
if ($dataProvider->isNotEmpty()) {
$data = $this->dataProvidedByMethods($className, $methodName, $dataProvider);
} else {
$data = $this->dataProvidedByMetadata($testWith);
}
if ($data === []) {
throw new InvalidDataProviderException(
'Empty data set provided by data provider',
);
}
foreach ($data as $key => $value) {
if (!is_array($value)) {
throw new InvalidDataProviderException(
sprintf(
'Data set %s is invalid',
is_int($key) ? '#' . $key : '"' . $key . '"',
),
);
}
}
return $data;
}
/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws InvalidDataProviderException
*/
private function dataProvidedByMethods(string $className, string $methodName, MetadataCollection $dataProvider): array
{
$testMethod = new Event\Code\ClassMethod($className, $methodName);
$methodsCalled = [];
$result = [];
foreach ($dataProvider as $_dataProvider) {
assert($_dataProvider instanceof DataProviderMetadata);
$dataProviderMethod = new Event\Code\ClassMethod($_dataProvider->className(), $_dataProvider->methodName());
Event\Facade::emitter()->dataProviderMethodCalled(
$testMethod,
$dataProviderMethod,
);
$methodsCalled[] = $dataProviderMethod;
try {
$class = new ReflectionClass($_dataProvider->className());
$method = $class->getMethod($_dataProvider->methodName());
if (!$method->isPublic()) {
throw new InvalidDataProviderException(
sprintf(
'Data Provider method %s::%s() is not public',
$_dataProvider->className(),
$_dataProvider->methodName(),
),
);
}
if (!$method->isStatic()) {
throw new InvalidDataProviderException(
sprintf(
'Data Provider method %s::%s() is not static',
$_dataProvider->className(),
$_dataProvider->methodName(),
),
);
}
if ($method->getNumberOfParameters() > 0) {
throw new InvalidDataProviderException(
sprintf(
'Data Provider method %s::%s() expects an argument',
$_dataProvider->className(),
$_dataProvider->methodName(),
),
);
}
$className = $_dataProvider->className();
$methodName = $_dataProvider->methodName();
$data = $className::$methodName();
} catch (Throwable $e) {
Event\Facade::emitter()->dataProviderMethodFinished(
$testMethod,
...$methodsCalled,
);
throw new InvalidDataProviderException(
$e->getMessage(),
$e->getCode(),
$e,
);
}
if ($data instanceof Traversable) {
$origData = $data;
$data = [];
foreach ($origData as $key => $value) {
if (is_int($key)) {
$data[] = $value;
} elseif (array_key_exists($key, $data)) {
Event\Facade::emitter()->dataProviderMethodFinished(
$testMethod,
...$methodsCalled,
);
throw new InvalidDataProviderException(
sprintf(
'The key "%s" has already been defined by a previous data provider',
$key,
),
);
} else {
$data[$key] = $value;
}
}
}
if (is_array($data)) {
$result = array_merge($result, $data);
}
}
Event\Facade::emitter()->dataProviderMethodFinished(
$testMethod,
...$methodsCalled,
);
return $result;
}
private function dataProvidedByMetadata(MetadataCollection $testWith): array
{
$result = [];
foreach ($testWith as $_testWith) {
assert($_testWith instanceof TestWith);
if ($_testWith->hasName()) {
$key = $_testWith->name();
if (array_key_exists($key, $result)) {
throw new InvalidDataProviderException(
sprintf(
'The key "%s" has already been defined by a previous TestWith attribute',
$key,
),
);
}
$result[$key] = $_testWith->data();
} else {
$result[] = $_testWith->data();
}
}
return $result;
}
/**
* @psalm-param class-string $className
*
* @throws InvalidDataProviderException
*/
private function dataProvidedByTestWithAnnotation(string $className, string $methodName): ?array
{
$docComment = (new ReflectionMethod($className, $methodName))->getDocComment();
if ($docComment === false) {
return null;
}
$docComment = str_replace("\r\n", "\n", $docComment);
$docComment = preg_replace('/\n\s*\*\s?/', "\n", $docComment);
$docComment = substr($docComment, 0, -1);
$docComment = rtrim($docComment, "\n");
if (!preg_match('/@testWith\s+/', $docComment, $matches, PREG_OFFSET_CAPTURE)) {
return null;
}
$offset = strlen($matches[0][0]) + (int) $matches[0][1];
$annotationContent = substr($docComment, $offset);
$data = [];
foreach (explode("\n", $annotationContent) as $candidateRow) {
$candidateRow = trim($candidateRow);
if ($candidateRow[0] !== '[') {
break;
}
$dataSet = json_decode($candidateRow, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidDataProviderException(
'The data set for the @testWith annotation cannot be parsed: ' . json_last_error_msg(),
);
}
$data[] = $dataSet;
}
if (!$data) {
throw new InvalidDataProviderException(
'The data set for the @testWith annotation cannot be parsed.',
);
}
return $data;
}
}