如何在 Laravel 5.2 中测试文件上传 [英] How to test file upload in Laravel 5.2
问题描述
我正在尝试测试上传 API,但每次都失败:
测试代码:
$JSONResponse = $this->call('POST', '/upload', [], [], ['照片' =>new UploadedFile(base_path('public/uploads/test') .'/34610974.jpg', '34610974.jpg')]);$this->assertResponseOk();$this->seeJsonStructure(['name']);$response = json_decode($JSONResponse);$this->assertTrue(file_exists(base_path('public/uploads') . '/' . $response['name']));
<块引用>
文件路径为/public/uploads/test/34610974.jpg
这是我在控制器中的上传代码:
$this->validate($request, ['照片' =>'保释|需要|图像|最大:1024']);$name = 'adummyname' .'.'.$request->file('photo')->getClientOriginalExtension();$request->file('photo')->move('/uploads', $name);return response()->json(['name' => $name]);
我应该如何在 Laravel 5.2 中测试文件上传?如何使用call
方法上传文件?
当你创建一个 UploadedFile
的实例时,将最后一个参数 $test
设置为 true代码>.
$file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true);^^^^
这是一个工作测试的快速示例.它期望您在 tests/stubs
文件夹中有一个存根 test.png
文件.
class UploadTest extends TestCase{公共函数 test_upload_works(){$stub = __DIR__.'/stubs/test.png';$name = str_random(8).'.png';$path = sys_get_temp_dir().'/'.$name;复制($存根,$路径);$file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true);$response = $this->call('POST', '/upload', [], [], ['photo' => $file], ['Accept' => 'application/json']);$this->assertResponseOk();$content = json_decode($response->getContent());$this->assertObjectHasAttribute('name', $content);$uploaded = 'uploads'.DIRECTORY_SEPARATOR.$content->name;$this->assertFileExists(public_path($uploaded));@unlink($上传);}}
<前>➔ phpunit tests/UploadTest.phpPHPUnit 4.8.24 由 Sebastian Bergmann 和贡献者编写..时间:2.97 秒,内存:14.00MbOK(1 个测试,3 个断言)
Im trying to test an upload API but it fails every time:
Test Code :
$JSONResponse = $this->call('POST', '/upload', [], [], [
'photo' => new UploadedFile(base_path('public/uploads/test') . '/34610974.jpg', '34610974.jpg')
]);
$this->assertResponseOk();
$this->seeJsonStructure(['name']);
$response = json_decode($JSONResponse);
$this->assertTrue(file_exists(base_path('public/uploads') . '/' . $response['name']));
file path is /public/uploads/test/34610974.jpg
Here is My Upload code in a controller :
$this->validate($request, [
'photo' => 'bail|required|image|max:1024'
]);
$name = 'adummyname' . '.' . $request->file('photo')->getClientOriginalExtension();
$request->file('photo')->move('/uploads', $name);
return response()->json(['name' => $name]);
How should I test file upload in Laravel 5.2? How to use call
method to upload a file?
When you create an instance of UploadedFile
set the last parameter $test
to true
.
$file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true);
^^^^
Here is a quick example of a working test. It expects that you have a stub test.png
file in tests/stubs
folder.
class UploadTest extends TestCase
{
public function test_upload_works()
{
$stub = __DIR__.'/stubs/test.png';
$name = str_random(8).'.png';
$path = sys_get_temp_dir().'/'.$name;
copy($stub, $path);
$file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true);
$response = $this->call('POST', '/upload', [], [], ['photo' => $file], ['Accept' => 'application/json']);
$this->assertResponseOk();
$content = json_decode($response->getContent());
$this->assertObjectHasAttribute('name', $content);
$uploaded = 'uploads'.DIRECTORY_SEPARATOR.$content->name;
$this->assertFileExists(public_path($uploaded));
@unlink($uploaded);
}
}
➔ phpunit tests/UploadTest.php PHPUnit 4.8.24 by Sebastian Bergmann and contributors. . Time: 2.97 seconds, Memory: 14.00Mb OK (1 test, 3 assertions)
这篇关于如何在 Laravel 5.2 中测试文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!