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

Use the tenant model column term consistently

This commit is contained in:
lukinovec 2025-06-11 12:26:22 +02:00
parent 035eb9578f
commit eb4890343c

View file

@ -18,11 +18,11 @@ use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
use function Stancl\Tenancy\Tests\pest; use function Stancl\Tenancy\Tests\pest;
test('tenants can be resolved using cached resolvers', function (string $resolver, bool $configureTenantModelColumn) { test('tenants can be resolved using cached resolvers', function (string $resolver, bool $configureTenantModelColumn) {
$tenant = Tenant::create([$tenantColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']); $tenant = Tenant::create([$tenantModelColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']);
$tenant->createDomain($tenant->{$tenantColumn}); $tenant->createDomain($tenant->{$tenantModelColumn});
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
})->with([ })->with([
DomainTenantResolver::class, DomainTenantResolver::class,
PathTenantResolver::class, PathTenantResolver::class,
@ -33,25 +33,25 @@ test('tenants can be resolved using cached resolvers', function (string $resolve
]); ]);
test('the underlying resolver is not touched when using the cached resolver', function (string $resolver, bool $configureTenantModelColumn) { test('the underlying resolver is not touched when using the cached resolver', function (string $resolver, bool $configureTenantModelColumn) {
$tenant = Tenant::create([$tenantColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']); $tenant = Tenant::create([$tenantModelColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']);
$tenant->createDomain($tenant->{$tenantColumn}); $tenant->createDomain($tenant->{$tenantModelColumn});
DB::enableQueryLog(); DB::enableQueryLog();
config(['tenancy.identification.resolvers.' . $resolver . '.cache' => false]); config(['tenancy.identification.resolvers.' . $resolver . '.cache' => false]);
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
DB::flushQueryLog(); DB::flushQueryLog();
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
pest()->assertNotEmpty(DB::getQueryLog()); // not empty pest()->assertNotEmpty(DB::getQueryLog()); // not empty
config(['tenancy.identification.resolvers.' . $resolver . '.cache' => true]); config(['tenancy.identification.resolvers.' . $resolver . '.cache' => true]);
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
DB::flushQueryLog(); DB::flushQueryLog();
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
expect(DB::getQueryLog())->toBeEmpty(); // empty expect(DB::getQueryLog())->toBeEmpty(); // empty
})->with([ })->with([
DomainTenantResolver::class, DomainTenantResolver::class,
@ -63,20 +63,20 @@ test('the underlying resolver is not touched when using the cached resolver', fu
]); ]);
test('cache is invalidated when the tenant is updated', function (string $resolver, bool $configureTenantModelColumn) { test('cache is invalidated when the tenant is updated', function (string $resolver, bool $configureTenantModelColumn) {
$tenant = Tenant::create([$tenantColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']); $tenant = Tenant::create([$tenantModelColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']);
$tenant->createDomain($tenant->{$tenantColumn}); $tenant->createDomain($tenant->{$tenantModelColumn});
DB::enableQueryLog(); DB::enableQueryLog();
config(['tenancy.identification.resolvers.' . $resolver . '.cache' => true]); config(['tenancy.identification.resolvers.' . $resolver . '.cache' => true]);
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
expect(DB::getQueryLog())->not()->toBeEmpty(); expect(DB::getQueryLog())->not()->toBeEmpty();
DB::flushQueryLog(); DB::flushQueryLog();
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
expect(DB::getQueryLog())->toBeEmpty(); expect(DB::getQueryLog())->toBeEmpty();
// Tenant cache gets invalidated when the tenant is updated // Tenant cache gets invalidated when the tenant is updated
@ -84,7 +84,7 @@ test('cache is invalidated when the tenant is updated', function (string $resolv
DB::flushQueryLog(); DB::flushQueryLog();
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
expect(DB::getQueryLog())->not()->toBeEmpty(); // Cache was invalidated, so the tenant was retrieved from the DB expect(DB::getQueryLog())->not()->toBeEmpty(); // Cache was invalidated, so the tenant was retrieved from the DB
})->with([ })->with([
@ -98,25 +98,25 @@ test('cache is invalidated when the tenant is updated', function (string $resolv
test('cache is invalidated when the tenant is deleted', function (string $resolver, bool $configureTenantModelColumn) { test('cache is invalidated when the tenant is deleted', function (string $resolver, bool $configureTenantModelColumn) {
DB::statement('SET FOREIGN_KEY_CHECKS=0;'); // allow deleting the tenant DB::statement('SET FOREIGN_KEY_CHECKS=0;'); // allow deleting the tenant
$tenant = Tenant::create([$tenantColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']); $tenant = Tenant::create([$tenantModelColumn = tenantModelColumn($configureTenantModelColumn) => 'acme']);
$tenant->createDomain($tenant->{$tenantColumn}); $tenant->createDomain($tenant->{$tenantModelColumn});
DB::enableQueryLog(); DB::enableQueryLog();
config(['tenancy.identification.resolvers.' . $resolver . '.cache' => true]); config(['tenancy.identification.resolvers.' . $resolver . '.cache' => true]);
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
expect(DB::getQueryLog())->not()->toBeEmpty(); expect(DB::getQueryLog())->not()->toBeEmpty();
DB::flushQueryLog(); DB::flushQueryLog();
expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn))))->toBeTrue(); expect($tenant->is(app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn))))->toBeTrue();
expect(DB::getQueryLog())->toBeEmpty(); expect(DB::getQueryLog())->toBeEmpty();
$tenant->delete(); $tenant->delete();
DB::flushQueryLog(); DB::flushQueryLog();
expect(fn () => app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantColumn)))->toThrow(TenantCouldNotBeIdentifiedException::class); expect(fn () => app($resolver)->resolve(getResolverArgument($resolver, $tenant, $tenantModelColumn)))->toThrow(TenantCouldNotBeIdentifiedException::class);
expect(DB::getQueryLog())->not()->toBeEmpty(); // Cache was invalidated, so the DB was queried expect(DB::getQueryLog())->not()->toBeEmpty(); // Cache was invalidated, so the DB was queried
})->with([ })->with([
DomainTenantResolver::class, DomainTenantResolver::class,
@ -332,23 +332,23 @@ test('PathTenantResolver properly separates cache for each tenant column', funct
*/ */
function tenantModelColumn(bool $configureTenantModelColumn): string { function tenantModelColumn(bool $configureTenantModelColumn): string {
// Default tenant model column is 'id' // Default tenant model column is 'id'
$tenantColumn = 'id'; $tenantModelColumn = 'id';
if ($configureTenantModelColumn) { if ($configureTenantModelColumn) {
// Use 'name' as the custom tenant model column // Use 'name' as the custom tenant model column
$tenantColumn = 'name'; $tenantModelColumn = 'name';
Tenant::$extraCustomColumns = [$tenantColumn]; Tenant::$extraCustomColumns = [$tenantModelColumn];
Schema::table('tenants', function (Blueprint $table) use ($tenantColumn) { Schema::table('tenants', function (Blueprint $table) use ($tenantModelColumn) {
$table->string($tenantColumn)->unique(); $table->string($tenantModelColumn)->unique();
}); });
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_model_column' => $tenantColumn]); config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantColumn]); config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
} }
return $tenantColumn; return $tenantModelColumn;
} }
/** /**