使用symfony2测试下载文件 [英] Testing a download file with symfony2
本文介绍了使用symfony2测试下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
能否请您帮助我如何在symfony2中测试下载文件? 有我的源代码:
Can you please help me on how to test a download file in symfony2? There are my source code :
Controller.php
Controller.php
public function downloadAction($picture_id)
{
$fichier= $this->uploadDir.$picture_id;
if (($fichier != "") && (file_exists($fichier))) {
$content = file_get_contents($fichier);
$response = new Response();
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment;filename="'.$picture_id);
$response->setContent($content);
return $response;
}
else return new Response(json_encode(array("response_code" => "ko")));
}
ControllerTest.php
ControllerTest.php
public function testdownload()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, "path/img.jpg");
$response = curl_exec($ch);
}
问题在于我如何测试下载是否成功,或者可以使用哪个断言进行功能测试?
The issue is that how can I test that the download has been successful,or which assertion could I use to do my functionnal test?
推荐答案
5年前提出,但是也许有人会发现这个有用(功能测试):
Asked 5 years ago, but maybe someone will find this useful (functional test):
$client = static::createClient();
$client->request('GET', '/path/img.jpg');
$this->assertEquals(true, $client->getResponse()->isOk());
$binaryData = $client->getInternalResponse()->getContent()
问题在于,当有二进制数据时,$client->getResponse()->getContent()
返回空字符串.
The problem is that $client->getResponse()->getContent()
returns empty string when there's binary data.
经过Symfony 4.2测试.
Tested with Symfony 4.2.
这篇关于使用symfony2测试下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文