PHPUnit Test for error_log writing to stdout
<p>I'm trying to write PHPUnit tests for a class that writes to <code>error_log</code> but seem to be having trouble with PHPUnit's output buffering.</p>

<p>Because of the potential volatility of <code>error_log</code> given an unknown SUT php.ini configuration, I ultimately decided upon <a href="https://stackoverflow.com/questions/21784240/is-there-any-way-to-expect-output-to-error-log-in-phpunit-tests">writing to stdout</a> after having limited success with <a href="https://stackoverflow.com/questions/31637068/testing-error-log-with-phpunit">overloading via namespace</a>.</p>

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

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

<pre><code>&lt;?php
namespace Some\Test;

class ObTest
{
    public function writeToErrorLog($message)
    {
        error_log($message);
    }
}
</code></pre>

<p>And here's the test I wrote:</p>

<pre><code>&lt;?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-&gt;expectOutputString("Test");

        $ob_test-&gt;writeToErrorLog("Test");
    }
}
</code></pre>

<p>And finally the output:</p>

<pre>
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   :''
</pre>

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

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

<h2>Update 2016-05-22</h2>

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

<p>For anyone interested or encountering a similar issue, I also have since logged an <a href="https://github.com/sebastianbergmann/phpunit/issues/2155" rel="nofollow noreferrer">issue on GitHub</a> 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.</p>