Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Testing File Uploads with Storage Fake

Premium
3 min read

Let's talk about testing the file uploads and checking various things:

  • Whether the file upload was successful
  • Whether the file is where it belongs
  • Whether the file name is assigned successfully.

Like a few other "external" things in Laravel, file uploads need to be faked for testing.

So, you don't work with the actual file uploads to the real storage. You fake the Storage driver, then Laravel, with its testing mechanism, puts those files elsewhere in a separate temporary folder, and then you assert whether that Storage temporary fake disk contains the file.


Scenario Example

For example, you have a Controller to store a product, and one of the fields is photo. You store the photo in the products folder and save the filename as a string to the photo database field.

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
 
class ProductController extends Controller
{
// ...
 
public function store(Request $request): RedirectResponse
{
$product = Product::create($request->all());
 
if ($request->hasFile('photo')) {
$filename = $request->file('photo')->getClientOriginalName();
$request->file('photo')->storeAs('products', $filename);
$product->update(['photo' => $filename]);
}
 
return redirect()->route('products.index');
}
 
// ...
}

Test

The start of the test is...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…