6

I'm using PHPUnit to test a function which downloads a file. I want to test that the correct file is downloaded and so my idea was to check the output of the function. I'm trying to use output buffering:

ob_start();
$viewer->downloadById($fileId);
$output = ob_get_flush();
$this->assertEquals($expectedFileContents,$output);

The test passes/fails when it should, which is good. My issue is that the contents of the output buffer is also printed to the console. How do I hide this?

1 Answer 1

11

Use ob_get_clean() instead of ob_get_flush(). The former will remove the buffer without printing it and return its contents. The latter will do the same and print the contents of the buffer.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I had just worked that one out for myself, and was coming back here to post the answer!
@user1578653, are downloading the file from an external website, executing HTTP request? (absolutely irrelevant to the issue in your question, just out of curiosity)
nope, in $viewer->downloadById() I am retrieving a BLOB from an Oracle database (which could be remote) and printing out the contents (after sending headers).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.