test the return value of a method that triggers an error with PHPUnit

前端 未结 5 1611
自闭症患者
自闭症患者 2020-12-16 17:33

This question is specific to using PHPUnit.

PHPUnit automatically converts php errors to exceptions. Is there a way to test the return value of a method that

5条回答
  •  悲哀的现实
    2020-12-16 17:53

    This answer is a bit late to the party, but anyhow:

    You can use Netsilik/BaseTestCase (MIT License) to test directly for triggered Notices/Warnings, without ignoring them or converting them to Exceptions. Because the notices/warnings they are not converted to an Exception, the execution is not halted.

    composer require netsilik/base-test-case


    Testing for an E_USER_NOTICE:

    bar();
    
            self::assertErrorTriggered(E_USER_NOTICE, 'The notice message');
        }
    }
    

    Hope this helps someone in the future.

提交回复
热议问题