用于检测字符串中 PHP 错误/警告的库

发布于 2024-12-04 11:14:31 字数 646 浏览 0 评论 0原文

我们使用 PHPUnit 运行单元测试和功能测试,通过 HTTP 查询一些 URL 并根据一些 XPath 检查输出。

现在有时,PHP 错误会出现在页面的 HTML 输出中(display_errors 处于打开状态,因为它是一个开发系统),我希望获得详细的 phpunit 错误消息以及 html 页面中的错误。

处理错误没有问题,我知道该怎么做。问题是从 HTML 输出中提取错误消息 - 以及启用 xdebug 时的堆栈跟踪。

有没有一个我可以使用的库已经可以做到这一点?


问题

如果您为该页面本身编写一个测试,而不是检索该页面的测试,可能会更好?

不幸的是,并非所有遗留代码都易于测试。因此,执行一些 HTTP 查询并检查 HTML 有时是唯一(或最简单)的选择。

设置一个自定义错误处理程序来记录单元测试时发生的所有错误不是更容易吗?

这些测试部分在远程服务器上运行,我无法从单元测试中直接访问这些服务器。

当然,除了我将它们记录在一些公共可访问文件中并通过 http 获取它们之外 - 但随后我需要弄清楚错误属于哪个测试,并且当几个人同时运行测试时会出现问题。

We're using PHPUnit to run both unit tests and functional tests that query some URLs via HTTP and check the output against some XPaths.

Now sometimes, PHP errors appear in the HTML output of the pages (display_errors is on, since it's a dev system) and I'd like to have detailed phpunit error messages with the error from the html page.

Handling the error is no problem, I know how to do this. The problem is extracting the error message - and when xdebug is enabled, the stack trace - from the HTML output.

Is there a library I can use which does that already?


Questions

it would probably be better if you wrote a test for that page itself, instead of something that retrieves the page?

Unfortunately, not all legacy code is easily testable. So doing some HTTP queries and checking the HTML is sometimes the only (or easiest) option.

Is not easier to set a custom error handler which logs all errors that occur while unit testing?

The tests are partly run on remote servers where I don't have direct access to from my unit tests.

Except of course I log them in some public accessible file and fetch them via http, too - but then I need to figure out which test the error belongs to, and have problems when several people run the tests at the same time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-12-11 11:14:31

我不知道有这样的库,但是穷人的 preg_match 解决方案对我来说效果很好。喜欢

 if (preg_match('~\n(Notice|Warning|Fatal error): (.+?) in (\S+) on line (\d+)~')......

I'm not aware of such a library, but the poor man's solution with preg_match works quite well for me. Like

 if (preg_match('~\n(Notice|Warning|Fatal error): (.+?) in (\S+) on line (\d+)~')......
白云悠悠 2024-12-11 11:14:31

我会将测试运行的时间和日志文件中的错误消息的时间关联起来。因此,我也不会记录错误,而是记录错误,而不是 display_errors = On。这假设您正在服务器上运行单元测试。

如果没有,您必须使用 syslog-ng 来访问日志文件。如果所有这些都是基于云的,我会将其推送到 loggly 并使用他们的 API 进行搜索。无论如何!

我不知道有任何交钥匙解决方案,但简而言之,这就是我想要做的。

基本想法是编写 测试监听器

class CweiskeListener extends PHPUnit_Framework_TestListener
{
    public function endTest(PHPUnit_Framework_Test $test, $time)
    {
        // Feb 25 20:36:06
        $syslogDateStr = date('M d h:i');
        // run something like:
        $logOut = system("cat /var/log/syslog|grep {$syslogDateStr}");
        // fail the test here? 
    }
}

HTH – 我还没有尝试过代码,但它应该可以帮助您入门。

I'd correlate time of the test run and the time of the error message from the logfile. So instead of display_errors = On, or in addition, I'd log the error as well. This assumes you are running unit tests on the server server.

If not, you'd have to use syslog-ng to get to your log file. If all of this is cloud-based, I'd push it into loggly and use their API to search. Anyhow!

I'm not aware of any turn-key-solution but that is what I'd try to do in a nutshell.

The basic idea would be to write a test listener:

class CweiskeListener extends PHPUnit_Framework_TestListener
{
    public function endTest(PHPUnit_Framework_Test $test, $time)
    {
        // Feb 25 20:36:06
        $syslogDateStr = date('M d h:i');
        // run something like:
        $logOut = system("cat /var/log/syslog|grep {$syslogDateStr}");
        // fail the test here? 
    }
}

HTH – I haven't tried the code but it should get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文