如何使用laravel和phpunit测试文件上传? [英] How to test file upload with laravel and phpunit?
本文介绍了如何使用laravel和phpunit测试文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在laravel控制器上运行此功能测试.我想测试图像处理,但是这样做是要伪造图像上传.我该怎么做呢?我在网上找到了一些示例,但似乎没有一个适合我.这就是我所拥有的:
I'm trying to run this functional test on my laravel controller. I would like to test image processing, but to do so I want to fake image uploading. How do I do this? I found a few examples online but none seem to work for me. Here's what I have:
public function testResizeMethod()
{
$this->prepareCleanDB();
$this->_createAccessableCompany();
$local_file = __DIR__ . '/test-files/large-avatar.jpg';
$uploadedFile = new Symfony\Component\HttpFoundation\File\UploadedFile(
$local_file,
'large-avatar.jpg',
'image/jpeg',
null,
null,
true
);
$values = array(
'company_id' => $this->company->id
);
$response = $this->action(
'POST',
'FileStorageController@store',
$values,
['file' => $uploadedFile]
);
$readable_response = $this->getReadableResponseObject($response);
}
但是控制器未通过此检查:
But the controller doesn't get passed this check:
elseif (!Input::hasFile('file'))
{
return Response::error('No file uploaded');
}
因此,文件无法正确传递.我该怎么办?
So somehow the file isn't passed correctly. How do I go about this?
推荐答案
查看全文