且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHPUnit-当dataProvider返回空数组时,不要失败

更新时间:2023-02-16 20:08:00

虽然我不知道任何phpunit选项(这并不意味着一个不存在),但是您可以使用如下所示的内容:>

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
}

当然,这是一种解决方法,但它应该可以防止出现错误.显然,您的操作将取决于是否允许第一个(或任何任意一个)参数为false,但是您可以进行调整以适合您的测试.

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.