Jas*_*nis 19 php phpunit laravel
我正在尝试在我的laravel控制器上运行此功能测试.我想测试图像处理,但这样做我想伪造图像上传.我该怎么做呢?我在网上找到了一些例子,但似乎没有一个适合我.这就是我所拥有的:
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);
}
Run Code Online (Sandbox Code Playgroud)
但是控制器没有通过这个检查:
elseif (!Input::hasFile('file'))
{
return Response::error('No file uploaded');
}
Run Code Online (Sandbox Code Playgroud)
所以文件不能正确传递.我该怎么做?
更新
基于max.lanin的建议,我也尝试过:
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);
}
Run Code Online (Sandbox Code Playgroud)
但没有成功.使用的文件存在且大小正确.
Ale*_*lex 11
CrawlerTrait.html#method_action的文档内容如下:
参数
string $ method
string $ action
array $ wildcards
array $ parameters
array $ cookies
array $ files
array $ server
string $ content
所以我假设正确的呼叫应该是
$response = $this->action(
'POST',
'FileStorageController@store',
[],
$values,
[],
['file' => $uploadedFile]
);
Run Code Online (Sandbox Code Playgroud)
除非它需要非空的通配符和cookie.
作为旁注,它绝不是单元测试.
Ina*_*man 10
最好最简单的方法:首先导入必要的东西
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
Run Code Online (Sandbox Code Playgroud)
然后制作一个假文件来上传。
Storage::fake('local');
$file = UploadedFile::fake()->create('file.pdf');
Run Code Online (Sandbox Code Playgroud)
然后制作一个 JSON 数据来传递文件。例子
$parameters =[
'institute'=>'Allen Peter Institute',
'total_marks'=>'100',
'aggregate_marks'=>'78',
'percentage'=>'78',
'year'=>'2002',
'qualification_document'=>$file,
];
Run Code Online (Sandbox Code Playgroud)
然后将数据发送到您的 API。
$user = User::where('email','candidate@fakemail.com')->first();
$response = $this->json('post', 'api/user', $parameters, $this->headers($user));
$response->assertStatus(200);
Run Code Online (Sandbox Code Playgroud)
我希望它会起作用。
对于任何绊倒这个问题的人,你现在可以这样做:
$response = $this->postJson('/product-import', [
'file' => new \Illuminate\Http\UploadedFile(resource_path('test-files/large-avatar.jpg'), 'large-avatar.jpg', null, null, null, true),
]);
Run Code Online (Sandbox Code Playgroud)
使用phpunit,您可以使用attach()方法将文件附加到表单.
流明文档示例:
public function testPhotoCanBeUploaded()
{
$this->visit('/upload')
->name('File Name', 'name')
->attach($absolutePathToFile, 'photo')
->press('Upload')
->see('Upload Successful!');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15496 次 |
| 最近记录: |