symfony - Download file functional test on Symfony2 -
with symfony2, how test (functional test) file has been downloaded on client's side? have tried following code wrong.
my controller:
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; } }
test code:
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; }
finally, response phpunit:
1) application\mediabundle\tests\controller\picturecontrollercopytest::testupload
trying property of non-object/var/www/symfony/ws1/src/application/mediabundle/tests/controller/picturecontrollercopytest.php:28
failures! tests: 1, assertions: 0, errors: 1.
write functional test instead of using curl. advantage won't need web server run test, since kernel called directly (it's faster going through web server).
read on functional tests in docs: http://symfony.com/doc/current/book/testing.html#functional-tests
Comments
Post a Comment