gpt4 book ai didi

php - 自定义错误处理程序 : this phpunit test fails. 为什么? (PHP7)

转载 作者:搜寻专家 更新时间:2023-10-31 21:03:42 25 4
gpt4 key购买 nike

我正在学习 PHP,与此同时,我正在编写玩具代码来理解我所学的内容。

这些是我的“错误和异常处理程序”函数。

我的意图是:

  • 将任何“旧式”PHP 错误转换为异常
  • 显示异常
  • 在系统日志中记录错误消息

function display_exception($exception)
{
print_r($exception);
}

function log_error_or_exception($error_type, $error_message, $error_number = null, $file_name = null, $line_number = null)
{

$text = 'Message: ' . $error_message . ' - ErrorType: ' . $error_type;

if ($error_number) {
$text .= ' - Error#: ' . $error_number;
}
if ($file_name) {
$text .= ' -> ' . $file_name;
}
if ($line_number) {
$text .= ':' . $line_number;
}

syslog(LOG_ERR, $text);
}


/**
* This function logs any uncaught exception that has been thrown.
* If in the "dev" environment it also displays the Exception
*
* @var Exception $exception
* @return void
*/
function custom_exception_handler($exception)
{
if (ENV == 'dev') {
display_exception($exception);
}

log_error_or_exception(get_class($exception), $exception->getMessage(), $exception->getCode(), $exception->getFile(), $exception->getLine());
}

/**
* This function converts an "old style" PHP Error in an ErrorException
*
* @var int $errno
* @var string $errstr
* @var string|null $errfile
* @var int|null $errline
* @var array $errcontext
* @return false|void
*/
function custom_error_handler(int $errno, string $errstr, string $errfile = null, int $errline = null, array $errcontext = array())
{
// if error_reporting() is false it means that the error was trigger by a line having
// the error control operator "@" so the error should never be displayed.
// Sometimes thought it's convenient to log it
if (!error_reporting()) {
if (ENV == 'dev') {
log_error_or_exception('Error', $errstr, $errno, $errfile, $errline);
}
return false;
}

throw new ErrorException($errstr, $errno, 1, $errfile, $errline);
echo "this should not be displayed";
}

set_exception_handler('custom_exception_handler');
set_error_handler('custom_error_handler');

现在我有一个对象

<?php

require_once 'app/src/ErrorExceptionHandlers.php';

class Obj
{
public function method($value){
$result = preg_match('/@/', $value);
}
}

如果我运行这段代码,一切都会按预期工作:

define('ENV', 'dev');

$object = new Obj;
$object->method(array());

我得到:

ErrorException Object
(
[message:protected] => preg_match() expects parameter 2 to be string, array given
[string:Exception:private] =>
...
)

如果我尝试运行这个 phpunit 测试

<?php

require_once 'app/src/models/obj.php';

define('ENV', 'dev');

class ObjTest extends \PHPUnit_Framework_TestCase
{
/**
* @ExpectedException ErrorException
*/
public function test_method(){
$object = new Obj();
$object->method(array());
}
}

我得到:

There was 1 error:

1) ObjTest::test_method
ErrorException: preg_match() expects parameter 2 to be string, array given

为什么测试失败?

最佳答案

因此,注释名称区分大小写。@ExpectedException 应该是 @expectedException

关于php - 自定义错误处理程序 : this phpunit test fails. 为什么? (PHP7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36267449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com