0

使用 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。

4

1 回答 1

0

编写功能测试而不是使用 curl。优点是您不需要网络服务器来运行测试,因为内核会被直接调用(它比通过网络服务器更快)。

阅读文档中的功能测试:http: //symfony.com/doc/current/book/testing.html#functional-tests

于 2013-04-06T10:27:51.607 回答