Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fake on demand storage disk in Laravel 8

Part of my code creates an on-demand storage disk using config values that are stored in the database. I am writing a feature test for this code, how do I get my test to use fake storage when this disk is created instead of trying to actually create the real connection? With any named storage disks I just do Storage::fake('mydisk') at the start of my test but since it is on-demand it doesn't have a name.

like image 617
geoffs3310 Avatar asked Oct 25 '25 04:10

geoffs3310


1 Answers

Managed to crack it in the end:

$localDisk = Storage::fake('local');

Storage::shouldReceive('build')->with([
    'driver' => 'sftp',
    'host' => $endpoint->host,
    'username' => $endpoint->username,
    'password' => $endpoint->password,
    'port' => $endpoint->port ?? 22,
])->andReturn($localDisk);
like image 92
geoffs3310 Avatar answered Oct 26 '25 19:10

geoffs3310