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

Simplify comments in test

This commit is contained in:
lukinovec 2025-06-24 13:38:41 +02:00
parent ee28d9ec7d
commit 8254b9b7b7

View file

@ -1212,10 +1212,8 @@ test('global scopes on syncable models can break resource syncing', function ()
'role' => 'admin', // not 'visible' 'role' => 'admin', // not 'visible'
]); ]);
// Create a new tenant resource. // Create a tenant resource. The global id matches that of the central user created above,
// While creating or updating a tenant resource, it's attempted to find the central resource with the same global_id. // so the synced columns of the central record will be updated.
// A central resource with the 'foo' global_id exists already,
// so it will be found and updated.
$tenant1->run(fn () => TenantUser::create([ $tenant1->run(fn () => TenantUser::create([
'global_id' => 'foo', 'global_id' => 'foo',
'name' => 'tenant1 user', 'name' => 'tenant1 user',
@ -1226,18 +1224,12 @@ test('global scopes on syncable models can break resource syncing', function ()
expect($centralUser->refresh()->name)->toBe('tenant1 user'); expect($centralUser->refresh()->name)->toBe('tenant1 user');
// Add a global scope to the central resource. // While syncing a tenant resource with the same global id,
// The scope hides the corresponding central resources from internal resource syncing query results, // the central resource will not be found due to this scope,
// so during the syncing, the corresponding central resource won't be found // leading to the syncing logic trying to create a new central resource with that same global id,
// because its 'role' column is not set to 'visible'. // triggering a unique constraint violation exception.
CentralUser::addGlobalScope(new VisibleScope()); CentralUser::addGlobalScope(new VisibleScope());
// Create another tenant resource that should be synced to the 'foo' central resource.
// While creating or updating a tenant resource, it's attempted to find the central resource with the same global_id.
// Because of the global VisibleScope, the central resource is NOT FOUND.
// A new central resource is attempted to be created with the 'foo' global_id.
// The central resource with the 'foo' global_id (which is unique) exists already,
// so a duplicate entry exception will be thrown.
expect(function () use ($tenant1) { expect(function () use ($tenant1) {
$tenant1->run(fn () => TenantUser::create([ $tenant1->run(fn () => TenantUser::create([
'global_id' => 'foo', 'global_id' => 'foo',
@ -1251,17 +1243,12 @@ test('global scopes on syncable models can break resource syncing', function ()
// The central resource stays the same // The central resource stays the same
expect($centralUser->refresh()->name)->toBe('tenant1 user'); expect($centralUser->refresh()->name)->toBe('tenant1 user');
// As a workaround, UpdateOrCreateSyncedResource::$scopeGetModelQuery // Use UpdateOrCreateSyncedResource::$scopeGetModelQuery to bypass the global scope.
// can be used to bypass the global scope while syncing resources,
// allowing even central resources with role other than 'visible' to be found
// and correctly updated while keeping the model's global scope.
UpdateOrCreateSyncedResource::$scopeGetModelQuery = function (Builder $query) { UpdateOrCreateSyncedResource::$scopeGetModelQuery = function (Builder $query) {
$query->withoutGlobalScope(VisibleScope::class); $query->withoutGlobalScope(VisibleScope::class);
}; };
// Run essentially the same code as above, but this time, // Now, the central resource IS found, and no exception is thrown
// the central resource will be found and updated successfully.
// The central resource already exists, and now, it IS found, and no exception is thrown.
$tenant2->run(fn () => TenantUser::create([ $tenant2->run(fn () => TenantUser::create([
'global_id' => 'foo', 'global_id' => 'foo',
'name' => 'tenant2 user', 'name' => 'tenant2 user',