Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use output buffering inside PHPUnit test?

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?

like image 583
user1578653 Avatar asked Feb 10 '23 15:02

user1578653


1 Answers

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.

like image 58
Havelock Avatar answered Feb 23 '23 14:02

Havelock