Update 2016/04/26 11:30 GMT+2 Workaround
从Laravel 5.2.15开始,$test参数被删除,但是没有明确的原因,因为symfony的UploadedFile仍然有$test参数.
A workaround is to use Laravel 5.2.14 provisionally.
Update 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,但标记的答案对我不适用.
Test Case
我创建了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();
}
}
Controller
namespace App\Http\Controllers;
class FileController extends Controller
{
public function upload(Request $request)
{
var_dump($request->file('file')); // $test = false
return [];
}
}
Problem
- 要发布的文件的参数为
true
表示$test
- The posted file arrives in
upload()
-
$request->file('file')
包含正确的参数,但是$test is false
It seems the argument $test is not past by the post call. Is this a bug?