在 phpunit 测试中抑制 symfony 4 响应体
Suppress symfony 4 response body in phpunit test
我正在将 Silex 应用程序迁移到 Symfony Flex,到目前为止一切正常,除了当我 运行 phpunit 测试时我将响应主体输出到 phpunit 输出中。
即。
> bin/phpunit
#!/usr/bin/env php
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.
Testing unit
.......<http://data.nobelprize.org/resource/laureate/914> a <http://data.nobelprize.org/terms/Laureate> , <http://xmlns.com/foaf/0.1/Person> ;
<http://www.w3.org/2000/01/rdf-schema#label> "Malala Yousafzai" ;
<http://data.nobelprize.org/terms/laureateAward> <http://data.nobelprize.org/resource/laureateaward/974> ;
<http://data.nobelprize.org/terms/nobelPrize> <http://data.nobelprize.org/resource/nobelprize/Peace/2014> ;
然后是整个 RDF 文档
. 8 / 8 (100%)
Time: 1.07 seconds, Memory: 14.00MB
OK (8 tests, 71 assertions)
Generating code coverage report in Clover XML format ... done
所以它工作正常,但我不知道如何禁用此输出?
要求很简单
$this->client->request('GET', "/nobel_914.ttl", [], [], ['HTTP_ACCEPT' => $request_mime]);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(), "GET should be allowed.");
$response = $this->client->getResponse();
$charset = $response->getCharset();
等等
并且客户端设置在这样的基础 class 中
class MyAppTestBase extends WebTestCase
{
/**
* @var \Symfony\Component\BrowserKit\Client
*/
protected $client;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->client = static::createClient();
$this->client->catchExceptions(false);
}
我确定我遗漏了一些明显的东西,但这对我来说是新的。我 运行 在 'test' 环境中使用 'debug' == false。
感谢任何帮助。
所以这可能一直是个问题,但在从 Silex 到 Symfony Flex 的转换中才开始暴露出来。
我们通过
流式传输回复
$filename = $this->path;
$stream = function () use ($filename) {
readfile($filename);
};
return new StreamedResponse($stream, 200, $res->headers->all());
和 readfile
将内容扔到输出缓冲区。将 readfile
切换为 file_get_contents
解决了这个问题
$filename = $this->path;
$stream = function () use ($filename) {
file_get_contents($filename);
};
return new StreamedResponse($stream, 200, $res->headers->all());
我正在将 Silex 应用程序迁移到 Symfony Flex,到目前为止一切正常,除了当我 运行 phpunit 测试时我将响应主体输出到 phpunit 输出中。
即。
> bin/phpunit
#!/usr/bin/env php
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.
Testing unit
.......<http://data.nobelprize.org/resource/laureate/914> a <http://data.nobelprize.org/terms/Laureate> , <http://xmlns.com/foaf/0.1/Person> ;
<http://www.w3.org/2000/01/rdf-schema#label> "Malala Yousafzai" ;
<http://data.nobelprize.org/terms/laureateAward> <http://data.nobelprize.org/resource/laureateaward/974> ;
<http://data.nobelprize.org/terms/nobelPrize> <http://data.nobelprize.org/resource/nobelprize/Peace/2014> ;
然后是整个 RDF 文档
. 8 / 8 (100%)
Time: 1.07 seconds, Memory: 14.00MB
OK (8 tests, 71 assertions)
Generating code coverage report in Clover XML format ... done
所以它工作正常,但我不知道如何禁用此输出?
要求很简单
$this->client->request('GET', "/nobel_914.ttl", [], [], ['HTTP_ACCEPT' => $request_mime]);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(), "GET should be allowed.");
$response = $this->client->getResponse();
$charset = $response->getCharset();
等等
并且客户端设置在这样的基础 class 中
class MyAppTestBase extends WebTestCase
{
/**
* @var \Symfony\Component\BrowserKit\Client
*/
protected $client;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->client = static::createClient();
$this->client->catchExceptions(false);
}
我确定我遗漏了一些明显的东西,但这对我来说是新的。我 运行 在 'test' 环境中使用 'debug' == false。
感谢任何帮助。
所以这可能一直是个问题,但在从 Silex 到 Symfony Flex 的转换中才开始暴露出来。
我们通过
流式传输回复$filename = $this->path;
$stream = function () use ($filename) {
readfile($filename);
};
return new StreamedResponse($stream, 200, $res->headers->all());
和 readfile
将内容扔到输出缓冲区。将 readfile
切换为 file_get_contents
解决了这个问题
$filename = $this->path;
$stream = function () use ($filename) {
file_get_contents($filename);
};
return new StreamedResponse($stream, 200, $res->headers->all());