-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moving testing UploadedFile with Symphony breaks #26783
Comments
This is because As these ways differ from each other I don't think that these two methods should match each others behavior exactly. |
I disagree completely. If your production implementation needs to change because the testing environment doesn't quite match then there's a discrepancy there that needs to be addressed. Simply suggesting to use the |
It really frustrates me that issues like this are closed without much thought or consideration 😒 |
Using the |
The methods on the storage class will copy the file your quite right, but Symphony's At the moment, I would say it's perfectly acceptable to move the temporary file to the same location as used in |
Ah I think I see where you're getting at. I think this falls a little out of scope why the fake framework/src/Illuminate/Http/Testing/FileFactory.php Lines 9 to 21 in 8da5b7d
|
So you can't do integration testing with fake file uploads that get moved? Here's an example of what our production code looks like (simplified example): namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UserRegisterTest extends TestCase
{
use DatabaseTransactions;
public function test_can_register_user()
{
$response = $this
->post(route('users.store'), [
'email' => 'jane@blahh.com',
'picture' => UploadedFile::fake()->image('jane.jpg', 400, 400),
]);
$response->assertSessionHasNoErrors();
$user = User::first();
$this->assertNotNull($user);
$this->assertEquals('jane@blah.com', $user->email);
$this->assertTrue(file_exists(storage_path('user-pictures/' . $user->picture)));
}
}
class UserController extends Controller {
public function store()
{
$user = User::create([
'email' => request('email'),
'picture' => Str::random(10) . '.jpg',
]);
// This works perfectly fine in production code.
// But unit testing with a fake uploaded file won't work because `->move()`
// is trying to move a tmpfile() which won't work.
request()->file('picture')->move(storage_path('user-pictures/' . $user->picture));
return redirect()->route('users.show', $user);
}
}
This clearly isn't true because it does work when you use |
The problem is that files created with |
Sorry to resurrect such an old thread, but how did you "resolve" this for your own use-case @garygreen ? I have the exact same scenario here and am looking for a workable solution. Interestingly enough, I don't believe this was ever a problem when testing on GNU/Linux; it only became a problem when testing on Windows. |
@cbj4074 honestly I didn't really look that much further into it so I basically gave up unit testing in regards to file uploads. 😆 From what I gather, if you want to use Laravel's If you do manage to solve it then let me know as I'm still looking for a solution that doesn't require using |
@garygreen Thank you for the quick reply! I appreciate it tremendously. Like you, I don't have the time or patience to wrestle with it, so I'm just using a real file from the filesystem in the test: $testFile = base_path('tests/assets/test.jpg');
$file = new UploadedFile($testFile, 'test.jpg', 'image/png', filesize($testFile), null, true); The test passes and I can move on with life. Of course, this "solution" is in no way ideal because it crosses I/O boundaries and requires the test file to be under version control (it's a tiny JPEG file). In the end, I'm not sure which is worse: using |
Description:
When you use
$file->move($path)
on aIlluminate\Http\Testing\File
it will fail because the file is locked. This can cause implementation difficulty if you use->move
in your application instead ofStorage::
Steps To Reproduce:
This is because under the hood Laravel uses
tmpfile
to generate a temporary file which is locked and cannot be moved. However if you use theStorage
method, it will work:This seems a bit of a discrepancy to me. If the testing uploaded file is extending Symphony, all methods on it should still work, really?
The text was updated successfully, but these errors were encountered: