From eae2bd607ce6368e52d204344e9eb2d5341f9b71 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 9 Nov 2022 15:33:38 +0500 Subject: [PATCH] correct method name --- src/Contracts/SyncMaster.php | 2 +- src/Database/Concerns/ResourceSyncing.php | 2 +- src/Listeners/UpdateSyncedResource.php | 10 +++---- tests/ResourceSyncingPolymorphicTest.php | 4 +-- tests/ResourceSyncingTest.php | 34 +++++++++++------------ 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Contracts/SyncMaster.php b/src/Contracts/SyncMaster.php index b72988ab..0bb37b17 100644 --- a/src/Contracts/SyncMaster.php +++ b/src/Contracts/SyncMaster.php @@ -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; } diff --git a/src/Database/Concerns/ResourceSyncing.php b/src/Database/Concerns/ResourceSyncing.php index 97f8e020..0276febc 100644 --- a/src/Database/Concerns/ResourceSyncing.php +++ b/src/Database/Concerns/ResourceSyncing.php @@ -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); diff --git a/src/Listeners/UpdateSyncedResource.php b/src/Listeners/UpdateSyncedResource.php index 117d3b59..39391eac 100644 --- a/src/Listeners/UpdateSyncedResource.php +++ b/src/Listeners/UpdateSyncedResource.php @@ -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); }); diff --git a/tests/ResourceSyncingPolymorphicTest.php b/tests/ResourceSyncingPolymorphicTest.php index 3d1cf58a..c7274b82 100644 --- a/tests/ResourceSyncingPolymorphicTest.php +++ b/tests/ResourceSyncingPolymorphicTest.php @@ -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']); diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index c547f853..f6e2650b 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -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);