Skip to content

Commit d2d13b7

Browse files
committed
feat(Data): New behavior for DataZip: if the providers have different lengths, the resulting data sets will be as many as the shortest provider.
1 parent 5b42e7c commit d2d13b7

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/Data/DataZip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Zips multiple data providers together.
1414
*
1515
* Each data set will contain one value from each provider, combined into an array.
16-
* If the providers have different lengths, `null` will be used for missing values.
16+
* If the providers have different lengths, the resulting data sets will be as many as the shortest provider.
1717
*
1818
* @api
1919
*/

src/Data/Internal/DataProviderInterceptor.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,16 @@ static function () use ($info, $providerAttr) {
225225
);
226226

227227
dataset:
228-
$allFinished = true;
229228
$dataSet = [];
230229
$key = [];
231230
foreach ($generators as $gen) {
232-
if ($gen->valid()) {
233-
$allFinished = false;
234-
$dataSet[] = $gen->current();
235-
$key[] = $gen->key();
236-
$gen->next();
237-
} else {
238-
$key[] = '';
239-
$dataSet[] = null;
231+
if (!$gen->valid()) {
232+
return;
240233
}
241-
}
242234

243-
if ($allFinished) {
244-
return;
235+
$dataSet[] = $gen->current();
236+
$key[] = $gen->key();
237+
$gen->next();
245238
}
246239

247240
yield \implode(self::KEY_SEPARATOR_ZIP, $key) => \array_merge(...$dataSet);

tests/Data/Self/DataZip.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public static function numbersProvider(): array
1919
];
2020
}
2121

22+
public static function aFewLettersProvider(): iterable
23+
{
24+
yield 'ab' => ['a', 'b'];
25+
yield 'cd' => ['c', 'd'];
26+
}
27+
2228
public static function lettersProvider(): iterable
2329
{
2430
yield 'ab' => ['a', 'b'];
@@ -39,4 +45,17 @@ public function sum(int $a, int $b, string $c, string $d): void
3945
[5, 6, 'e', 'f'],
4046
], true));
4147
}
48+
49+
#[Test]
50+
#[\Testo\Data\DataZip(
51+
new DataProvider('numbersProvider'),
52+
new DataProvider('aFewLettersProvider'),
53+
)]
54+
public function sumFew(int $a, int $b, string $c, string $d): void
55+
{
56+
Assert::true(\in_array([$a, $b, $c, $d], [
57+
[1, 2, 'a', 'b'],
58+
[3, 4, 'c', 'd'],
59+
], true));
60+
}
4261
}

0 commit comments

Comments
 (0)