Update 2016/04/26 11:30 GMT+2 Workaround
Since Laravel 5.2.15, the $test parameter is removed, but there is no clear reason, because Symfony's UploadedFile still has the $test parameter.
A workaround is to use Laravel 5.2.14 provisionally.
Update 2016/04/26 11:00 GMT+2
Laravel's own UploadedFile doesn't pass the $test parameter. See these resources:
- https://github.com/laravel/framework/issues/12620
- https://github.com/laravel/framework/commit/5062a9b42632e55ee90b7397141c0b12622447e1
I know, there's another question: How to test file upload in Laravel 5.2, but the marked answer doesn't work for me.
Test Case
I create an instance of Symfony's UploadedFile class and I set $test
to true
. I post the file to 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
- The file to post has the argument
true
for$test
- The posted file arrives in
upload()
$request->file('file')
contains the right arguments, but$test is false
It seems the argument $test is not past by the post call. Is this a bug?