Stack Exchange
phpunit - Help needed about risky tests
i'm implementing some tests for a site.
In a particular test, this result occurred:
{
"event": "test",
"suite": "Example_V_test",
"test": "Example_V_test::test_3",
"status": "error",
"time": 13.469105958939,
"trace": [
    {
        "file": "\/opt\/lampp\/htdocs\/buy\/application\/tests\/phpunit.phar",
        "line": 569,
        "function": "main",
        "class": "PHPUnit_TextUI_Command",
        "type": "::"
    }
],
"message": "Risky Test: Test code or tested code did not (only) close its own output buffers",
"output": ""
}R                                                                 3 / 3 (100%)

Time: 25.76 seconds, Memory: 59.25MB

There was 1 risky test:

1) Example_V_test::test_3
Test code or tested code did not (only) close its own output buffers

/opt/lampp/htdocs/buy/application/tests/phpunit.phar:569

OK, but incomplete, skipped, or risky tests!

My question is this: how to find the line of code that cause this 'issue'?

You can find it in error log.

Where can I find the error log? I'm testing a site built with codeigniter and i didn't find it in the directory that contains the phpunit.phar file, i.e.:/opt/lampp/htdocs/codeigniter/application/tests . Also, i look in the codeigniter/application/log folder and here it's no output from phpunit testing.

/var/log/apache2/error_log. Try it bro. xD

I understand. In my case it's the /opt/lampp/logs/error.log file, but there is no output from phpunit test...

The message is reported if PHPUnit detects that the output buffering level at the end of a test method is different from the level at the beginning. This is due to the fact that it uses its own output buffer and checks that the test produces no output.
Because there is no option in PHPUnit to ignore this, you need to find the reason why the output buffering in your code was started but not ended (or ended too much) and fix that.
That might be really difficult to achieve because there is no hint, where output buffering is started, but you could use a full-text search on your source code (and libs) to identify candidates. Then look for Exceptions, which might break the normal program flow and so prevent ob_end_flush() (or similar) to be called.

I checked ob_get_level() value at the end of tested code and it returns 2. Now i will investigate on this. Thank you for your answer!

Works great now, thank you! I had an abort/die inside the buffer, which causes the error in the test script.
Before:
ob_start(); abort(404); ob_get_clean();
Solution:
ob_start(); ob_end_flush(); abort(404); ob_get_clean();

Specifically to Test code or tested code did not (only) close its own output buffers
Recently ran into this.
Originally this SO question, and the answer was a solution for me. However, I didnt like the solution. There was a deeper reason as to why this was happening, and only on one test out of hundreds I have written...
After doing a little bit more digging, I was able to locate my issue stemmed from a @section() and @endsection call within my blade template.
Some how, through mistake the keyboard was struck and a random key was left there. This didn't cause issues visually, or any red flags to the end user when page rendered so it went unnoticed.
In my case -- My error was
@section('sudo')
...
@endsection2

removing the 2 fixed the original issue and allowed me to remove the hacky solution for
//        // start hacky
//            ob_start();
//            ob_end_flush();
//            ob_get_clean();
//        // end hacky

ps. this may only fix a percentage of peoples issues that trip across this SO question. Sorry if it doesnt solve your issue :)

I literally never would have found this if you hadn't of said, so thanks for this.

Thanks, because  of this i checked my blade file and found this out.
Somehow i had 2 @section tags for the same section
@section('before_content')

@section('before_content')
  ...
@endsection


Mine is fixed by using:
ob_end_flush();
ob_get_clean();

At end of tests.

I ran into this error also:
Test code or tested code did not (only) close its own output buffers

It's useful to use ob_get_level() to determine the output buffer level at the start and end of the affected test. I put breakpoints in the test then used the console in PHPStorm to check this.
In my case, I was testing a method that opened a Symfony\Component\HttpFoundation\StreamedResponse. The level was 1 at the start of the test, but 0 at the end - so for me the solution was to add ob_start() at the end of the test.

Were you able to test the 206 partial response as well?
