Use Laravel UploadedFile class for requests#3417
Use Laravel UploadedFile class for requests#3417torkiljohnsen wants to merge 1 commit intoCodeception:2.2from
Conversation
Using Symfony’s UploadedFile has the effect that when Laravel’s Request class does convertUploadedFiles, all files are converted to Laravel’s UploadedFile class, but with the test flag set to false. This, in turn, makes it difficult to use the class in a test, because the class then must pass the is_uploaded_file test in UploadedFile::isValid().
|
Can you show me some pseudo code of a test that fails due to this problem? That way I can play around with it a bit. I think this PR will not cause any problems, but I'd like to look into it myself a bit better before I merge it. Tests for this sort of problem should be added to the Laravel 5 sample application, but I can do that myself. |
|
Sure. Here is a CEST method and the corresponding controller endpoint. This uses the REST module too obviously. // CEST test
public function createMedia(ApiTester $i)
{
$i->wantTo('create media');
$i->sendPOST('media', [], ['file' => codecept_data_dir('cat1.jpg')]);
$i->seeRecord('media', [
'path' => public_path('images/cat1.jpg')
]);
}
// Controller method
public function store(Request $request)
{
$uploadedFile = $request->file('file');
$file = $uploadedFile->move(public_path('images'), $uploadedFile->getClientOriginalName());
Media::create(['path' => $file->getRealPath()]);
return response(null, 201);
}Without my patch, this will fail, because |
|
To elaborate: When the POST request is made, Laravel converts files to its native |
|
When an instance of Laravel's |
|
I created a test in the Laravel 5 sample application that reproduces your issue. The fix you propose is also correct, only you forgot to add the import for the After the PR is merged I will push the test to the Laravel 5 sample application, because it will break the build if I do that right away. Thanks for the help! |
|
Ouch. Good call on the missing import and thanks for helping out :) |
Using Symfony’s
UploadedFileclass has the effect that when Laravel’sRequestclass doesconvertUploadedFiles(), all files are converted to Laravel’sUploadedFileclass, but with the$testset to false.This, in turn, makes it difficult to use the class in a test, because the class then must pass the
is_uploaded_file()test inUploadedFile::isValid().This is just a proposal, I have no tests to go with the code, as this repo is missing some basic requirements for this class to even be instantiable, like Laravel's Eloquent and Symfony's HttpFoundation component. Please have a look @janhenkgerritsen.