1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 18:04:03 +00:00

Fixed Link command for Laravel v6, added StorageLink Events, more StorageLink tests, added RemoveStorageSymlinks Job, added Storage Jobs to TenancyServiceProvider stub, renamed misleading config example.

This commit is contained in:
Martin Vlček 2021-09-13 19:22:06 +02:00
parent 717b834c51
commit 5ed5aea6d6
11 changed files with 253 additions and 15 deletions

View file

@ -17,7 +17,10 @@ use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\Tenant;
@ -218,7 +221,7 @@ class BootstrapperTest extends TestCase
FilesystemTenancyBootstrapper::class,
],
'tenancy.filesystem.root_override.public' => '%storage_path%/app/public/',
'tenancy.filesystem.url_override.public' => 'storage-%tenant_id%'
'tenancy.filesystem.url_override.public' => 'public-%tenant_id%'
]);
$tenant1 = Tenant::create();
@ -226,16 +229,57 @@ class BootstrapperTest extends TestCase
tenancy()->initialize($tenant1);
$this->assertEquals(
'http://localhost/storage-'.$tenant1->getTenantKey().'/',
'http://localhost/public-'.$tenant1->getTenantKey().'/',
Storage::disk('public')->url('')
);
tenancy()->initialize($tenant2);
$this->assertEquals(
'http://localhost/storage-'.$tenant2->getTenantKey().'/',
'http://localhost/public-'.$tenant2->getTenantKey().'/',
Storage::disk('public')->url('')
);
}
/** @test */
public function create_and_delete_storage_symlinks_jobs_works()
{
Event::listen(
TenantCreated::class,
JobPipeline::make([CreateStorageSymlinks::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Event::listen(
TenantDeleted::class,
JobPipeline::make([RemoveStorageSymlinks::class])->send(function (TenantDeleted $event) {
return $event->tenant;
})->toListener()
);
config([
'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class,
],
'tenancy.filesystem.suffix_base' => 'tenant-',
'tenancy.filesystem.root_override.public' => '%storage_path%/app/public/',
'tenancy.filesystem.url_override.public' => 'public-%tenant_id%'
]);
/** @var \Stancl\Tenancy\Database\Models\Tenant $tenant */
$tenant = Tenant::create();
tenancy()->initialize($tenant);
$tenant_key = $tenant->getTenantKey();
$this->assertDirectoryExists(storage_path("app/public"));
$this->assertEquals(storage_path("app/public/"), readlink(public_path("public-$tenant_key")));
$tenant->delete();
$this->assertDirectoryDoesNotExist(public_path("public-$tenant_key"));
}
// for queues see QueueTest
}

View file

@ -34,7 +34,7 @@ class CommandsTest extends TestCase
],
'tenancy.filesystem.suffix_base' => 'tenant-',
'tenancy.filesystem.root_override.public' => '%storage_path%/app/public/',
'tenancy.filesystem.url_override.public' => 'storage-%tenant_id%'
'tenancy.filesystem.url_override.public' => 'public-%tenant_id%'
]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
@ -215,8 +215,35 @@ class CommandsTest extends TestCase
Artisan::call('tenants:link');
$this->assertDirectoryExists(storage_path("tenant-$tenantId1/app/public"));
$this->assertDirectoryExists(public_path("storage-$tenantId1"));
$this->assertEquals(storage_path("tenant-$tenantId1/app/public/"), readlink(public_path("public-$tenantId1")));
$this->assertDirectoryExists(storage_path("tenant-$tenantId2/app/public"));
$this->assertDirectoryExists(public_path("storage-$tenantId2"));
$this->assertEquals(storage_path("tenant-$tenantId2/app/public/"), readlink(public_path("public-$tenantId2")));
Artisan::call('tenants:link', [
'--remove' => true,
]);
$this->assertDirectoryDoesNotExist(public_path("public-$tenantId1"));
$this->assertDirectoryDoesNotExist(public_path("public-$tenantId2"));
}
/** @test */
public function link_command_with_tenant_specified_works()
{
$tenant_key = Tenant::create()->getTenantKey();
Artisan::call('tenants:link', [
'--tenants' => [$tenant_key],
]);
$this->assertDirectoryExists(storage_path("tenant-$tenant_key/app/public"));
$this->assertEquals(storage_path("tenant-$tenant_key/app/public/"), readlink(public_path("public-$tenant_key")));
Artisan::call('tenants:link', [
'--remove' => true,
'--tenants' => [$tenant_key],
]);
$this->assertDirectoryDoesNotExist(public_path("public-$tenant_key"));
}
}