Skip to content

Commit

Permalink
Fix ignored test with duplicate key in dataprovider
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Dec 13, 2018
1 parent c1b8b32 commit 0654c1d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
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;
} 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;
}
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 testWithVDuplicateKeyDataProviders(): 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

0 comments on commit 0654c1d

Please sign in to comment.