How can I get phpunit to not swallow error_log output?
Asked Answered
C

2

5

When error_log() is run from within phpunit it isn't written to the normal error log file. I'd like to stop this so that it writes to file as if I was hitting PHP via a browser.

<?php

class exampleTest extends PHPUnit_Framework_TestCase {

    public function testSomething() {
        error_log('This will not be written to the error log, but I wish it was!');
        $this->assertEquals(2, 1+1);
    }

}

I am currently using php version 5.5, phpunit version 3.7. This happens both on osx and ubunutu. This does not happen on Windows 7.

Consumedly answered 8/8, 2014 at 0:31 Comment(4)
Judging by what the manual says, if you don't pass any parameters other than the message into error_log(), it'll save it to the PHP system logger, probably ends up in the tmp folder of your PHP installation. If you want to save it somewhere else you can specify a path like error_log('Your message', 3, '/path/to/log/file');Stover
with log_errors = On and error_log = "/var/log/php/php_errors.log" in my php.ini, error_log() writes to that file. However in PHP unit it doesn't. I don't want to have to adjust all of our error_log calls just to work around phpunit.Consumedly
The above test works for me on Ubuntu 13.10 running PHP 5.5.3 and PHPUnit 4.2.0. You may want to upgrade PHPUnit. It does output buffering in some cases (which has changed over the years), but I don't see how that could interfere with writing to the log file. Also, var_dump (or assert) both log_errors and error_log in the test to make sure they are what you expect.Thump
Tried using phpunit 4.2 above test will write to console but not to the error log file.Consumedly
P
6

So this turned out to be a symptom of php run from command line not logging errors (discussed here) and was solved by ensuring that the user executing phpunit has write permissions to the error log.

Also at play was the fact when invoked from the command line php used a different ini file (discussed here).

Parotitis answered 8/8, 2014 at 0:32 Comment(0)
S
1

When executed by PHPUnit, the error_log() function writes to the PHP STDERR rather than the config of the project itself. This is resulted by an error handler defined by PHPUnit. This default behavior can be altered using a custom error handler.

Sears answered 10/10, 2018 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.