How to catch PHP Warning in PHPUnit

后端 未结 6 1034
迷失自我
迷失自我 2021-01-01 15:47

I am writing test cases and here is a question I have.

So say I am testing a simple function someClass::loadValue($value)

The normal test case

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 16:04

    PHPUnit_Util_ErrorHandler::handleError() throws one of several exception types based on the error code:

    • PHPUnit_Framework_Error_Notice for E_NOTICE, E_USER_NOTICE, and E_STRICT
    • PHPUnit_Framework_Error_Warning for E_WARNING and E_USER_WARNING
    • PHPUnit_Framework_Error for all others

    You can catch and expect these as you would any other exception.

    /**
     * @expectedException PHPUnit_Framework_Error_Warning
     */
    function testNegativeNumberTriggersWarning() {
        $fixture = new someClass;
        $fixture->loadValue(-1);
    }
    

提交回复
热议问题