diff --git a/src/Middleware/InitializeTenancyByRequestData.php b/src/Middleware/InitializeTenancyByRequestData.php index 95a76c0b..a3b1bd4e 100644 --- a/src/Middleware/InitializeTenancyByRequestData.php +++ b/src/Middleware/InitializeTenancyByRequestData.php @@ -36,11 +36,11 @@ class InitializeTenancyByRequestData * @param \Closure $next * @return mixed */ - public function handle($request, Closure $next, TenantManager $tenantManager) + public function handle($request, Closure $next) { if ($request->method() !== 'OPTIONS') { try { - $this->initializeTenancy($request, $tenantManager); + $this->initializeTenancy($request); } catch (TenantCouldNotBeIdentifiedException $e) { ($this->onFail)($e); } @@ -49,9 +49,9 @@ class InitializeTenancyByRequestData return $next($request); } - protected function initializeTenancy(Request $request, TenantManager $tenancy) + protected function initializeTenancy(Request $request) { - if ($tenancy->initialized) { + if (tenancy()->initialized) { return; } @@ -66,6 +66,6 @@ class InitializeTenancyByRequestData throw new TenantCouldNotBeIdentifiedException($request->getHost()); } - $tenancy->initialize($tenancy->find($tenant)); + tenancy()->initialize(tenancy()->find($tenant)); } } diff --git a/tests/RequestDataIdentificationTest.php b/tests/RequestDataIdentificationTest.php new file mode 100644 index 00000000..0fbacf09 --- /dev/null +++ b/tests/RequestDataIdentificationTest.php @@ -0,0 +1,60 @@ + [ + 'localhost', + ], + ]); + + Route::middleware(InitializeTenancyByRequestData::class)->get('/test', function () { + return 'Tenant id: ' . tenant('id'); + }); + } + + /** @test */ + public function header_identification_works() + { + $this->app->bind(InitializeTenancyByRequestData::class, function () { + return new InitializeTenancyByRequestData('X-Tenant'); + }); + $tenant = Tenant::new()->save(); + $tenant2 = Tenant::new()->save(); + + $this + ->withoutExceptionHandling() + ->get('test', [ + 'X-Tenant' => $tenant->id, + ]) + ->assertSee($tenant->id); + } + + /** @test */ + public function query_parameter_identification_works() + { + $this->app->bind(InitializeTenancyByRequestData::class, function () { + return new InitializeTenancyByRequestData(null, 'tenant'); + }); + $tenant = Tenant::new()->save(); + $tenant2 = Tenant::new()->save(); + + $this + ->withoutExceptionHandling() + ->get('test?tenant=' . $tenant->id) + ->assertSee($tenant->id); + } +}