You are doing a foreach on filename, does this mean you are handling multiple files?
In your test you are only submitting one file.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey Guys,
I'm having real issues testing my file upload functionality, I just cant seem to get it to work.
In my controller i have the following code;
if ($request->hasFile('filename')) {
foreach ($request->file('filename') as $image) {
if ($upload = $image->store('images', 'public')) {
$filename = $upload;
// more logic here
}
}
}
This works fine for uploading files on my frontend, but my test is failing. My test looks like so;
Storage::fake('public');
$this->postJson('/api/upload', [
'filename' => $image = UploadedFile::fake()->image('fakeimage.jpg')
]);
Storage::disk('public')->assertExists("images/{$image->hashName()}");
Storage::disk('public')->assertMissing('images/missing.jpg');
The error im seeing is as follows;
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function store() on string
I have no idea why this would work on the frontend, but fail on the test.
Any help would be greatly appreciated.
Here's my code and it pass and please check the status also the URL if it is correct
and you should pass the images as array
'filename' => [
$image = UploadedFile::fake()->image('fakeimage.jpg')
]
api.php
Route::post('/upload', 'TestController');
Controller
public function __invoke(Request $request)
{
if ($request->hasFile('filename')) {
foreach ($request->file('filename') as $image) {
if ($upload = $image->store('images', 'public')) {
return response()->json($upload, 201);
}else{
return response()->json("no", 403);
}
}
}
}
Test
public function testExample()
{
// to track the errors
// $this->withoutExceptionHandling();
Storage::fake('public');
$this->postJson('/api/upload', [
'filename' => [
$image = UploadedFile::fake()->image('fakeimage.jpg')
]
])
->assertStatus(201);
Storage::disk('public')->assertExists("images/".$image->hashName());
Storage::disk('public')->assertMissing('images/missing.jpg');
}
Please or to participate in this conversation.