如何在PHPUnit测试中使用输出缓冲? [英] How to use output buffering inside PHPUnit test?

查看:89
本文介绍了如何在PHPUnit测试中使用输出缓冲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPUnit测试下载文件的功能.我想测试是否下载了正确的文件,所以我的想法是检查函数的输出.我正在尝试使用输出缓冲:

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?

推荐答案

使用 ob_get_clean() 而不是ob_get_flush().前者将删除缓冲区而不打印它并返回其内容.后者将执行相同的并打印缓冲区的内容.

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.

这篇关于如何在PHPUnit测试中使用输出缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆