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 #5150

Closed
jnoordsij opened this issue Jan 25, 2023 · 0 comments · Fixed by #5225
Closed

Allow providing named arguments from a data provider #5150

jnoordsij opened this issue Jan 25, 2023 · 0 comments · Fixed by #5225
Labels
feature/data-provider Data Providers type/enhancement A new idea that should be implemented

Comments

@jnoordsij
Copy link
Contributor

jnoordsij commented Jan 25, 2023

Using a data provider you can easily run the same test for a different set of values. However, this construction currently requires the number of parameters to match the expected number, and them be in the exact same order as expected by the test method.

Since PHP 8.0 it is possible to pass argument by name to a method, which can be combined with array destructuring to allow passing arguments by name to a function. This would allow something like the very simple example below:

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DataTest extends TestCase
{
    /**
     * @dataProvider additionProvider
     */
    public function testEquals(int $a, int $b = 0, int $expected = 0): void
    {
        $this->assertSame($expected, $a + $b);
    }

    public function additionProvider(): array
    {
        return [
            [0, 1, 1], // already works
            [0], // already works
            [1, 'b' => 3, 'expected' => 4], // already works (array keys are discarded)
            [1, 'expected' => 4, 'b' => 3], // does not work (as intended) yet
        ];
    }
}

I got my local testsuite to behave like this by removing the array_values call that is applied to the test arguments before running, i.e. changing

$testResult = $this->{$this->name}(...array_values($testArguments));

to

            $testResult = $this->{$this->name}(...$testArguments); 

but I'm not sure of possible consequences elsewhere, so have not yet found time to make a proper PR yet.

Edit: it seems that the array_values call is actually required to make dependencyInput function properly; if I change

$testArguments = array_merge($this->data, $this->dependencyInput);

to

            $testArguments = array_merge($this->data, array_values($this->dependencyInput)); 

all existing tests seem to pass locally as before.

@jnoordsij jnoordsij added the type/enhancement A new idea that should be implemented label Jan 25, 2023
@sebastianbergmann sebastianbergmann added type/bug Something is broken type/enhancement A new idea that should be implemented and removed type/enhancement A new idea that should be implemented type/bug Something is broken labels Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/data-provider Data Providers type/enhancement A new idea that should be implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants