Skip to content

Commit 4a6b558

Browse files
Merge branch '10.5' into 11.1
2 parents 69cd67a + 5713658 commit 4a6b558

File tree

4 files changed

+72
-29
lines changed

4 files changed

+72
-29
lines changed

ChangeLog-11.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes of the PHPUnit 11.1 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [11.1.3] - 2024-MM-DD
6+
7+
### Fixed
8+
9+
* [#5819](https://github.com/sebastianbergmann/phpunit/issues/5819): Duplicate keys from different data providers are not handled properly
10+
511
## [11.1.2] - 2024-04-14
612

713
### Fixed
@@ -39,6 +45,7 @@ All notable changes of the PHPUnit 11.1 release series are documented in this fi
3945
* [#5689](https://github.com/sebastianbergmann/phpunit/issues/5689): The `restrictDeprecations` attribute on the `<source>` element of the XML configuration file is now deprecated in favor of the `ignoreSelfDeprecations`, `ignoreDirectDeprecations`, and `ignoreIndirectDeprecations` attributes
4046
* [#5709](https://github.com/sebastianbergmann/phpunit/issues/5709): Deprecate support for using comma-separated values with the `--group`, `--exclude-group`, `--covers`, `--uses`, and `--test-suffix` CLI options
4147

48+
[11.1.3]: https://github.com/sebastianbergmann/phpunit/compare/11.1.2...11.1
4249
[11.1.2]: https://github.com/sebastianbergmann/phpunit/compare/11.1.1...11.1.2
4350
[11.1.1]: https://github.com/sebastianbergmann/phpunit/compare/11.1.0...11.1.1
4451
[11.1.0]: https://github.com/sebastianbergmann/phpunit/compare/11.0.10...11.1.0

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;
@@ -35,7 +34,6 @@
3534
use ReflectionClass;
3635
use ReflectionMethod;
3736
use Throwable;
38-
use Traversable;
3937

4038
/**
4139
* @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -157,33 +155,24 @@ private function dataProvidedByMethods(string $className, string $methodName, Me
157155
);
158156
}
159157

160-
if ($data instanceof Traversable) {
161-
$origData = $data;
162-
$data = [];
163-
164-
foreach ($origData as $key => $value) {
165-
if (is_int($key)) {
166-
$data[] = $value;
167-
} elseif (array_key_exists($key, $data)) {
168-
Event\Facade::emitter()->dataProviderMethodFinished(
169-
$testMethod,
170-
...$methodsCalled,
171-
);
172-
173-
throw new InvalidDataProviderException(
174-
sprintf(
175-
'The key "%s" has already been defined by a previous data provider',
176-
$key,
177-
),
178-
);
179-
} else {
180-
$data[$key] = $value;
181-
}
182-
}
183-
}
158+
foreach ($data as $key => $value) {
159+
if (is_int($key)) {
160+
$result[] = $value;
161+
} elseif (array_key_exists($key, $result)) {
162+
Event\Facade::emitter()->dataProviderMethodFinished(
163+
$testMethod,
164+
...$methodsCalled,
165+
);
184166

185-
if (is_array($data)) {
186-
$result = array_merge($result, $data);
167+
throw new InvalidDataProviderException(
168+
sprintf(
169+
'The key "%s" has already been defined by a previous data provider',
170+
$key,
171+
),
172+
);
173+
} else {
174+
$result[$key] = $value;
175+
}
187176
}
188177
}
189178

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
use PHPUnit\Framework\Attributes\DataProvider;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class DuplicateKeyDataProvidersTest extends TestCase
16+
{
17+
public static function dataProvider1(): iterable
18+
{
19+
return [
20+
'bar' => [1],
21+
];
22+
}
23+
24+
public static function dataProvider2(): iterable
25+
{
26+
return [
27+
'bar' => [2],
28+
];
29+
}
30+
31+
#[DataProvider('dataProvider1')]
32+
#[DataProvider('dataProvider2')]
33+
public function test($value): void
34+
{
35+
$this->assertSame(2, $value);
36+
}
37+
}

tests/unit/Metadata/Api/DataProviderTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\Attributes\Small;
1515
use PHPUnit\Framework\InvalidDataProviderException;
1616
use PHPUnit\Framework\TestCase;
17+
use PHPUnit\TestFixture\DuplicateKeyDataProvidersTest;
1718
use PHPUnit\TestFixture\DuplicateKeyDataProviderTest;
1819
use PHPUnit\TestFixture\MultipleDataProviderTest;
1920
use PHPUnit\TestFixture\TestWithAttributeDataProviderTest;
@@ -157,7 +158,7 @@ public function testWithVariousIterableNonStaticDataProviders(): void
157158
], $dataSets);
158159
}
159160

160-
public function testWithDuplicateKeyDataProviders(): void
161+
public function testWithDuplicateKeyDataProvider(): void
161162
{
162163
$this->expectException(InvalidDataProviderException::class);
163164
$this->expectExceptionMessage('The key "foo" has already been defined by a previous data provider');
@@ -186,4 +187,13 @@ public function testTestWithAttributeWithDuplicateKey(): void
186187
/* @noinspection UnusedFunctionResultInspection */
187188
(new DataProvider)->providedData(TestWithAttributeDataProviderTest::class, 'testWithDuplicateName');
188189
}
190+
191+
public function testWithDuplicateKeyDataProviders(): void
192+
{
193+
$this->expectException(InvalidDataProviderException::class);
194+
$this->expectExceptionMessage('The key "bar" has already been defined by a previous data provider');
195+
196+
/* @noinspection UnusedFunctionResultInspection */
197+
(new DataProvider)->providedData(DuplicateKeyDataProvidersTest::class, 'test');
198+
}
189199
}

0 commit comments

Comments
 (0)