本文介绍了捕获ArgumentCountError和PHPUnit_Framework_Error_Warning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人向我的一个库提交了一个拉取请求,其中通过将function doSomething($var)之类的内容替换为function doSomething($var = 'whatever')来使参数成为可选参数.

Someone submitted a pull request to a library of mine wherein a parameter was made optional by replacing something like function doSomething($var) with function doSomething($var = 'whatever').

因此,我添加了一个单元测试,以确保如果没有将足够的变量传递给该方法,则将发出错误.为了解决这个问题,我使用了PHPUnit注释@expectedException.对于PHP 7.0,预期的例外是PHPUnit_Framework_Error_Warning,但是对于PHP 7.1+,预期的例外是ArgumentCountError.这带来了一个小问题.我可以使测试通过PHP 7.0或更早版本,或者通过PHP 7.1或更高版本.我不能让他们都支持.

So I added a unit test to make sure that an error would be issued if you didn't pass enough variables to the method. To catch this I was using the PHPUnit annotation @expectedException. For PHP 7.0 the expected exception is PHPUnit_Framework_Error_Warning but for PHP 7.1+ the expected exception is ArgumentCountError. This presents a small problem. I can make the tests pass PHP 7.0 and earlier or pass PHP 7.1 and later. I can't make them support both.

另一个PHPUnit注释是@requires,但看来这仅允许您将测试限制为最低PHP版本-而不是最高PHP版本.例如.如果我执行@requires PHP 7.1,则意味着PHP 7.1是运行测试所需的最低PHP版本,但无法使PHP 7.0成为运行测试的最高版本.

Another PHPUnit annotation is @requires but it seems like that only lets you restrict tests to a minimum PHP version - not to a maximum PHP version. eg. if I do @requires PHP 7.1 that'd mean that PHP 7.1 is the minimum version of PHP required to run the test but that there's no way to make PHP 7.0 the maximum version to run a test.

我认为执行@expectedException Exception是可行的(因为大概PHPUnit_Framework_Error_WarningArgumentCountError都扩展了Exception,但事实并非如此.

I thought doing @expectedException Exception would work (since presumably PHPUnit_Framework_Error_Warning and ArgumentCountError both extend Exception but that does not appear to be the case either.

如果我可以做类似@expectedException PHPUnit_Framework_Error_Warning|ArgumentCountError的事情,那将很酷,但是PHPUnit文档中没有任何内容使我相信我可以并且 https://github.com/sebastianbergmann/phpunit/issues/2216 听起来好像无法完成.

It'd be cool if I could do something like @expectedException PHPUnit_Framework_Error_Warning|ArgumentCountError but nothing in the PHPUnit docs leads me to believe that I can and https://github.com/sebastianbergmann/phpunit/issues/2216 makes it sound like that can't be done period.

也许我应该一起删除这个特定的单元测试?

Maybe I should just remove this particular unit test all together?

推荐答案

您可以使用expectException()方法调用,而不是@expectedException批注.仍然建议使用方法调用 .

You can use the expectException() method call, instead of the @expectedException annotation. Using the method call is recommended anyway.

测试中的条件通常不是一个好主意,因为测试应该简单明了,但是如果您坚持要执行以下操作:

Conditionals in tests are usually a bad idea as tests should be straightforward, but if you insist you could implement the following:

public function testIt()
{
    if (PHP_VERSION_ID >= 70100) {
        $this->expectException(ArgumentCountError::class);
    } else {
        $this->expectException(PHPUnit_Framework_Error_Warning::class);
    }

    // ...
}

您还可以实现两个单独的测试用例,并根据PHP版本跳过一个或另一个:

You could also implement two separate test cases and skip one or the other based on the PHP version:

public function testItForPHP70()
{
    if (PHP_VERSION_ID >= 70100) {
        $this->markTestSkipped('PHPUnit_Framework_Error_Warning exception is thrown for legacy PHP versions only');
    }

    $this->expectException(PHPUnit_Framework_Error_Warning::class);

    // ...
}

public function testItForPHP71AndUp()
{
    if (PHP_VERSION_ID < 70100) {
        $this->markTestSkipped('ArgumentCountError exception is thrown for latest PHP versions only');
    }

    $this->expectException(ArgumentCountError::class);

    // ...
}

这篇关于捕获ArgumentCountError和PHPUnit_Framework_Error_Warning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:01
查看更多