Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit - Don't fail when the dataProvider returns an empty array

Tags:

phpunit

I have a PHPUnit test which uses a @dataProvider. The dataprovider checks the filesystem for certain files.

However I'm using this test in different environments what means it can happen that the files do not exist. This means that the dataProvider does not find anything and that the test is not executed.

And this causes the test run to end in a failure.

Question: is the a way to configure PHPUnit to ignore tests which have providers that produce nothing?

like image 483
thehpi Avatar asked Feb 12 '23 21:02

thehpi


1 Answers

While I do not know of any phpunit option (which doesn't mean one doesn't exist), you could just use something like the following:

public function providerNewFileProvider()
{
    //get files from the "real" provider
    $files = $this->providerOldFileProvider();
    //no files? then, make a fake entry to "skip" the test
    if (empty($files)) {
        $files[] = array(false,false);
    }

    return $files;
}

/**
* @dataProvider providerNewFileProvider
*/

public function testMyFilesTest($firstArg,$secondArg)
{
    if (false === $firstArg) {
        //no files existing is okay, so just end immediately
        $this->markTestSkipped('No files to test');
    }

    //...normal operation code goes here
}

It's a workaround, sure, but it should stop an error from showing up. Obviously, whatever you do will depend on whether the first (or any arbitrary) argument is allowed to be false, but you can make tweaks to fit your tests.

like image 63
Willem Renzema Avatar answered Apr 28 '23 17:04

Willem Renzema