catching ArgumentCountError and PHPUnit_Framework_Error_Warning

后端 未结 1 1127
不思量自难忘°
不思量自难忘° 2021-01-20 19:47

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

相关标签:
1条回答
  • 2021-01-20 20:22

    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);
        }
    
        // ...
    }
    

    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);
    
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题