【发布时间】:2020-07-17 22:52:43
【问题描述】:
我想测试我组合的自定义错误处理类,但这本身就带来了一些问题。
首先,由于在测试中设置自定义 error_handler 会覆盖 PHPUnits error_handling ,因此每次更改为自定义实现和测试时都必须调用 restore_error_handler() ,以便以后的任何测试都有 phpunits error_handling。虽然我承认没有尝试过,但 PHPUnit 在每次测试后都会自行重置,但我有点怀疑。
我有各种配置要测试,在不同的条件下,即 E_ERROR , E_WARNING 等..
然后我有了模拟 error_handler 的想法,但在模拟测试方面仍然很陌生。一个快速的谷歌搜索让我找到了一个类似的方法,这是修改和实现的想法,可能适合我的需要......
//Create mock with error_handler method
$errorH = $this->getMock ('ErrorHandler', array ('error_handler'));
// error handler should be called at least once
$errorH->expects ($this->atLeastOnce())->method ('error_handler');
// Mock will need to be configured (here?) and tested
// against specific error_handling settings
// not implemented yet nor needed for this post.
//set mock as error handler for duration of this test
set_error_handler (array($errorH, 'error_handler'));
//test current mock error handler configuration
// for different conditions-> NOTICE / WARNING / ERROR
// Give phpunit's toy back!
restore_error_handler ();
这是一种有效的方法吗?
【问题讨论】:
标签: php unit-testing phpunit