Skip to content

Comments

Tenant not determined during browser testing#629

Closed
moisish wants to merge 2 commits intospatie:mainfrom
moisish:determine-tenant-on-testing
Closed

Tenant not determined during browser testing#629
moisish wants to merge 2 commits intospatie:mainfrom
moisish:determine-tenant-on-testing

Conversation

@moisish
Copy link
Contributor

@moisish moisish commented Feb 20, 2026

The Problem

When running Pest browser tests, the current tenant is never determined. This is because
Multitenancy::configureRequests() checks $this->app->runningInConsole() and skips determineCurrentTenant() entirely when running in a console context.

Since the test runner executes in the console, tenant resolution is bypassed — even though the application is handling
real HTTP requests during browser tests.

My Workaround

To work around this in my project, I had to register a custom middleware in my TestCase::setUp() that manually
resolves the tenant for every request:

protected function setUp(): void
{
    parent::setUp();

    $this->app->bind('pest-resolve-tenant', fn () => new class
    {
        public function handle(Request $request, Closure $next): Response
        {
            if (! app(IsTenant::class)::checkCurrent()) {
                app(TenantFinder::class)->findForRequest($request)?->makeCurrent();
            }

          return $next($request);
      }
  });

  $this->app['router']->prependMiddlewareToGroup('web', 'pest-resolve-tenant');
}

This works but is boilerplate that every user running browser tests would need to add.

Proposed Fix

Add a new config option determine_current_tenant_in_tests (default false) that allows tenant determination when running in a test context.
It only applies during tests and is fully opt-in — no change in default behavior.
src/Multitenancy.php — Updated configureRequests():

protected function configureRequests(): static
{
    if (! $this->app->runningInConsole() || ($this->app->runningUnitTests() && $this->app['config']->get('multitenancy.determine_current_tenant_in_tests'))) {
        $this->determineCurrentTenant();
    }

    return $this;
}

@moisish moisish marked this pull request as ready for review February 20, 2026 11:00
@masterix21
Copy link
Collaborator

Hi @moisish, I think the PR introduces an unnecessary complexity. Your workaround requires less code lines than your PR: and I think this is enough.

Thanks for your effort.

@masterix21 masterix21 closed this Feb 20, 2026
@moisish
Copy link
Contributor Author

moisish commented Feb 20, 2026

This is an issue that other people will have. The default behaviour does not change so I disagree with the unnecessary complexity.

@masterix21
Copy link
Collaborator

Your workaround could be documented in our doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants