Skip to content

Commit

Permalink
Fix an issue with numeric data provider keys and run tests with forma…
Browse files Browse the repository at this point in the history
…t clear for both PHPUnit >= 10 and PHPUnit < 10
  • Loading branch information
sidz committed Dec 6, 2023
1 parent 5d7a36f commit f573156
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use function ltrim;
use function preg_quote;
use function rtrim;
use function Safe\preg_match;
use function sprintf;
use function version_compare;

Expand Down Expand Up @@ -127,19 +128,22 @@ private function getMethodNameWithDataProvider(string $methodNameWithDataProvide
$methodNameWithDataProviderResult = $methodNameWithDataProvider;

/*
* in PHPUnit >=10 data providers with keys are stored as `Class\\test_method#some key`
* in PHPUnit <10 data providers with keys are stored as `Class\\test_method with data set "some key"`
* in PHPUnit >=10 data providers with keys are stored as `Class\\test_method#some key` or `Class\\test_method#0`
* in PHPUnit <10 data providers with keys are stored as `Class\\test_method with data set "some key"` or `Class\\test_method with data set #0`
*
* we need to translate to the old format because this is what PHPUnit <10 and >=10 understands from CLI `--filter` option
*/
if (version_compare($testFrameworkVersion, '10', '>=')) {
$methodNameParts = explode('#', $methodNameWithDataProviderResult, self::MAX_EXPLODE_PARTS);

if (count($methodNameParts) > 1) {
$methodName = $methodNameParts[0];
$dataProviderKey = $methodNameParts[1];
[$methodName, $dataProviderKey] = $methodNameParts;

$methodNameWithDataProviderResult = sprintf('%s with data set "%s"', $methodName, $dataProviderKey);
if (preg_match('/^(\d+)$/', $dataProviderKey)) {

Check failure on line 142 in src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php

View workflow job for this annotation

GitHub Actions / Autoreview on PHP 8.2

Only booleans are allowed in an if condition, int given.
$methodNameWithDataProviderResult = sprintf('%s with data set #%s', $methodName, $dataProviderKey);
} else {
$methodNameWithDataProviderResult = sprintf('%s with data set "%s"', $methodName, $dataProviderKey);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,24 @@ public function provideTestCases(): Generator
'10.1',
'/Test\:\:test_case1 with data set "With special chars \:\:"/',
];

yield '1 test case from data provider with numeric data provider keys - PHPUnit10' => [
true,
[
'App\Test::test_case1#0',
'App\Test::test_case1#1',
],
'10.5',
'/Test\:\:test_case1 with data set \#0|Test\:\:test_case1 with data set \#1/',
];

yield '1 test case from data provider with leading numbers in the key string - PHPUnit10' => [
true,
[
'App\Test::test_case1#123a',
],
'10.5',
'/Test\:\:test_case1 with data set "123a"/',
];
}
}

0 comments on commit f573156

Please sign in to comment.