Skip to content

Allow tests to opt out of the data providers argument count validation #6277

@stof

Description

@stof

PHPUnit 12 has added a warning when a data provider has more values than the number of arguments.

While this warning might allow to catch legitimate mistakes, I have cases where I intentionally relied on the fact that PHP does not forbid passing extra arguments to a function call. This happens when several tests reuse the same data provider, but one of them does not care about the last provided value. I could add an unused parameter, but CS tools would then report it as unused.

Example use case
class VoterTest extends TestCase
{
    // setup omitted, as well as the implementation of custom assertions

    /**
     * Administrator users are granted the security attributes regardless of
     * their permission profile value.
     */
    #[DataProvider('providePermissionMapping')]
    public function testGrantedForAdministrator(string $attribute): void
    {
        $this->currentMember->isAdministrator()->willReturn(true);

        $this->assertGranted($attribute);
    }

    #[DataProvider('providePermissionMapping')]
    public function testGrantedWithPermission(string $attribute, string $profilePermission): void
    {
        $this->permissionChecker->check($profilePermission, $this->user)->willReturn(true);

        $this->assertGranted($attribute);
    }

    #[DataProvider('providePermissionMapping')]
    public function testDeniedWithoutPermission(string $attribute, string $profilePermission): void
    {
        $this->permissionChecker->check($profilePermission, $this->user)->willReturn(false);

        $this->assertDenied($attribute);
    }

    /**
     * @return iterable<array{string, string}>
     */
    public static function providePermissionMapping(): iterable
    {
        // ...
    }
}

To support this use case, it would be great if a test could opt-out of this validation (maybe with a property in the DataProvider attribute, maybe with a separate AllowExtraProvidedArguments attribute, or whatever the name).
Note that such opt-out makes sense only for attributes allowing to use reusable data providers (DataProvider and DataProviderExternal), not for TestWith (as this is specific to a test, there is no use cases for a mismatch)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/enhancementA new idea that should be implemented

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions