如何在php中对自定义异常处理程序进行单元测试?
您会在php中测试自定义异常处理程序吗?例如,我有以下几点:如何在php中对自定义异常处理程序进行单元测试?,php,phpunit,Php,Phpunit,您会在php中测试自定义异常处理程序吗?例如,我有以下几点: <?php namespace Freya\Exceptions; /** * Custom exception handler. * * Instantiate with: * * <pre> * new Freya\Exceptions\ExceptionHandler(); * </pre> * * This is a dependency in the Freya-Load
<?php
namespace Freya\Exceptions;
/**
* Custom exception handler.
*
* Instantiate with:
*
* <pre>
* new Freya\Exceptions\ExceptionHandler();
* </pre>
*
* This is a dependency in the Freya-Loader package and is instantiated for you in the constructor of the auto loader.
*
* @package Freya\Exceptions
*/
class ExceptionHandler {
/**
* Set up the exception handler.
*/
public function __construct() {
set_exception_handler(array($this, 'exceptionHandler'));
}
/**
* Create the exception handler.
*
* Start with the message that was produced. Then provide a stack trace.
*/
public function exceptionHandler($exception) {
echo $exception->getMessage();
echo '<br />';
echo '<pre> ' . $exception->getTraceAsString() . ' </pre>';
}
}
您可以通过抛出任意异常并手动验证输出,从技术上测试您的类
通过传递一个newexception(),可以对类进行自动化的、可重复的测试代码>到您的异常处理程序
类并验证输出。您可以通过调用$lastHandler=set\u exception\u handler(null)来测试构造函数
以验证最后一个集合处理程序是否是您的自定义处理程序
从这两个测试中,您可以放心,PHP已经完成了自己的单元测试,以确保set\u exception\u handler
工作。在某个地方填充throw'Whatever'
?测试是否抛出错误,该错误的内容如何,以确保有堆栈跟踪并打印消息?或者我应该费心测试这个类吗?