How to resolve: Test code or tested code did not (only) close its own output buffers ?

Card Puncher Data Processing

The problem

When running phpunit test case, you may get this warning message

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

The solution

The solution is that generally that you needs to end up your test with a output buffer level of 1.

Write at the end of your risky test, the following

fputs(STDOUT, "Buffer level".ob_get_level());

If your level is above 1, ends an clean a level with:

ob_end_clean();

Note that it means that you have put content in the buffer that you have not used.

  • If this is caused by an expected exception, this is good
  • otherwise, there is some content in your buffer level that was not used and this is not good (ie an ob_start was not closed)

The details on why it happens ?

This message is a check that comes from the function stopOutputBuffering that stops the output buffering mechanism at the end of each running test.

The condition that triggers 1) this message is:

ob_get_level() !== $this->outputBufferingLevel

This condition checks that the level of the buffer is the same than when the unit started. Generally this→outputBufferingLevel has a value of 1.

when the level at the start of an unit test is not the same at the end.

Because it means that there was an error somewhere (functional or technical) that didn't capture the data in the output buffer generated.





Discover More
Card Puncher Data Processing
How to capture the output with the php output buffer

A basic explanation of the output buffer mechanism in php



Share this page:
Follow us:
Task Runner