使用 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
失败!测试:1,断言:0,错误:1。