Calling Storage::fake() doesn't remove tenant directories as one would expect inline with Laravel Docs
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]
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.
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);
}
}
}
I will add this to docs once I update the test case example once I work on #250.
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.
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 existerror. and it is due to the Facade File in thetearDownmethod.
Try with:
use Illuminate\Support\Facades\File;
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(),
]]);