@pazitron Within your controller, you have it saving to /images/blog_posts on your public disk, but in your test, you have it asserting against the root path of your local disk.
Nov 26, 2020
8
Level 18
Problem with UploadedFile::fake() and PHPUnit test - Unable to find a file at path
I keep getting "Unable to find a file at path [photoA.jpg]. Failed asserting that false is true." when trying to test image upload.
my controller store method:
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required | max:100',
'description' => 'required',
'image' => 'required | image | max:1000',
'excerpt' => 'required | max:200'
]);
$post = Post::create([
'title' => request('title'),
'description' => request('description'),
'image' => $request->file('image')->store('images/blog_posts', 'public'),
'excerpt' => request('excerpt')
]);
return redirect()->route('manage.posts.show', $post)
->with('success', 'Your post has been successfully created!');
}
my test:
public function it_can_upload_post_image()
{
Storage::fake('local');
$manager = factory(User::class)
->states('manager')
->create();
$this->actingAs($manager)->post(route('manage.posts.store'), [
'title' => 'Post title A',
'slug' => 'post-title-a',
'image' => UploadedFile::fake()->image('photoA.jpg'),
'excerpt' => 'Excerpt for post A',
'description' => 'Description for post A'
]);
Storage::disk('local')->assertExists('photoA.jpg');
}
what am I missing? Thank you in advance!
Level 51
@pazitron You don't need to chain it there.. the test is only making the fake file to include within the POST request. You only need to update your controller with storeAs() to be specific about the name. I've never used just store(), I've always used storeAs() so I can sanitize the filename. You can get the filename within your controller using request()->file('image')->getClientOriginalName() then do whatever you want with the string before passing it into the storeAs() method.
Please or to participate in this conversation.