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 を使用する代わりに機能テストを作成します。利点は、カーネルが直接呼び出されるため、テストを実行するために Web サーバーが必要ないことです (Web サーバーを経由するよりも高速です)。

ドキュメントで機能テストを読んでください: http://symfony.com/doc/current/book/testing.html#functional-tests

于 2013-04-06T10:27:51.607 に答える