简体   繁体   中英

Unit testing in laravel using filesystems

I'm doing unit testing using the filesystems config. I want to map my file system disk to a testing directory where I will put my files(JSON, Excel, txt) to be used by the application for testing purposes. I don't want to use fake directories as I need to put the file in the 'import' location.

filesystems.php:

'disks' => [
    'import' => [
        'driver' => 'local',
        'root' => '/import/clientname',
        'visibility' => 'private'
    ],
];

In the application, here is the function which i want to test:

public function importFile(){
    $filesystem = Storage::disk('import');
    
    ...
}

The solution I was thinking is to use this location for my testing:

在此处输入图像描述

Is there a way to implement this solution?

Yes, you can, for example, upload file with each of your disks, then assert if file exist in that directory.

Example of unit test for upload image with form:

public function testStorage()
{
    $file = UploadedFile::fake()->image('File10.png');
    $response = $this->post(route("save.image"), [
        'file' => $file,
    ]);
    $response
        ->assertStatus(200)
        ->assertSessionHasNoErrors();
    Storage::disk('local')->assertExists("/images/" . $file->name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM