Test with dependencies and data provider fails
| Q | A |
|---|---|
| PHPUnit version | 11.1.3 |
| PHP version | 8.3.1 |
| Installation Method | Composer |
Summary
If test case has dependency on other test and has data provider, than it will fail to pass arguments with error
1) App\Tests\MyTest::testSomething with data set "DataSet1" (true, ['a', 'b'])
Error: Cannot use positional argument after named argument during unpacking
How to reproduce
Create following test case
public function testOtherMethod(): void
{
...
return 'PREV_TEST';
}
#[Depends('testOtherMethod')]
#[DataProviderExternal(MyTestDataProvider::class, 'getSomethingData')]
public function testSomething(bool $expected, array $data, array $previousTestData): void
{
}
Thrown in PHPUnit\Framework\TestCase::runTest@1173
final protected function runTest(): mixed
{
$testArguments = array_merge($this->data, array_values($this->dependencyInput)); // ['expected' => true, 'data' => ['a', 'b'], 0 => 'PREV_TEST'];
try {
$testResult = $this->{$this->methodName}(...$testArguments);
} catch (Throwable $exception) {
Expected behavior
Test method is called correctly
Possible solution
Do not merge arguments to one array
final protected function runTest(): mixed
{
try {
$testResult = $this->{$this->methodName}(...$this->data, ...array_values($this->dependencyInput));
} catch (Throwable $exception) {
Thank you for your report.
Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.
Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.
Did this work with PHPUnit 11.1.2 and now no longer works with PHPUnit 11.1.3?
In my case, this error occurs if the data provider method returns an array with a mixture of associative and non-associative arrays.
Example:
<?php declare(strict_types=1);
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
final class DataProviderTestCase extends TestCase
{
#[DataProvider('workingProvider')]
public function testUseWorkingProvider(string $from, int $expected): void
{
$this->assertSame('january', $from);
$this->assertSame(4, $expected);
}
public static function workingProvider(): array
{
return [
[ 'from' => 'january', 'expected' => 4 ],
];
}
#[DataProvider('notWorkingProvider')]
public function testUseNotWorking(string $from, int $expected): void
{
$this->assertSame('january', $from);
$this->assertSame(4, $expected);
}
public static function notWorkingProvider(): array
{
return [
[ 'from' => 'january', 4 ],
];
}
}
My output:
PHPUnit 11.1.3 by Sebastian Bergmann and contributors.
Runtime: PHP 8.3.6
.E 2 / 2 (100%)
Time: 00:00.022, Memory: 4.00 MB
There was 1 error:
1) DataProviderTestCase::testUseNotWorking with data set #0 ('january', 4)
Error: Cannot use positional argument after named argument during unpacking
ERRORS!
Tests: 2, Assertions: 2, Errors: 1.
Failed to execute command php vendor/bin/phpunit TestCase.php: exit status 2
And for 11.1.2 it also doesn't work:
PHPUnit 11.1.2 by Sebastian Bergmann and contributors.
Runtime: PHP 8.3.6
.E 2 / 2 (100%)
Time: 00:00.014, Memory: 4.00 MB
There was 1 error:
1) DataProviderTestCase::testUseNotWorking with data set #0 ('january', 4)
Error: Cannot use positional argument after named argument during unpacking
ERRORS!
Tests: 2, Assertions: 2, Errors: 1.
Failed to execute command php vendor/bin/phpunit TestCase.php: exit status 2