1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 04:14:03 +00:00

improve request data tests, simplify complex test in UrlGeneratorBootstrapperTest

This commit is contained in:
Samuel Štancl 2025-05-30 02:04:12 +02:00
parent 8fb9e8f74f
commit 94d9dd3201
4 changed files with 84 additions and 62 deletions

View file

@ -14,12 +14,9 @@ beforeEach(function () {
'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 () {
Route::middleware([InitializeTenancyByRequestData::class])->get('/test', function () {
return 'Tenant id: ' . tenant('id');
});
});
@ -27,35 +24,52 @@ beforeEach(function () {
test('header identification works', function () {
$tenant = Tenant::create();
$this
->withoutExceptionHandling()
->withHeader('X-Tenant', $tenant->id)
->get('test')
->assertSee($tenant->id);
// Default header name
$this->withoutExceptionHandling()->withHeader('X-Tenant', $tenant->id)->get('test')->assertSee($tenant->id);
// Custom header name
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.header' => 'X-Custom-Tenant']);
$this->withoutExceptionHandling()->withHeader('X-Custom-Tenant', $tenant->id)->get('test')->assertSee($tenant->id);
// Setting the header to null disables header identification
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.header' => null]);
expect(fn () => $this->withoutExceptionHandling()->withHeader('X-Tenant', $tenant->id)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
});
test('query parameter identification works', function () {
$tenant = Tenant::create();
$this
->withoutExceptionHandling()
->get('test?tenant=' . $tenant->id)
->assertSee($tenant->id);
// Default query parameter name
$this->withoutExceptionHandling()->get('test?tenant=' . $tenant->id)->assertSee($tenant->id);
// Custom query parameter name
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => 'custom_tenant']);
$this->withoutExceptionHandling()->get('test?custom_tenant=' . $tenant->id)->assertSee($tenant->id);
// Setting the query parameter to null disables query parameter identification
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => null]);
expect(fn () => $this->withoutExceptionHandling()->get('test?tenant=' . $tenant->id))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
});
test('cookie identification works', function () {
$tenant = Tenant::create();
$this
->withoutExceptionHandling()
->withUnencryptedCookie('tenant', $tenant->id)
->get('test')
->assertSee($tenant->id);
// Default cookie name
$this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $tenant->id)->get('test')->assertSee($tenant->id);
// Custom cookie name
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.cookie' => 'custom_tenant_id']);
$this->withoutExceptionHandling()->withUnencryptedCookie('custom_tenant_id', $tenant->id)->get('test')->assertSee($tenant->id);
// Setting the cookie to null disables cookie identification
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.cookie' => null]);
expect(fn () => $this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $tenant->id)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
});
// todo@tests encrypted cookie
test('middleware throws exception when tenant data is not provided in the request', function () {
test('an exception is thrown when no tenant data is not provided in the request', function () {
pest()->expectException(TenantCouldNotBeIdentifiedByRequestDataException::class);
$this->withoutExceptionHandling()->get('test');
});