3

I'm trying to write PHPUnit tests for a class that writes to error_log but seem to be having trouble with PHPUnit's output buffering.

Because of the potential volatility of error_log given an unknown SUT php.ini configuration, I ultimately decided upon writing to stdout after having limited success with overloading via namespace.

Unfortunately, there appears to be some quirk in how PHPUnit is buffering output in that calling expectOutputString returns an empty string as "Actual" rather than what was written to stdout.

Here's a quick-and-dirty test class I wrote to eliminate anything that might be giving me unintended grief ("ObTest.php"):

<?php
namespace Some\Test;

class ObTest
{
    public function writeToErrorLog($message)
    {
        error_log($message);
    }
}

And here's the test I wrote:

<?php

namespace Some\Test;

include "ObTest.php";

class ObTestTest extends \PHPUnit_Framework_TestCase
{
    public static $error_log_config;

    public static function setUpBeforeClass()
    {
        self::$error_log_config = ini_get("error_log");

        ini_set("error_log", "/dev/stdout");
    }

    public static function tearDownAfterClass()
    {
        ini_set("error_log", self::$error_log_config);
    }

    public function testWriteToErrorLogOutputs()
    {
        $ob_test = new ObTest();

        $this->expectOutputString("Test");

        $ob_test->writeToErrorLog("Test");
    }
}

And finally the output:

PHPUnit 5.4-g9b5b99b by Sebastian Bergmann and contributors.

[06-May-2016 01:05:51 UTC] Test

Failed asserting that two strings are equal.
Expected :'Test'
Actual   :''

Note that I do realize that the test will fail anyway due to the timestamp, but writing a test with expectOutputRegex is unnecessary to demonstrate the blank string being returned as "Actual"; I can confirm that I get the same result using the regex option.

I'm running PHP 5.6.20-1+deb.sury.org~trusty+1 if it's relevant.

Update 2016-05-22

Per the comments, I have tried several variants of setting error_log to write to output, including the STDOUT constant and php://output stream, all with no success.

For anyone interested or encountering a similar issue, I also have since logged an issue on GitHub thinking that this may be some sort of bug, but it hasn't yet received any attention as of this update. The issue notes that I've also tried this on the latest stable PHPUnit version (5.3.2) without success.

3
  • Try using the STDOUT constant insead of /dev/stdout. You also may try php://stdout and php://output Commented May 22, 2016 at 9:45
  • Thanks, @MaximKrizhanovsky. I have tried those alternatives with no success. I'll update the question to make that clear. Commented May 22, 2016 at 14:41
  • Just want to say I'm having this same issue, trying to follow the instructions from that previous question. Commented May 11, 2022 at 21:58

0

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.