How to use output buffering inside PHPUnit test?

烈酒焚心 提交于 2019-12-23 21:15:05

问题


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:


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.



来源:https://stackoverflow.com/questions/29122683/how-to-use-output-buffering-inside-phpunit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!