Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ignored test with duplicate key in dataprovider #3444

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Framework/InvalidDataProviderException.php
@@ -0,0 +1,14 @@
<?php
/*
* 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\Framework;

class InvalidDataProviderException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Util/Test.php
Expand Up @@ -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;
Expand Down Expand Up @@ -881,6 +882,14 @@ private static function getDataFromDataProviderAnnotation(string $docComment, st
foreach ($origData as $key => $value) {
if (\is_int($key)) {
$data[] = $value;
} elseif (\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;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/_files/DuplicateKeyDataProviderTest.php
@@ -0,0 +1,27 @@
<?php
/*
* 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.
*/
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
{
}
}
9 changes: 9 additions & 0 deletions tests/unit/Util/TestTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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 */");
Expand Down