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

Allow providing named arguments from a data provider #5225

Merged
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
4 changes: 2 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1176,10 +1176,10 @@ final protected function registerFailureType(string $classOrInterface): void
*/
final protected function runTest(): mixed
{
$testArguments = array_merge($this->data, $this->dependencyInput);
$testArguments = array_merge($this->data, array_values($this->dependencyInput));

try {
$testResult = $this->{$this->name}(...array_values($testArguments));
$testResult = $this->{$this->name}(...$testArguments);
} catch (Throwable $exception) {
if (!$this->shouldExceptionExpectationsBeVerified($exception)) {
throw $exception;
Expand Down
30 changes: 30 additions & 0 deletions tests/_files/DataProviderNamedArgumentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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;

class DataProviderNamedArgumentsTest extends TestCase
{
public static function providerMethod()
{
return [
['a' => 1, 'b' => 2, 'c' => 3],
['c' => 3, 'a' => 2, 'b' => 1],
];
}

#[DataProvider('providerMethod')]
public function testAdd($a, $b, $c): void
{
$this->assertEquals($c, $a + $b);
}
}
20 changes: 20 additions & 0 deletions tests/end-to-end/generic/dataprovider-named-arguments.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
phpunit ../../_files/DataProviderNamedArgumentsTest.php
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = __DIR__ . '/../../_files/DataProviderNamedArgumentsTest.php';

require_once __DIR__ . '/../../bootstrap.php';
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

.. 2 / 2 (100%)

Time: %s, Memory: %s

OK (2 tests, 2 assertions)