1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 10:34:04 +00:00
tenancy/tests/RequestDataIdentificationTest.php

61 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
use Stancl\Tenancy\Tests\Etc\Tenant;
use function Stancl\Tenancy\Tests\pest;
beforeEach(function () {
config([
'tenancy.identification.central_domains' => [
'localhost',
],
'tenancy.identification.' . RequestDataTenantResolver::class . '.header' => 'X-Tenant',
'tenancy.identification.' . RequestDataTenantResolver::class . '.query_parameter' => 'tenant',
'tenancy.identification.' . RequestDataTenantResolver::class . '.cookie' => 'tenant',
]);
Route::middleware(['tenant', InitializeTenancyByRequestData::class])->get('/test', function () {
return 'Tenant id: ' . tenant('id');
});
});
test('header identification works', function () {
$tenant = Tenant::create();
$this
->withoutExceptionHandling()
->withHeader('X-Tenant', $tenant->id)
->get('test')
->assertSee($tenant->id);
});
test('query parameter identification works', function () {
$tenant = Tenant::create();
$this
->withoutExceptionHandling()
->get('test?tenant=' . $tenant->id)
->assertSee($tenant->id);
});
test('cookie identification works', function () {
$tenant = Tenant::create();
$this
->withoutExceptionHandling()
->withUnencryptedCookie('tenant', $tenant->id)
->get('test')
->assertSee($tenant->id);
});
// todo@tests encrypted cookie
test('middleware throws exception when tenant data is not provided in the request', function () {
pest()->expectException(TenantCouldNotBeIdentifiedByRequestDataException::class);
$this->withoutExceptionHandling()->get('test');
});