Skip to content

Commit 1e82280

Browse files
kubawerlossebastianbergmann
authored andcommitted
Fix collecting data from data providers
1 parent 0e03860 commit 1e82280

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

src/Metadata/Api/DataProvider.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace PHPUnit\Metadata\Api;
1111

1212
use function array_key_exists;
13-
use function array_merge;
1413
use function assert;
1514
use function explode;
1615
use function is_array;
@@ -39,7 +38,6 @@
3938
use ReflectionClass;
4039
use ReflectionMethod;
4140
use Throwable;
42-
use Traversable;
4341

4442
/**
4543
* @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -176,33 +174,24 @@ private function dataProvidedByMethods(string $className, string $methodName, Me
176174
);
177175
}
178176

179-
if ($data instanceof Traversable) {
180-
$origData = $data;
181-
$data = [];
182-
183-
foreach ($origData as $key => $value) {
184-
if (is_int($key)) {
185-
$data[] = $value;
186-
} elseif (array_key_exists($key, $data)) {
187-
Event\Facade::emitter()->dataProviderMethodFinished(
188-
$testMethod,
189-
...$methodsCalled,
190-
);
191-
192-
throw new InvalidDataProviderException(
193-
sprintf(
194-
'The key "%s" has already been defined by a previous data provider',
195-
$key,
196-
),
197-
);
198-
} else {
199-
$data[$key] = $value;
200-
}
201-
}
202-
}
177+
foreach ($data as $key => $value) {
178+
if (is_int($key)) {
179+
$result[] = $value;
180+
} elseif (array_key_exists($key, $result)) {
181+
Event\Facade::emitter()->dataProviderMethodFinished(
182+
$testMethod,
183+
...$methodsCalled,
184+
);
203185

204-
if (is_array($data)) {
205-
$result = array_merge($result, $data);
186+
throw new InvalidDataProviderException(
187+
sprintf(
188+
'The key "%s" has already been defined by a previous data provider',
189+
$key,
190+
),
191+
);
192+
} else {
193+
$result[$key] = $value;
194+
}
206195
}
207196
}
208197

tests/unit/Metadata/Api/DataProviderTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,10 @@ public function testWithDuplicateKeyDataProvider(): void
168168

169169
public function testWithDuplicateKeyDataProviders(): void
170170
{
171-
$dataSets = (new DataProvider)->providedData(DuplicateKeyDataProvidersTest::class, 'test');
172-
173-
$this->assertSame(
174-
[
175-
'bar' => [2],
176-
],
177-
$dataSets,
178-
);
171+
$this->expectException(InvalidDataProviderException::class);
172+
$this->expectExceptionMessage('The key "bar" has already been defined by a previous data provider');
173+
174+
/* @noinspection UnusedFunctionResultInspection */
175+
(new DataProvider)->providedData(DuplicateKeyDataProvidersTest::class, 'test');
179176
}
180177
}

0 commit comments

Comments
 (0)