1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 11:44:04 +00:00

Improve URL bootstrapper test

This commit is contained in:
lukinovec 2023-02-01 17:06:03 +01:00
parent f4b39adcb9
commit 3fbf6b4a45

View file

@ -11,6 +11,7 @@ use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Jobs\CreateDatabase;
@ -28,6 +29,7 @@ use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
@ -385,26 +387,41 @@ function getDiskPrefix(string $disk): string
test('url bootstrapper overrides the root url when tenancy gets initialized and reverts the url to the central one after tenancy ends', function() { test('url bootstrapper overrides the root url when tenancy gets initialized and reverts the url to the central one after tenancy ends', function() {
config(['tenancy.bootstrappers.url' => UrlTenancyBootstrapper::class]); config(['tenancy.bootstrappers.url' => UrlTenancyBootstrapper::class]);
UrlTenancyBootstrapper::$rootUrlOverride = function (Tenant $tenant) { $routeName = 'home';
$baseUrl = URL::to('/'); $routeUri = '/';
$scheme = str($baseUrl)->before('://') . '://';
return str($baseUrl) Route::group([
->after($scheme) 'middleware' => InitializeTenancyBySubdomain::class,
->prepend($tenant->getTenantKey() . '.') ], function () use ($routeUri, $routeName) {
->prepend($scheme) Route::get($routeUri, function () {
->toString(); return true;
})->name($routeName);
});
$baseUrl = url(route($routeName));
$scheme = str($baseUrl)->before('://');
$hostname = str($baseUrl)->after($scheme . '://');
$rootUrlOverride = function (Tenant $tenant) use ($scheme, $hostname) {
return $scheme . '://' . $tenant->getTenantKey() . '.' . $hostname;
}; };
$tenant = Tenant::create(); UrlTenancyBootstrapper::$rootUrlOverride = $rootUrlOverride;
expect(URL::to('/'))->not()->toContain($tenant->getTenantKey()); $tenant = Tenant::create();
$tenantUrl = $rootUrlOverride($tenant);
expect($tenantUrl)->not()->toBe($baseUrl);
expect(url(route($routeName)))->toBe($baseUrl);
expect(URL::to($routeUri))->toBe($baseUrl);
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
expect(URL::to('/'))->toContain($tenant->getTenantKey()); expect(url(route($routeName)))->toBe($tenantUrl);
expect(URL::to($routeUri))->toBe($tenantUrl);
tenancy()->end(); tenancy()->end();
expect(URL::to('/'))->not()->toContain($tenant->getTenantKey()); expect(url(route($routeName)))->toBe($baseUrl);
expect(URL::to($routeUri))->toBe($baseUrl);
}); });