diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 865bb93d..d8e76e6f 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -46,6 +46,8 @@ class TenancyServiceProvider extends ServiceProvider ])->send(function (Events\DeletingTenant $event) { return $event->tenant; })->shouldBeQueued(false), + + // Listeners\DeleteTenantStorage::class, ], Events\TenantDeleted::class => [ JobPipeline::make([ diff --git a/src/Listeners/DeleteTenantStorage.php b/src/Listeners/DeleteTenantStorage.php new file mode 100644 index 00000000..ce1a4203 --- /dev/null +++ b/src/Listeners/DeleteTenantStorage.php @@ -0,0 +1,16 @@ +tenant->run(fn () => storage_path())); + } +} diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 96afbc83..ada6b964 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; use Stancl\JobPipeline\JobPipeline; +use Illuminate\Support\Facades\File; use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Event; @@ -14,6 +14,7 @@ use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Events\TenantCreated; +use Illuminate\Filesystem\FilesystemAdapter; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; @@ -21,6 +22,8 @@ use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; +use Stancl\Tenancy\Events\DeletingTenant; +use Stancl\Tenancy\Listeners\DeleteTenantStorage; beforeEach(function () { $this->mockConsoleOutput = false; @@ -184,6 +187,29 @@ test('filesystem data is separated', function () { expect($new_storage_path)->toEqual($expected_storage_path); }); +test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function () { + config([ + 'tenancy.bootstrappers' => [ + FilesystemTenancyBootstrapper::class, + ], + ]); + + Event::listen(DeletingTenant::class, DeleteTenantStorage::class); + + tenancy()->initialize(Tenant::create()); + $tenantStoragePath = storage_path(); + + Storage::fake('test'); + + expect(File::isDirectory($tenantStoragePath))->toBeTrue(); + + Storage::put('test.txt', 'testing file'); + + tenant()->delete(); + + expect(File::isDirectory($tenantStoragePath))->toBeFalse(); +}); + function getDiskPrefix(string $disk): string { /** @var FilesystemAdapter $disk */