From d7c349953f71db2ea17cd7a58b89a6e491748ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Deruss=C3=A9?= Date: Mon, 10 Dec 2018 11:15:53 +0100 Subject: [PATCH] Fix ignored test with duplicate key in dataprovider --- .../InvalidDataProviderException.php | 14 ++++++++++ src/Util/Test.php | 9 +++++++ tests/_files/DuplicateKeyDataProviderTest.php | 27 +++++++++++++++++++ tests/unit/Util/TestTest.php | 9 +++++++ 4 files changed, 59 insertions(+) create mode 100644 src/Framework/InvalidDataProviderException.php create mode 100644 tests/_files/DuplicateKeyDataProviderTest.php diff --git a/src/Framework/InvalidDataProviderException.php b/src/Framework/InvalidDataProviderException.php new file mode 100644 index 00000000000..3743bdf025b --- /dev/null +++ b/src/Framework/InvalidDataProviderException.php @@ -0,0 +1,14 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\Framework; + +class InvalidDataProviderException extends Exception +{ +} diff --git a/src/Util/Test.php b/src/Util/Test.php index 573fc02f710..aa3436fe053 100644 --- a/src/Util/Test.php +++ b/src/Util/Test.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\CodeCoverageException; use PHPUnit\Framework\Exception; use PHPUnit\Framework\InvalidCoversTargetException; +use PHPUnit\Framework\InvalidDataProviderException; use PHPUnit\Framework\SelfDescribing; use PHPUnit\Framework\SkippedTestError; use PHPUnit\Framework\TestCase; @@ -881,6 +882,14 @@ private static function getDataFromDataProviderAnnotation(string $docComment, st foreach ($origData as $key => $value) { if (\is_int($key)) { $data[] = $value; + } else if(\array_key_exists($key, $data)) { + throw new InvalidDataProviderException( + sprintf( + 'The key "%s" as already been defined in the dataprovider "%s".', + $key, + $match + ) + ); } else { $data[$key] = $value; } diff --git a/tests/_files/DuplicateKeyDataProviderTest.php b/tests/_files/DuplicateKeyDataProviderTest.php new file mode 100644 index 00000000000..cdf486f1dee --- /dev/null +++ b/tests/_files/DuplicateKeyDataProviderTest.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +use PHPUnit\Framework\TestCase; + +class DuplicateKeyDataProviderTest extends TestCase +{ + public static function dataProvider() + { + yield 'foo' => ['foo']; + + yield 'foo' => ['bar']; + } + + /** + * @dataProvider dataProvider + */ + public function test($arg): void + { + } +} diff --git a/tests/unit/Util/TestTest.php b/tests/unit/Util/TestTest.php index 6a1cf49122b..26c86058c5d 100644 --- a/tests/unit/Util/TestTest.php +++ b/tests/unit/Util/TestTest.php @@ -12,6 +12,7 @@ use PharIo\Version\VersionConstraint; use PHPUnit\Framework\CodeCoverageException; use PHPUnit\Framework\Exception; +use PHPUnit\Framework\InvalidDataProviderException; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Warning; @@ -562,6 +563,14 @@ public function testWithVariousIterableDataProviders(): void ], $dataSets); } + public function testWithDuplicateKeyDataProviders(): void + { + $this->expectException(InvalidDataProviderException::class); + $this->expectExceptionMessage('The key "foo" as already been defined in the dataprovider "dataProvider".'); + + Test::getProvidedData(\DuplicateKeyDataProviderTest::class, 'test'); + } + public function testTestWithEmptyAnnotation(): void { $result = Test::getDataFromTestWithAnnotation("/**\n * @anotherAnnotation\n */");