6

I have a code to test:

class ToTest
{
    public function testMe()
    {
        echo 'test';
    }
}

class TestTest extends \PHPUnit\Framework\TestCase
{
    public function testX()
    {
        ob_start();
        (new ToTest())->testMe();
        $c = ob_get_clean();

        $this->assertSame('test', $c);
    }
}

this passes but marked as risky test:

Test code or tested code did not (only) close its own output buffers

to me, it looks I handled output buffering well...

John Smith
  • 6,129
  • 12
  • 68
  • 123

1 Answers1

4

There must be some code under test interfering with the buffer. For example, if the code under test opens a buffer and doesn't close it for some reason, then you will end up with an open buffer.

The following will cause the a risky test:

class ToTest
{
    public function testMe(): void
    {
        ob_start(); // buffer is never closed
        echo 'test';
    }
}

final class FizzTest extends TestCase
{
    public function testObjectBuffer(): void
    {
        ob_start();
        (new ToTest())->testMe();
        $c = ob_get_clean(); 

        $this->assertSame('test', $c);
    }
}

The first thing to do is use the PHPUnit output testing assertions:

    public function testObjectBuffer(): void
    {
        $this->expectOutputString('test');

        (new ToTest())->testMe();
    }

Once you do that, you will know for sure that the code under test is opening a buffer somewhere and not closing it.

Gerard Roche
  • 6,162
  • 4
  • 43
  • 69