使用symfony2测试下载文件
你能帮我测试一下symfony2中的下载文件吗? 以下是我的源代码: Controller.php使用symfony2测试下载文件,symfony,phpunit,Symfony,Phpunit,你能帮我测试一下symfony2中的下载文件吗? 以下是我的源代码: Controller.php public function downloadAction($picture_id) { $fichier= $this->uploadDir.$picture_id; if (($fichier != "") && (file_exists($fichier))) { $content = file_get_contents($fic
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
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);
}
问题是,我如何测试下载是否成功,或者我可以使用哪个断言来进行功能测试?5年前有人问,但也许有人会发现这很有用(功能测试): 问题是当存在二进制数据时,
$client->getResponse()->getContent()
返回空字符串
使用Symfony 4.2进行了测试。我建议您看看这个。谢谢,我已经找到了解决方案。非常感谢much@little如果你已经找到了解决方案,那你为什么不跟我们分享?
$client = static::createClient();
$client->request('GET', '/path/img.jpg');
$this->assertEquals(true, $client->getResponse()->isOk());
$binaryData = $client->getInternalResponse()->getContent()