diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index f6fd2728..a3626225 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -47,6 +47,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 fadef050..a610fbd2 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -2,13 +2,10 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests; - -use ReflectionObject; -use ReflectionProperty; 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; @@ -18,11 +15,13 @@ use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Events\TenantDeleted; +use Stancl\Tenancy\Events\DeletingTenant; use Illuminate\Filesystem\FilesystemAdapter; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Jobs\CreateStorageSymlinks; use Stancl\Tenancy\Jobs\RemoveStorageSymlinks; use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Listeners\DeleteTenantStorage; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; @@ -191,7 +190,7 @@ test('filesystem data is separated', function () { expect($new_storage_path)->toEqual($expected_storage_path); }); -test('local storage public urls are generated correctly', function() { +test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function () { config([ 'tenancy.bootstrappers' => [ FilesystemTenancyBootstrapper::class, @@ -310,6 +309,23 @@ test('create and delete storage symlinks jobs work', function() { $this->assertDirectoryDoesNotExist(public_path("public-$tenantKey")); }); +test('local storage public urls are generated correctly', function() { + 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 */