Is there any way to #39;expect#39; output to error_log in PHPUnit tests?(有没有办法在 PHPUnit 测试中“期望输出到 error_log?)
问题描述
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?
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:
error_log( 'This is a message from error_log()', 3, '/dev/stdout' );
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:
$cur_error_log = ini_get( 'error_log' );
ini_set( 'error_log', '/dev/stdout' );
error_log( 'This is a message from error_log()' );
ini_set( 'error_log', $cur_error_log );
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().
这篇关于有没有办法在 PHPUnit 测试中“期望"输出到 error_log?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法在 PHPUnit 测试中“期望"输出到 error_log?


- 带有通配符的 Laravel 验证器 2021-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01