mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 12:54:05 +00:00
correct method name
This commit is contained in:
parent
01a3a5fbab
commit
eae2bd607c
5 changed files with 26 additions and 26 deletions
|
|
@ -14,7 +14,7 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|||
*/
|
||||
interface SyncMaster extends Syncable
|
||||
{
|
||||
public function resources(): MorphToMany;
|
||||
public function tenants(): MorphToMany;
|
||||
|
||||
public function getTenantModelName(): string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ trait ResourceSyncing
|
|||
return true;
|
||||
}
|
||||
|
||||
public function resources(): MorphToMany
|
||||
public function tenants(): MorphToMany
|
||||
{
|
||||
return $this->morphToMany($this->getResourceTenantModelName(), 'tenant_resources', 'tenant_resources', 'resource_global_id', 'tenant_id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
|
|
|
|||
|
|
@ -46,10 +46,10 @@ class UpdateSyncedResource extends QueueableListener
|
|||
|
||||
// Since this model is "dirty" (taken by reference from the event), it might have the tenants
|
||||
// relationship already loaded and cached. For this reason, we refresh the relationship.
|
||||
$centralModel->load('resources');
|
||||
$centralModel->load('tenants');
|
||||
|
||||
/** @var TenantCollection $tenants */
|
||||
$tenants = $centralModel->resources;
|
||||
$tenants = $centralModel->tenants;
|
||||
|
||||
return $tenants;
|
||||
}
|
||||
|
|
@ -80,7 +80,7 @@ class UpdateSyncedResource extends QueueableListener
|
|||
return ((string) $model->pivot->tenant_id) === ((string) $tenant->getTenantKey());
|
||||
};
|
||||
|
||||
$mappingExists = $centralModel->resources->contains($currentTenantMapping);
|
||||
$mappingExists = $centralModel->tenants->contains($currentTenantMapping);
|
||||
|
||||
if (! $mappingExists) {
|
||||
// Here we should call TenantPivot, but we call general Pivot, so that this works
|
||||
|
|
@ -89,12 +89,12 @@ class UpdateSyncedResource extends QueueableListener
|
|||
/** @var Tenant */
|
||||
$tenant = $event->tenant;
|
||||
|
||||
$centralModel->resources()->attach($tenant->getTenantKey());
|
||||
$centralModel->tenants()->attach($tenant->getTenantKey());
|
||||
});
|
||||
}
|
||||
|
||||
/** @var TenantCollection $tenants */
|
||||
$tenants = $centralModel->resources->filter(function ($model) use ($currentTenantMapping) {
|
||||
$tenants = $centralModel->tenants->filter(function ($model) use ($currentTenantMapping) {
|
||||
// Remove the mapping for the current tenant.
|
||||
return ! $currentTenantMapping($model);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ test('polymorphic relationship works for every model when syncing resources from
|
|||
});
|
||||
|
||||
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
$tenant1->run(function () use ($centralUser) {
|
||||
$resourceUser = ResourceUserForPolymorphic::first()->only(['name', 'email', 'password', 'role']);
|
||||
|
|
@ -95,7 +95,7 @@ test('polymorphic relationship works for every model when syncing resources from
|
|||
});
|
||||
|
||||
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
||||
$centralCompany->resources()->attach('t2');
|
||||
$centralCompany->tenants()->attach('t2');
|
||||
|
||||
$tenant2->run(function () use ($centralCompany) {
|
||||
$resourceCompany = ResourceCompanyForPolymorphic::first()->only(['name', 'email']);
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ test('sync resource creation works when central model provides attributes and re
|
|||
});
|
||||
|
||||
// When central model provides the list of attributes, resource model will be created from the provided list of attributes' values
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
$tenant1->run(function () {
|
||||
$resourceUser = ResourceUserProvidingDefaultValues::all();
|
||||
|
|
@ -206,7 +206,7 @@ test('sync resource creation works when central model provides default values an
|
|||
});
|
||||
|
||||
// When central model provides the list of default values, resource model will be created from the provided list of default values
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
$tenant1->run(function () {
|
||||
// Assert resource user was created using the list of default values
|
||||
|
|
@ -258,7 +258,7 @@ test('sync resource creation works when central model provides mixture and resou
|
|||
});
|
||||
|
||||
// When central model provides the list of a mixture (attributes and default values), resource model will be created from the provided list of mixture (attributes and default values)
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
$tenant1->run(function () {
|
||||
$resourceUser = ResourceUser::first();
|
||||
|
|
@ -312,7 +312,7 @@ test('sync resource creation works when central model provides nothing and resou
|
|||
});
|
||||
|
||||
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
expect($centralUser->getSyncedCreationAttributes())->toBeNull();
|
||||
$tenant1->run(function () use ($centralUser) {
|
||||
|
|
@ -377,7 +377,7 @@ test('attaching a tenant to the central resource triggers a pull from the tenant
|
|||
expect(ResourceUser::all())->toHaveCount(0);
|
||||
});
|
||||
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
$tenant->run(function () {
|
||||
expect(ResourceUser::all())->toHaveCount(1);
|
||||
|
|
@ -433,8 +433,8 @@ test('resources are synced only to workspaces that have the resource', function
|
|||
]);
|
||||
migrateUsersTableForTenants();
|
||||
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->resources()->attach('t2');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
$centralUser->tenants()->attach('t2');
|
||||
// t3 is not attached
|
||||
|
||||
$t1->run(function () {
|
||||
|
|
@ -472,7 +472,7 @@ test('when a resource exists in other tenant dbs but is created in a tenant db t
|
|||
migrateUsersTableForTenants();
|
||||
|
||||
// Copy (cascade) user to t1 DB
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
$t2->run(function () {
|
||||
// Create user with the same global ID in t2 database
|
||||
|
|
@ -520,9 +520,9 @@ test('the synced columns are updated in other tenant dbs where the resource exis
|
|||
migrateUsersTableForTenants();
|
||||
|
||||
// Copy (cascade) user to t1 DB
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->resources()->attach('t2');
|
||||
$centralUser->resources()->attach('t3');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
$centralUser->tenants()->attach('t2');
|
||||
$centralUser->tenants()->attach('t3');
|
||||
|
||||
$t3->run(function () {
|
||||
ResourceUser::first()->update([
|
||||
|
|
@ -574,7 +574,7 @@ test('when the resource doesnt exist in the tenant db non synced columns will ca
|
|||
|
||||
migrateUsersTableForTenants();
|
||||
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
|
||||
$t1->run(function () {
|
||||
expect(ResourceUser::first()->role)->toBe('employee');
|
||||
|
|
@ -650,17 +650,17 @@ test('an event is fired for all touched resources', function () {
|
|||
migrateUsersTableForTenants();
|
||||
|
||||
// Copy (cascade) user to t1 DB
|
||||
$centralUser->resources()->attach('t1');
|
||||
$centralUser->tenants()->attach('t1');
|
||||
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
||||
return $event->tenant->getTenantKey() === 't1';
|
||||
});
|
||||
|
||||
$centralUser->resources()->attach('t2');
|
||||
$centralUser->tenants()->attach('t2');
|
||||
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
||||
return $event->tenant->getTenantKey() === 't2';
|
||||
});
|
||||
|
||||
$centralUser->resources()->attach('t3');
|
||||
$centralUser->tenants()->attach('t3');
|
||||
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
|
||||
return $event->tenant->getTenantKey() === 't3';
|
||||
});
|
||||
|
|
@ -748,7 +748,7 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
|
|||
expect(CentralUser::first()->role)->toBe('commenter');
|
||||
|
||||
// Assert mapping was created
|
||||
expect(CentralUser::first()->resources)->toHaveCount(1);
|
||||
expect(CentralUser::first()->tenants)->toHaveCount(1);
|
||||
|
||||
// Assert role change doesn't cascade
|
||||
CentralUser::first()->update(['role' => 'central superadmin']);
|
||||
|
|
@ -785,7 +785,7 @@ test('resources are synced only when sync is enabled', function (bool $enabled)
|
|||
'role' => 'commenter',
|
||||
]);
|
||||
|
||||
$centralUser->resources()->attach('t2');
|
||||
$centralUser->tenants()->attach('t2');
|
||||
|
||||
$tenant2->run(function () use ($enabled) {
|
||||
expect(TenantUserWithConditionalSync::all())->toHaveCount($enabled ? 1 : 0);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue