Is there any way to 'expect' output to error_log in PHPUnit tests?

♀尐吖头ヾ 提交于 2019-12-19 06:19:31

问题


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?


回答1:


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().




回答2:


I couldn't get this working with error_log, but was able to check the error using trigger_error. Try using annotations.

 * @expectedException PHPUnit_Framework_Exception
 * @expectedExceptionMessageRegExp /failed/

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)?




回答3:


You can use the uopz extension from Zend to overload functions like these. I use them all the time. Here is an example:

/**
 * Requires the following set in php.ini: 
 * - zend_extension=php_uopz.dll;
 * - uopz.overloads=1
 * 
* @author Aap Noot
*/
class MyClass extends PHPUnit_Framework_TestCase {
    public static $error_log = array();

     /**
     * Overload error_log function
     * @see PHPUnit_Framework_TestCase::setUpBeforeClass()
     */
    public static function setUpBeforeClass() {
        uopz_backup ( "error_log" );
        uopz_function ( "error_log", function ($message, $message_type = null, $destination = null, $extra_headers = null) {
            // We are only interested in the message
            MyClass::$error_log[] = $message;
        } );
        parent::setUpBeforeClass ();

    /**
     * Restore function(s)
     * @see PHPUnit_Framework_TestCase::tearDownAfterClass()
     */
    public static function tearDownAfterClass() {
        uopz_restore ( "error_log" );
        parent::tearDownAfterClass ();
    } 

    /**
    * Set up per test case
    * @see PHPUnit_Framework_TestCase::setUp()
    */
    protected function setUp() {
        parent::setUp ();
        MyClass::$error_log = array();
    }

    /**
     * Test error log
     * MyClass::$error_log should be an array with the error message
     */
    public function testErrorLog() {
        // Test response
        error_log("This message will be captured");

        // Test error log
        $this->assertNotEmpty(MyClass::$error_log);
    }
}


来源:https://stackoverflow.com/questions/21784240/is-there-any-way-to-expect-output-to-error-log-in-phpunit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!