【发布时间】:2016-08-19 20:39:48
【问题描述】:
更新 2016/04/26 11:30 GMT+2 解决方法
从 Laravel 5.2.15 开始,去掉了 $test 参数,但是没有明确的原因,因为 Symfony 的 UploadedFile 仍然有 $test 参数。
一种解决方法是临时使用 Laravel 5.2.14。
2016/04/26 11:00 GMT+2 更新
Laravel 自己的 UploadedFile 没有通过 $test 参数。查看这些资源:
- https://github.com/laravel/framework/issues/12620
- https://github.com/laravel/framework/commit/5062a9b42632e55ee90b7397141c0b12622447e1
我知道,还有一个问题:How to test file upload in Laravel 5.2,但标记的答案对我不起作用。
测试用例
我创建了一个 Symfony 的 UploadedFile 类的实例,并将 $test 设置为 true。我将文件发布到file/upload。
class FileControllerTest extends TestCase
{
use \Illuminate\Foundation\Testing\DatabaseTransactions;
private $file;
public function setUp()
{
parent::setUp();
$this->file = new Symfony\Component\HttpFoundation\File\UploadedFile(
public_path() . '/examples/example.jpg',
'example.jpg',
'image/jpeg',
filesize(public_path() . '/examples/example.jpg'),
null,
true // for $test
);
}
/** @test */
public function it_uploads_a_valid_file()
{
var_dump($this->file); // $test = true
$this->call('POST', 'file/upload', [], [], ['file' => $this->file],
['accept' => 'application/json']);
$this->assertResponseOk();
}
}
控制器
namespace App\Http\Controllers;
class FileController extends Controller
{
public function upload(Request $request)
{
var_dump($request->file('file')); // $test = false
return [];
}
}
问题
- 要发布的文件有
true的参数$test - 发布的文件到达
upload() -
$request->file('file')包含正确的参数,但是$test 是 false
似乎参数 $test 没有被 post 调用过去。这是一个错误吗?
【问题讨论】:
标签: laravel file-upload laravel-5 phpunit laravel-5.2