/
DatasetsRepository.php
241 lines (194 loc) · 7.04 KB
/
DatasetsRepository.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
<?php
declare(strict_types=1);
namespace Pest\Repositories;
use Closure;
use Generator;
use Pest\Exceptions\DatasetAlreadyExists;
use Pest\Exceptions\DatasetDoesNotExist;
use Pest\Exceptions\ShouldNotHappen;
use Pest\Support\Exporter;
use Traversable;
use function sprintf;
/**
* @internal
*/
final class DatasetsRepository
{
private const SEPARATOR = '>>';
/**
* Holds the datasets.
*
* @var array<string, Closure|iterable<int|string, mixed>>
*/
private static array $datasets = [];
/**
* Holds the withs.
*
* @var array<array<string, Closure|iterable<int|string, mixed>|string>>
*/
private static array $withs = [];
/**
* Sets the given.
*
* @param Closure|iterable<int|string, mixed> $data
*/
public static function set(string $name, Closure|iterable $data, string $scope): void
{
$datasetKey = "$scope".self::SEPARATOR."$name";
if (array_key_exists("$datasetKey", self::$datasets)) {
throw new DatasetAlreadyExists($name, $scope);
}
self::$datasets[$datasetKey] = $data;
}
/**
* Sets the given "with".
*
* @param array<Closure|iterable<int|string, mixed>|string> $with
*/
public static function with(string $filename, string $description, array $with): void
{
self::$withs["$filename".self::SEPARATOR."$description"] = $with;
}
public static function has(string $filename, string $description): bool
{
return array_key_exists($filename.self::SEPARATOR.$description, self::$withs);
}
/**
* @return Closure|array<int|string, mixed>
*
* @throws ShouldNotHappen
*/
public static function get(string $filename, string $description): Closure|array
{
$dataset = self::$withs[$filename.self::SEPARATOR.$description];
$dataset = self::resolve($dataset, $filename);
if ($dataset === null) {
throw ShouldNotHappen::fromMessage('Dataset [%s] not resolvable.');
}
return $dataset;
}
/**
* Resolves the current dataset to an array value.
*
* @param array<Closure|iterable<int|string, mixed>|string> $dataset
* @return array<string, mixed>|null
*/
public static function resolve(array $dataset, string $currentTestFile): ?array
{
if ($dataset === []) {
return null;
}
$dataset = self::processDatasets($dataset, $currentTestFile);
$datasetCombinations = self::getDatasetsCombinations($dataset);
$datasetDescriptions = [];
$datasetValues = [];
foreach ($datasetCombinations as $datasetCombination) {
$partialDescriptions = [];
$values = [];
foreach ($datasetCombination as $datasetCombinationElement) {
$partialDescriptions[] = $datasetCombinationElement['label'];
// @phpstan-ignore-next-line
$values = array_merge($values, $datasetCombinationElement['values']);
}
$datasetDescriptions[] = implode(' / ', $partialDescriptions);
$datasetValues[] = $values;
}
foreach (array_count_values($datasetDescriptions) as $descriptionToCheck => $count) {
if ($count > 1) {
$index = 1;
foreach ($datasetDescriptions as $i => $datasetDescription) {
if ($datasetDescription === $descriptionToCheck) {
$datasetDescriptions[$i] .= sprintf(' #%d', $index++);
}
}
}
}
$namedData = [];
foreach ($datasetDescriptions as $i => $datasetDescription) {
$namedData[$datasetDescription] = $datasetValues[$i];
}
return $namedData;
}
/**
* @param array<Closure|iterable<int|string, mixed>|string> $datasets
* @return array<int, array<int, mixed>>
*/
private static function processDatasets(array $datasets, string $currentTestFile): array
{
$processedDatasets = [];
foreach ($datasets as $index => $data) {
$processedDataset = [];
if (is_string($data)) {
$datasets[$index] = self::getScopedDataset($data, $currentTestFile);
}
if (is_callable($datasets[$index])) {
$datasets[$index] = call_user_func($datasets[$index]);
}
if ($datasets[$index] instanceof Traversable) {
$preserveKeysForArrayIterator = $datasets[$index] instanceof Generator
&& is_string($datasets[$index]->key());
$datasets[$index] = iterator_to_array($datasets[$index], $preserveKeysForArrayIterator);
}
foreach ($datasets[$index] as $key => $values) {
$values = is_array($values) ? $values : [$values];
$processedDataset[] = [
'label' => self::getDatasetDescription($key, $values),
'values' => $values,
];
}
$processedDatasets[] = $processedDataset;
}
return $processedDatasets;
}
/**
* @return Closure|iterable<int|string, mixed>
*/
private static function getScopedDataset(string $name, string $currentTestFile): Closure|iterable
{
$matchingDatasets = array_filter(self::$datasets, function (string $key) use ($name, $currentTestFile): bool {
[$datasetScope, $datasetName] = explode(self::SEPARATOR, $key);
if ($name !== $datasetName) {
return false;
}
return str_starts_with($currentTestFile, $datasetScope);
}, ARRAY_FILTER_USE_KEY);
$closestScopeDatasetKey = array_reduce(
array_keys($matchingDatasets),
fn (string|int|null $keyA, string|int|null $keyB): string|int|null => $keyA !== null && strlen((string) $keyA) > strlen((string) $keyB) ? $keyA : $keyB
);
if ($closestScopeDatasetKey === null) {
throw new DatasetDoesNotExist($name);
}
return $matchingDatasets[$closestScopeDatasetKey];
}
/**
* @param array<array<mixed>> $combinations
* @return array<array<array<mixed>>>
*/
private static function getDatasetsCombinations(array $combinations): array
{
$result = [[]];
foreach ($combinations as $index => $values) {
$tmp = [];
foreach ($result as $resultItem) {
foreach ($values as $value) {
$tmp[] = array_merge($resultItem, [$index => $value]);
}
}
$result = $tmp;
}
// @phpstan-ignore-next-line
return $result;
}
/**
* @param array<int, mixed> $data
*/
private static function getDatasetDescription(int|string $key, array $data): string
{
$exporter = Exporter::default();
if (is_int($key)) {
return sprintf('(%s)', $exporter->shortenedRecursiveExport($data));
}
return sprintf('dataset "%s"', $key);
}
}