Is there any way to run a test on output created from a call to 'error_log("Message")' when doing unit tests with phpunit?
Example code, one of my functions tests a credit card with a luhn algorithm:
if($checkLuhn && ($this->_luhn_check($cardNumber) == false)) {
error_log(__METHOD__ . " cardNumber failed luhn algorithm check.");
return false;
}
$checkLuhn is a boolean passed in to tell it whether to do the check, the _luhn_check() returns true if the $cardNumber passes. Problem is, I have more than one test in this function that can return false. I can use assertEquals on the return value, but also want to check why the error was thrown.
Can you override error_log or otherwise grab syslog output in a unit test somehow?
You can use the uopz extension from Zend to overload functions like these. I use them all the time. Here is an example:
There are a few different ways to direct where error_log() sends data.
First is to just as error_log() to send it some where else. An example would be:
That uses the destination option for error_log().
Another approach is to override the error_log ini setting in PHP. That would look something like:
Of the two options I generally prefer using the destination option in error_log() when possible.
From there you could use expectOutputString() from PHPUnit to look for the data sent from error_log().
I couldn't get this working with error_log, but was able to check the error using trigger_error. Try using annotations.
https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.expectedException
See also :
How to execute code after trigger_error(..., E_USER_WARNING) in unit test (PHPUnit)?