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.
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+)~')......
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.
精彩评论