Skip to content

Commit

Permalink
Merge branch '10.5' into 11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 19, 2024
2 parents 69cd67a + 5713658 commit 4a6b558
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
7 changes: 7 additions & 0 deletions ChangeLog-11.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## [11.1.3] - 2024-MM-DD

### Fixed

* [#5819](https://github.com/sebastianbergmann/phpunit/issues/5819): Duplicate keys from different data providers are not handled properly

## [11.1.2] - 2024-04-14

### Fixed
Expand Down Expand Up @@ -39,6 +45,7 @@ All notable changes of the PHPUnit 11.1 release series are documented in this fi
* [#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
* [#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

[11.1.3]: https://github.com/sebastianbergmann/phpunit/compare/11.1.2...11.1
[11.1.2]: https://github.com/sebastianbergmann/phpunit/compare/11.1.1...11.1.2
[11.1.1]: https://github.com/sebastianbergmann/phpunit/compare/11.1.0...11.1.1
[11.1.0]: https://github.com/sebastianbergmann/phpunit/compare/11.0.10...11.1.0
45 changes: 17 additions & 28 deletions src/Metadata/Api/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace PHPUnit\Metadata\Api;

use function array_key_exists;
use function array_merge;
use function assert;
use function explode;
use function is_array;
Expand All @@ -35,7 +34,6 @@
use ReflectionClass;
use ReflectionMethod;
use Throwable;
use Traversable;

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

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;
}
}
}
foreach ($data as $key => $value) {
if (is_int($key)) {
$result[] = $value;
} elseif (array_key_exists($key, $result)) {
Event\Facade::emitter()->dataProviderMethodFinished(
$testMethod,
...$methodsCalled,
);

if (is_array($data)) {
$result = array_merge($result, $data);
throw new InvalidDataProviderException(
sprintf(
'The key "%s" has already been defined by a previous data provider',
$key,
),
);
} else {
$result[$key] = $value;
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/_files/DuplicateKeyDataProvidersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\TestFixture;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class DuplicateKeyDataProvidersTest extends TestCase
{
public static function dataProvider1(): iterable
{
return [
'bar' => [1],
];
}

public static function dataProvider2(): iterable
{
return [
'bar' => [2],
];
}

#[DataProvider('dataProvider1')]
#[DataProvider('dataProvider2')]
public function test($value): void
{
$this->assertSame(2, $value);
}
}
12 changes: 11 additions & 1 deletion tests/unit/Metadata/Api/DataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\InvalidDataProviderException;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\DuplicateKeyDataProvidersTest;
use PHPUnit\TestFixture\DuplicateKeyDataProviderTest;
use PHPUnit\TestFixture\MultipleDataProviderTest;
use PHPUnit\TestFixture\TestWithAttributeDataProviderTest;
Expand Down Expand Up @@ -157,7 +158,7 @@ public function testWithVariousIterableNonStaticDataProviders(): void
], $dataSets);
}

public function testWithDuplicateKeyDataProviders(): void
public function testWithDuplicateKeyDataProvider(): void
{
$this->expectException(InvalidDataProviderException::class);
$this->expectExceptionMessage('The key "foo" has already been defined by a previous data provider');
Expand Down Expand Up @@ -186,4 +187,13 @@ public function testTestWithAttributeWithDuplicateKey(): void
/* @noinspection UnusedFunctionResultInspection */
(new DataProvider)->providedData(TestWithAttributeDataProviderTest::class, 'testWithDuplicateName');
}

public function testWithDuplicateKeyDataProviders(): void
{
$this->expectException(InvalidDataProviderException::class);
$this->expectExceptionMessage('The key "bar" has already been defined by a previous data provider');

/* @noinspection UnusedFunctionResultInspection */
(new DataProvider)->providedData(DuplicateKeyDataProvidersTest::class, 'test');
}
}

0 comments on commit 4a6b558

Please sign in to comment.