mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 15:14:04 +00:00
Add tests
This commit is contained in:
parent
04e59449a0
commit
d78ce10c21
2 changed files with 65 additions and 5 deletions
|
|
@ -36,11 +36,11 @@ class InitializeTenancyByRequestData
|
||||||
* @param \Closure $next
|
* @param \Closure $next
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function handle($request, Closure $next, TenantManager $tenantManager)
|
public function handle($request, Closure $next)
|
||||||
{
|
{
|
||||||
if ($request->method() !== 'OPTIONS') {
|
if ($request->method() !== 'OPTIONS') {
|
||||||
try {
|
try {
|
||||||
$this->initializeTenancy($request, $tenantManager);
|
$this->initializeTenancy($request);
|
||||||
} catch (TenantCouldNotBeIdentifiedException $e) {
|
} catch (TenantCouldNotBeIdentifiedException $e) {
|
||||||
($this->onFail)($e);
|
($this->onFail)($e);
|
||||||
}
|
}
|
||||||
|
|
@ -49,9 +49,9 @@ class InitializeTenancyByRequestData
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function initializeTenancy(Request $request, TenantManager $tenancy)
|
protected function initializeTenancy(Request $request)
|
||||||
{
|
{
|
||||||
if ($tenancy->initialized) {
|
if (tenancy()->initialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,6 +66,6 @@ class InitializeTenancyByRequestData
|
||||||
throw new TenantCouldNotBeIdentifiedException($request->getHost());
|
throw new TenantCouldNotBeIdentifiedException($request->getHost());
|
||||||
}
|
}
|
||||||
|
|
||||||
$tenancy->initialize($tenancy->find($tenant));
|
tenancy()->initialize(tenancy()->find($tenant));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
60
tests/RequestDataIdentificationTest.php
Normal file
60
tests/RequestDataIdentificationTest.php
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
|
class RequestDataIdentificationTest extends TestCase
|
||||||
|
{
|
||||||
|
public $autoCreateTenant = false;
|
||||||
|
public $autoInitTenancy = false;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.exempt_domains' => [
|
||||||
|
'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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue