3

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.

2
  • Is not easier to set a custom error handler which logs all errors that occur while unit testing? Commented Sep 15, 2011 at 9:35
  • Not an answer to your question, but it would probably be better (if there's nothing specific stopping you), if you wrote a test for that page itself, instead of something that retrieves the page? Commented Sep 15, 2011 at 9:35

2 Answers 2

1

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+)~')......
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's possible but does not go far enough. I'd also like to get xdebug stacktraces when they are there, and hand-crafting the preg to do that is not something I'd like to do. A lib would be better in that case.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.