Skip to content

expectOutputString and error_log #2155

@ddehart

Description

@ddehart

I've been banging my head against tests for a class involving error_log for a bit, and I'm starting to wonder if I'm running into some sort of output buffer bug in PHPUnit. PHPUnit appears not to be capturing error_log output as I would expect. Here's a quick-and-dirty test I wrote.

ObTest.php:

<?php
namespace Some\Test;

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

The corresponding test:

<?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 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   :''

I've also tried setting error_log to php://output thinking that the issue had something to do with stdout not getting captured in PHPUnit's output buffering at all; PHPUnit still returns the empty string as "Actual", which is ultimately what led me to believe this might be a bug.

I get the same result with PHPUnit 5.3.2, and in general I'm running PHP 5.6.20-1+deb.sury.org~trusty+1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions