Testing a multi tenant app with dynamic routes
Hi,
I built a multi tenant Laravel application with multiple domains. The current tenant is resolved through request()->getHost()
Some of the domains share a couple of routes, as they are very similar websites. But some domains have their own custom routes, which overwrite the shared routes:
Route::domain('{job_board?}')
->group(function () {
// Import the shared routes.
Route::name('job-board.')->group(base_path('routes/job-boards/shared.php'));
// Import the specific tenant routes.
if (file_exists(base_path(sprintf('routes/job-boards/tenants/%s.php', tenant()->slug)))) {
Route::name('job-board.')->group(base_path(sprintf('routes/job-boards/tenants/%s.php', tenant()?->slug)));
}
});
This all works fine on the server. But when running tests, the value of request()->getHost() is always set to the APP_URL value of the .env.testing file. So it doesn't load the dynamic routes, and tests of the tenant domains fail with a 404:
it('shows the tenant X home page')
->get('https://tenant-x.test')
->assertOk()
Any idea how to solve this, or how to structure the routes in a way this will work?
Things I've tried:
- Sending a HTTP_HOST header along with the get() request
- Set the APP_URL to the tenant's URL:
config(['app.url' => 'https://tenant-x.test']);inside the test - Define the dynamic routes in the RouteServiceProvider
- Overwriting the
$GLOBALS['_ENV']['APP_HOST']value - lots of other 'hacky' stuff...
Please or to participate in this conversation.