- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想使用 simpleTest 编写一个测试,如果我正在测试的方法导致 PHP E_NOTICE
“undefined index : foo”,该测试将失败。
我尝试了 expectError()
和 expectException()
但没有成功。 simpleTest 网页表明 simpleTest 无法捕获编译时 PHP 错误,但 E_NOTICE
似乎是运行时错误。
有没有办法捕获这样的错误并使我的测试失败?
最佳答案
这并不容易,但我终于设法捕获了我想要的 E_NOTICE
错误。我需要覆盖当前的 error_handler
以抛出我将在 try{}
语句中捕获的异常。
function testGotUndefinedIndex() {
// Overriding the error handler
function errorHandlerCatchUndefinedIndex($errno, $errstr, $errfile, $errline ) {
// We are only interested in one kind of error
if ($errstr=='Undefined index: bar') {
//We throw an exception that will be catched in the test
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
return false;
}
set_error_handler("errorHandlerCatchUndefinedIndex");
try {
// triggering the error
$foo = array();
echo $foo['bar'];
} catch (ErrorException $e) {
// Very important : restoring the previous error handler
restore_error_handler();
// Manually asserting that the test fails
$this->fail();
return;
}
// Very important : restoring the previous error handler
restore_error_handler();
// Manually asserting that the test succeed
$this->pass();
}
这似乎有点过于复杂,不得不重新声明错误处理程序以抛出异常只是为了捕获它。另一个困难的部分是在捕获到异常且未发生错误时正确恢复 error_handler,否则它只会混淆 SimpleTest 错误处理。
关于php - 如何在 simpleTest 中捕获 "undefined index"E_NOTICE 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3261051/
E_ALL ^ E_NOTICE 和 E_ALL & ~E_NOTICE 有什么区别? 据我了解,它们都排除了 E_NOTICE 级别的错误报告。但是在 PHP.ini 中使用了 &~ 形式,而我在很
我更喜欢在打开 E_NOTICES 的情况下进行开发,但是在处理数组索引时,我经常不得不编写过多的、过于冗长的代码。如何在启用 E_NOTICES 的情况下用更少的代码编写此代码。 禁止通知 if (
Looks like a lot of people did not understand the question, the notice I am getting is not about $fr
我尝试使用 laravel 在 mysql 中插入数据,但收到错误 ErrorException (E_NOTICE)试图获取非对象的属性,我不知道问题出在哪里,请帮助我。 我的 Controller
作为一名开发人员,我在打开 E_NOTICE 的情况下工作。不过最近有人问我为什么要修复 E_NOTICE 错误。我能想到的唯一原因是纠正这些问题是最佳实践。 还有其他人有任何理由证明花费额外的时间/
举例说明: 在Windows环境下:原本在php4.3.0中运行正常的程序,在4.3.1中为何多处报错,大体提示为:Notice:Undefined varialbe:变量名称.&nb
我应该在实时服务器上启用 E_NOTICE 吗? 正如我在 Why should I fix E_NOTICE errors? 中看到的那样 每个人都再次使用 isset()省略通知。我读过专业人士,
我正在编写自己的 var_dump 以满足某些可能只适用于我并且有问题的要求。 代码检查对象、数组等,最后进入一个阶段,它认为它留下了一个数字、一个字符串或一个 bool 值。 当然,一个字符串实际上
我有一个 PHP 脚本需要执行数小时,有时由于某些原因(例如执行需要连接到互联网的脚本时出现网络问题等),执行过程会停止一段时间后做了错误的事情(并且它总是导致 E_NOTICE 当进程走向错误的方向
我保证,我知道最好不要忽略错误。运行 XML-Sitemaps 生成器会点击没有有效 session 信息的 URL 来取悦 CodeIgniter。结果是每个被抓取的页面都有一个 E_NOTICE
通常 php 脚本在 E_NOTICE 之后继续运行,有没有办法在函数的上下文中将此提升为 fatal error ,也就是说我只需要在我的函数中通知退出而不是在核心 php 函数中退出,也就是说全局
我已经从 heroku 克隆了我的项目,并且正在使用 wampserver 和 composer 版本 2。有人可以帮助我理解为什么会出现此错误。 在 PackageManifest.php 第 13
我现在遇到的问题是很难找到真正的错误,因为发现了太多的 E_NOTICE。我们的服务器上有将近 600 个脚本复杂的站点,我为处理其中的大量站点做了很多工作,但仍然有很多。 每当错误百分比过高时,我都
首先我知道这个问题在这里不止一次出现过: Why should I fix E_NOTICE errors? Why should I fix E_NOTICE errors? Pros and co
这确实是一个非常简单的问题,我如何将 undefined variable 传递给函数而不会出现 E_NOTICE 错误? 当将 undefined variable 传递给诸如 isset() 之类
谁能解释一下 error_reporting(E_ALL); 和 error_reporting(E_ALL & ~E_NOTICE); 之间的区别? 我注意到,当我从 E_ALL 更改为 E_ALL
有哪些更好的方法可以避免应用程序逻辑中出现大量 isset(),并在需要时保留查看调试消息的能力 (E_NOTICE)? p> 首先假设:E_NOTICE 不是错误,而是用词不当,实际上应该是 E_D
对于 Symfony2 项目,我必须在博客文章和所谓的平台之间建立关系。平台根据您用于查看站点的域定义特定过滤器。例如:如果您通过 url first-example.com 加入该站点,该站点将仅提
我想使用 simpleTest 编写一个测试,如果我正在测试的方法导致 PHP E_NOTICE“undefined index : foo”,该测试将失败。 我尝试了 expectError() 和
我收到此错误:function split() deprecated list ($kk, $vv) = split( ' ', $buf, 2); 当我用 explode 或 preg_split
我是一名优秀的程序员,十分优秀!