I Think :
The reason your route()
helper is still generating URLs against localhost.test
in your PHPUnit tests is that, outside of an actual HTTP request, Laravel’s URL generator defaults to whatever you’ve set as your application’s base URL (i.e. config('app.url')
), rather than the tenant’s domain. Calling tenancy()->initialize($tenant)
sets up the database, cache, etc., but does not reconfigure the URL generator to use your tenant’s hostname
Why it happens
route()
uses config('app.url')
when there’s no real request host.
In a test, when you call route('tenant.profile.index')
without a live incoming HTTP host header, Laravel falls back to APP_URL
(or whatever you’ve overridden via config(['app.url' => …])
) to build the link
tenancy()->initialize()
doesn’t touch the URL generator.
The tenancy package swaps databases and filesystems but doesn’t automatically call URL::forceRootUrl()
or UrlGenerator::formatHostUsing()
, so Laravel still thinks your app root is http://localhost.test:8000
You can dynamically override app.url (and force it on the URL generator):
In your setUp()
after initializing tenancy, do:
tenancy()->initialize($this->tenant);
$fqdn = $this->tenant->domains()->first()->domain;
config(['app.url' => "http://{$fqdn}:8000"]);
\URL::forceRootUrl(config('app.url'));
look to this link