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

Test with dependencies and data provider fails #5827

Open
Justinas-Jurciukonis opened this issue Apr 24, 2024 · 3 comments
Open

Test with dependencies and data provider fails #5827

Justinas-Jurciukonis opened this issue Apr 24, 2024 · 3 comments
Labels
feature/data-provider Data Providers feature/test-dependencies Issues related to explicitly declared dependencies between tests status/waiting-for-feedback Waiting for feedback from original reporter type/bug Something is broken

Comments

@Justinas-Jurciukonis
Copy link

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) {
@Justinas-Jurciukonis Justinas-Jurciukonis added the type/bug Something is broken label Apr 24, 2024
@sebastianbergmann sebastianbergmann added the status/waiting-for-feedback Waiting for feedback from original reporter label Apr 24, 2024
@sebastianbergmann
Copy link
Owner

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.

@sebastianbergmann sebastianbergmann added feature/data-provider Data Providers feature/test-dependencies Issues related to explicitly declared dependencies between tests labels Apr 24, 2024
@sebastianbergmann
Copy link
Owner

Did this work with PHPUnit 11.1.2 and now no longer works with PHPUnit 11.1.3?

@nepda
Copy link

nepda commented May 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/data-provider Data Providers feature/test-dependencies Issues related to explicitly declared dependencies between tests status/waiting-for-feedback Waiting for feedback from original reporter type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

3 participants