phpstan-phpunit icon indicating copy to clipboard operation
phpstan-phpunit copied to clipboard

Detect invalid phpunit `@dataProvider`

Open staabm opened this issue 1 year ago • 2 comments

Feature request

we should error about dataProvider annotations without a corresponding method

https://phpstan.org/r/9870d218-28c5-4636-88e9-85f73ce78bc9

Did PHPStan help you today? Did it make you happy in any way?

No response

staabm avatar Oct 03 '24 11:10 staabm

Why doesn't PHPUnit warn / error about this?

ruudk avatar Oct 03 '24 11:10 ruudk

it does:

➜  phpstan-phpunit git:(2.0.x) ✗ vendor/bin/phpunit test.php
PHPUnit 9.6.16 by Sebastian Bergmann and contributors.

Warning:       No code coverage driver available

E                                                                   1 / 1 (100%)

Time: 00:00.002, Memory: 6.00 MB

There was 1 error:

1) Error
The data provider specified for test::testTrim is invalid.
PHPUnit\Util\Exception: Method test::wrongDataProvider() does not exist

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

staabm avatar Oct 03 '24 12:10 staabm

Also, since PHPUnit 11, it checks if data provider "named arguments" match the param names. And if not, PHPUnit fails hard. So that might also be part of the "invalid dataprovider rule".

class FooTest extends TestCase
{

    public static function provideData(): iterable
    {
        yield [
            'receivingKey' => 'foobar', // fails, the param is named $receiving
        ];
    }

    #[DataProvider('provideData')]
    public function testWhater(string $receiving)
    {
       // ...
    }
}

janedbal avatar Nov 25 '24 14:11 janedbal

my initial request is invalid. it already works as expected:

 ------ ----------------------------------------------------------- 
  Line   test.php                                                   
 ------ ----------------------------------------------------------- 
  9      @dataProvider wrongDataProvider related method not found.  
         🪪  phpunit.dataProviderMethod                             
 ------ ----------------------------------------------------------- 

staabm avatar Dec 29 '24 11:12 staabm

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 30 '25 00:01 github-actions[bot]