1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-09-20 15:14:03 +00:00

Assert symlink jobs dispatch events

Also cover RemoveStorageSymlinksAction not doing anything if there's no existing symlink
This commit is contained in:
lukinovec 2026-09-14 16:39:33 +02:00
parent 1a3ab476b0
commit 388f253964

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\StorageSymlinkCreated;
use Stancl\Tenancy\Events\StorageSymlinkRemoved;
use Stancl\Tenancy\Events\CreatingStorageSymlink;
use Stancl\Tenancy\Events\RemovingStorageSymlink;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
@ -53,8 +57,13 @@ test('create storage symlinks action works', function (string|null $rootOverride
Storage::disk('public')->put('foo.txt', 'tenant file');
Event::fake([CreatingStorageSymlink::class, StorageSymlinkCreated::class]);
(new CreateStorageSymlinksAction)($tenant);
Event::assertDispatched(CreatingStorageSymlink::class, fn (CreatingStorageSymlink $event) => $event->tenant->is($tenant));
Event::assertDispatched(StorageSymlinkCreated::class, fn (StorageSymlinkCreated $event) => $event->tenant->is($tenant));
// The symlink exists and points to the directory the tenant's disk writes to
expect(is_link($publicPath))->toBeTrue();
expect(readlink($publicPath))->toBe(config('filesystems.disks.public.root'));
@ -114,11 +123,25 @@ test('remove storage symlinks action works', function() {
expect(is_link($publicPath = public_path("public-$tenantKey")))->toBeTrue();
expect(file_exists($publicPath))->toBeTrue();
Event::fake([RemovingStorageSymlink::class, StorageSymlinkRemoved::class]);
(new RemoveStorageSymlinksAction)($tenant);
Event::assertDispatched(RemovingStorageSymlink::class, fn (RemovingStorageSymlink $event) => $event->tenant->is($tenant));
Event::assertDispatched(StorageSymlinkRemoved::class, fn (StorageSymlinkRemoved $event) => $event->tenant->is($tenant));
// The symlink doesn't exist
expect(is_link($publicPath))->toBeFalse();
expect(file_exists($publicPath))->toBeFalse();
// Flush
Event::fake([RemovingStorageSymlink::class, StorageSymlinkRemoved::class]);
// Nothing happens when there are no symlinks
(new RemoveStorageSymlinksAction)($tenant);
Event::assertNotDispatched(RemovingStorageSymlink::class);
Event::assertNotDispatched(StorageSymlinkRemoved::class);
});
test('removing tenant symlinks works even if the symlinks are invalid', function() {