tenancy icon indicating copy to clipboard operation
tenancy copied to clipboard

Calling Storage::fake() doesn't remove tenant directories as one would expect inline with Laravel Docs

Open devonmather opened this issue 6 years ago • 5 comments

In a test I have a function calling Storage::fake(). Calling this function makes a tenant directory after the test is run I still have the tenant folders, after running a lot of tests this can clutter things quite a bit and can be annoying to manually delete them.

Steps to reproduce

/** @test */
public function an_example_test()
{
    tenancy()->init('test.localhost');
    Storage::fake('test');
}

Expected behavior

As per laravel documentation relating to mocking Storage the following is stated;

"By default, the fake method will delete all files in its temporary directory."

If this isn't possible it would be nice to at least have a setting in the config for; 'delete_storage_after_tenant_deletion' => false, // delete the tenant's storage after deleting the tenant

Your setup

  • Laravel version: [e.g. 6.2.0]
  • stancl/tenancy version: [2.1.0]
  • Storage driver: [DB]

devonmather avatar Nov 15 '19 16:11 devonmather

Hi,

Nice catch.

By default, the fake method will delete all files in its temporary directory.

If the tenant folders are empty, then this is not a bug, since the temporary data is cleared like Laravel docs say.

I'll add a delete_storage_after_tenant_deletion option in 2.3.0.

For now you could for example set tenancy.filesystem.suffix_base to test_tenant and make test_tenant* part of your storage/.gitignore.

stancl avatar Nov 15 '19 17:11 stancl

Great, thanks for adding that to the road map. In case someone has similar issues until next release they can also use something along the following in their TestCase to remove those folders.

protected function setUp(): void
{
    parent::setUp();

    config('tenancy.filesystem.suffix_base', 'tenant_test');
}

protected function tearDown(): void
{
    parent::tearDown();

    $directories = File::directories(base_path('storage'));
    foreach ($directories as $directory) {
        if (substr($directory, strrpos($directory, '/') + 1, 11) == 'tenant_test') {
            File::deleteDirectory($directory);
        }
    }
}

devonmather avatar Nov 16 '19 00:11 devonmather

I will add this to docs once I update the test case example once I work on #250.

stancl avatar Mar 15 '20 20:03 stancl

Great, thanks for adding that to the road map. In case someone has similar issues until next release they can also use something along the following in their TestCase to remove those folders.

protected function setUp(): void
{
    parent::setUp();

    config('tenancy.filesystem.suffix_base', 'tenant_test');
}

protected function tearDown(): void
{
    parent::tearDown();

    $directories = File::directories(base_path('storage'));
    foreach ($directories as $directory) {
        if (substr($directory, strrpos($directory, '/') + 1, 11) == 'tenant_test') {
            File::deleteDirectory($directory);
        }
    }
}

This causes my dentral application tests to fail because of this. For some reason I get a Target class [files] does not exist error. and it is due to the Facade File in the tearDown method.

mreduar avatar Jun 18 '22 00:06 mreduar

Great, thanks for adding that to the road map. In case someone has similar issues until next release they can also use something along the following in their TestCase to remove those folders.

protected function setUp(): void
{
    parent::setUp();

    config('tenancy.filesystem.suffix_base', 'tenant_test');
}

protected function tearDown(): void
{
    parent::tearDown();

    $directories = File::directories(base_path('storage'));
    foreach ($directories as $directory) {
        if (substr($directory, strrpos($directory, '/') + 1, 11) == 'tenant_test') {
            File::deleteDirectory($directory);
        }
    }
}

This causes my dentral application tests to fail because of this. For some reason I get a Target class [files] does not exist error. and it is due to the Facade File in the tearDown method.

Try with:

 use Illuminate\Support\Facades\File;

victorelec14 avatar Jun 24 '22 11:06 victorelec14

I made it this way:

Storage::fake('public', [
            'visibility' => 'public',
        ]);
        // this is because Tenant will override the runtime storage config with the one from the config
        config(['filesystems.disks.public' => [
            ...config('filesystems.disks.public'),
            ...Storage::disk('public')->getConfig(),
        ]]);

mshamaseen avatar Sep 26 '23 18:09 mshamaseen