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

Improve route() name prefixing test

This commit is contained in:
lukinovec 2025-02-10 13:35:37 +01:00
parent 9db1fcf6bd
commit fbadaebc9c

View file

@ -88,42 +88,38 @@ test('the route helper can receive the tenant parameter automatically', function
// When the tenant parameter isn't added to the defaults, the tenant parameter has to be passed "manually" // When the tenant parameter isn't added to the defaults, the tenant parameter has to be passed "manually"
// by setting $passTenantParameterToRoutes to true. This is only preferrable with query string identification. // by setting $passTenantParameterToRoutes to true. This is only preferrable with query string identification.
// With path identification, this ultimately doesn't have any effect because of the defaults (but this can still be used instead of the defaults). // With path identification, this ultimately doesn't have any effect
// if UrlGeneratorBootstrapper::$addTenantParameterToDefaults is true,
// but TenancyUrlGenerator::$passTenantParameterToRoutes can still be used instead.
TenancyUrlGenerator::$passTenantParameterToRoutes = $passTenantParameterToRoutes; TenancyUrlGenerator::$passTenantParameterToRoutes = $passTenantParameterToRoutes;
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenantKey = $tenant->getTenantKey(); $tenantKey = $tenant->getTenantKey();
Route::get('/central/home', fn () => route('home')) Route::get('/central/home', fn () => route('home'))->name('home');
->name('home');
Route::get($identification === InitializeTenancyByPath::class ? "/{tenant}/home" : '/tenant/home', fn () => route('tenant.home')) $tenantRoute = $identification === InitializeTenancyByPath::class ? "/{tenant}/home" : "/tenant/home";
Route::get($tenantRoute, fn () => route('tenant.home'))
->name('tenant.home') ->name('tenant.home')
->middleware(['tenant', $identification]); ->middleware(['tenant', $identification]);
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
if ($identification === InitializeTenancyByRequestData::class && ! $passTenantParameterToRoutes) { $expectedUrl = match (true) {
expect(route('tenant.home'))->toBe("{$appUrl}/tenant/home"); $identification === InitializeTenancyByRequestData::class && $passTenantParameterToRoutes => "{$appUrl}/tenant/home?tenant={$tenantKey}",
} $identification === InitializeTenancyByRequestData::class => "{$appUrl}/tenant/home", // $passTenantParameterToRoutes is false
$identification === InitializeTenancyByPath::class && ($addTenantParameterToDefaults || $passTenantParameterToRoutes) => "{$appUrl}/{$tenantKey}/home",
$identification === InitializeTenancyByPath::class => null, // Should throw an exception -- route() doesn't receive the tenant parameter in this case
};
if ($identification === InitializeTenancyByRequestData::class) { if ($expectedUrl === null) {
if ($passTenantParameterToRoutes) { expect(fn () => route('tenant.home'))->toThrow(UrlGenerationException::class, 'Missing parameter: tenant');
expect(route('tenant.home'))->toBe("{$appUrl}/tenant/home?tenant={$tenantKey}"); } else {
} else { expect(route('tenant.home'))->toBe($expectedUrl);
expect(route('tenant.home'))->toBe("{$appUrl}/tenant/home");
}
} elseif ($identification === InitializeTenancyByPath::class) {
if ($addTenantParameterToDefaults || $passTenantParameterToRoutes) {
expect(route('tenant.home'))->toBe("{$appUrl}/{$tenantKey}/home");
} else {
expect(fn () => route('tenant.home'))->toThrow(UrlGenerationException::class);
}
} }
})->with([ })->with([InitializeTenancyByPath::class, InitializeTenancyByRequestData::class])
InitializeTenancyByPath::class, ->with([true, false]) // UrlGeneratorBootstrapper::$addTenantParameterToDefaults
InitializeTenancyByRequestData::class,
])->with([true, false]) // UrlGeneratorBootstrapper::$addTenantParameterToDefaults
->with([true, false]); // TenancyUrlGenerator::$passTenantParameterToRoutes ->with([true, false]); // TenancyUrlGenerator::$passTenantParameterToRoutes
test('both the name prefixing and the tenant parameter logic gets skipped when bypass parameter is used', function () { test('both the name prefixing and the tenant parameter logic gets skipped when bypass parameter is used', function () {