'database']); config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]); Event::listen( TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { return $event->tenant; })->toListener() ); Event::listen(Events\TenancyInitialized::class, Listeners\BootstrapTenancy::class); Event::listen(Events\TenancyEnded::class, Listeners\RevertToCentralContext::class); // Sessions table for central database pest()->artisan('migrate', [ '--path' => __DIR__ . '/../Etc/session_migrations', '--realpath' => true, ])->assertExitCode(0); }); test('central helper can be used in tenant requests', function (bool $enabled, bool $shouldThrow) { if ($enabled) { config()->set( 'tenancy.bootstrappers', array_merge(config('tenancy.bootstrappers'), [DatabaseSessionBootstrapper::class]), ); } $tenant = Tenant::create(); $tenant->domains()->create(['domain' => 'foo.localhost']); // run for tenants pest()->artisan('tenants:migrate', [ '--path' => __DIR__ . '/../Etc/session_migrations', '--realpath' => true, ])->assertExitCode(0); Route::middleware(['web', InitializeTenancyByDomain::class])->get('/bar', function () { session(['message' => 'tenant session']); tenancy()->central(function () { return 'central results'; }); return session('message'); }); // We initialize tenancy before making the request, since sessions work a bit differently in tests // and we need the DB session handler to use the tenant connection (as it does in a real app on tenant requests). tenancy()->initialize($tenant); try { $this->withoutExceptionHandling() ->get('http://foo.localhost/bar') ->assertOk() ->assertSee('tenant session'); if ($shouldThrow) { pest()->fail('Exception not thrown'); } } catch (Throwable $e) { if ($shouldThrow) { pest()->assertTrue(true); // empty assertion to make the test pass } else { pest()->fail('Exception thrown: ' . $e->getMessage()); } } })->with([ [ false, // Disabled true // Should throw ], [ true, // Enabled false // Should not throw ], ]); test('tenant run helper can be used on central requests', function (bool $enabled, bool $shouldThrow) { if ($enabled) { config()->set( 'tenancy.bootstrappers', array_merge(config('tenancy.bootstrappers'), [DatabaseSessionBootstrapper::class]), ); } Tenant::create(); // run for tenants pest()->artisan('tenants:migrate', [ '--path' => __DIR__ . '/../Etc/session_migrations', '--realpath' => true, ])->assertExitCode(0); Route::middleware(['web'])->get('/bar', function () { session(['message' => 'central session']); Tenant::first()->run(function () { return 'tenant results'; }); return session('message'); }); try { $this->withoutExceptionHandling() ->get('http://localhost/bar') ->assertOk() ->assertSee('central session'); if ($shouldThrow) { pest()->fail('Exception not thrown'); } } catch (Throwable $e) { if ($shouldThrow) { pest()->assertTrue(true); // empty assertion to make the test pass } else { pest()->fail('Exception thrown: ' . $e->getMessage()); } } })->with([ [ false, // Disabled true // Should throw ], [ true, // Enabled false // Should not throw ], ]);