使用symfony2,如何测试(功能测试)文件已经在客户端下载得很好?我尝试了下面的代码,但是出了点问题。
我的控制器:
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;
}
}
测试代码:
public function testupload()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));
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_to _myFile/file.png");
//curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
//$json_response = json_decode($response);
print "resp -> ".$response->headers;
}
最后,phpunit的回复:
1)Application \ MediaBundle \ Tests \ Controller \ PictureControllerCopyTest :: testupload
试图获得非对象的属性/var/www/symfony/ws1/src/Application/MediaBundle/Tests/Controller/PictureControllerCopyTest.php:28
FAILURES! 测试:1,断言:0,错误:1。
答案 0 :(得分:0)
编写功能测试而不是使用curl。优点是您不需要Web服务器来运行测试,因为内核将直接调用(它比通过Web服务器更快)。
阅读文档中的功能测试:http://symfony.com/doc/current/book/testing.html#functional-tests