From bf36783d447a4cdcbccb7b93d6df02fcc27ca146 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 4 Nov 2022 18:01:10 +0500 Subject: [PATCH] rename TenantUser class --- tests/ResourceSyncingTest.php | 65 ++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 9e4907bc..37bf5f8b 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -60,7 +60,7 @@ beforeEach(function () { test('an event is triggered when a synced resource is changed', function () { Event::fake([SyncedResourceSaved::class]); - $user = TenantUser::create([ + $user = ResourceUser::create([ 'name' => 'Foo', 'email' => 'foo@email.com', 'password' => 'secret', @@ -89,7 +89,7 @@ test('only the synced columns are updated in the central db', function () { tenancy()->initialize($tenant); // Create the same user in tenant DB - $user = TenantUser::create([ + $user = ResourceUser::create([ 'global_id' => 'acme', 'name' => 'John Doe', 'email' => 'john@localhost', @@ -124,7 +124,7 @@ test('only the synced columns are updated in the central db', function () { 'email' => 'john@foreignhost', // synced 'password' => 'secret', // no changes 'role' => 'superadmin', // unsynced - ], TenantUser::first()->getAttributes()); + ], ResourceUser::first()->getAttributes()); }); // This tests attribute list on the central side, and default values on the tenant side @@ -253,14 +253,14 @@ test('sync resource creation works when central model provides mixture and tenan ]); $tenant1->run(function () { - expect(TenantUser::all())->toHaveCount(0); + expect(ResourceUser::all())->toHaveCount(0); }); // 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->tenants()->attach('t1'); $tenant1->run(function () { - $resourceUser = TenantUser::first(); + $resourceUser = ResourceUser::first(); // Assert resource user was created using the provided attributes and default values expect($resourceUser->global_id)->toBe('acme'); @@ -274,7 +274,7 @@ test('sync resource creation works when central model provides mixture and tenan tenancy()->initialize($tenant2); // When resource model provides nothing/null, the central model will be created as a 1:1 copy of resource model - $resourceUser = TenantUser::create([ + $resourceUser = ResourceUser::create([ 'global_id' => 'acmey', 'name' => 'John Doe', 'email' => 'john@localhost', @@ -363,7 +363,7 @@ test('trying to update synced resources from central context using tenant models expect(tenancy()->initialized)->toBeFalse(); pest()->expectException(ModelNotSyncMasterException::class); - TenantUser::first()->update(['role' => 'foobar']); + ResourceUser::first()->update(['role' => 'foobar']); }); test('attaching a tenant to the central resource triggers a pull from the tenant db', function () { @@ -381,13 +381,13 @@ test('attaching a tenant to the central resource triggers a pull from the tenant migrateUsersTableForTenants(); $tenant->run(function () { - expect(TenantUser::all())->toHaveCount(0); + expect(ResourceUser::all())->toHaveCount(0); }); $centralUser->tenants()->attach('t1'); $tenant->run(function () { - expect(TenantUser::all())->toHaveCount(1); + expect(ResourceUser::all())->toHaveCount(1); }); }); @@ -406,7 +406,7 @@ test('attaching users to tenants does not do anything', function () { migrateUsersTableForTenants(); $tenant->run(function () { - expect(TenantUser::all())->toHaveCount(0); + expect(ResourceUser::all())->toHaveCount(0); }); // The child model is inaccessible in the Pivot Model, so we can't fire any events. @@ -414,7 +414,7 @@ test('attaching users to tenants does not do anything', function () { $tenant->run(function () { // Still zero - expect(TenantUser::all())->toHaveCount(0); + expect(ResourceUser::all())->toHaveCount(0); }); }); @@ -446,17 +446,17 @@ test('resources are synced only to workspaces that have the resource', function $t1->run(function () { // assert user exists - expect(TenantUser::all())->toHaveCount(1); + expect(ResourceUser::all())->toHaveCount(1); }); $t2->run(function () { // assert user exists - expect(TenantUser::all())->toHaveCount(1); + expect(ResourceUser::all())->toHaveCount(1); }); $t3->run(function () { // assert user does NOT exist - expect(TenantUser::all())->toHaveCount(0); + expect(ResourceUser::all())->toHaveCount(0); }); }); @@ -483,7 +483,7 @@ test('when a resource exists in other tenant dbs but is created in a tenant db t $t2->run(function () { // Create user with the same global ID in t2 database - TenantUser::create([ + ResourceUser::create([ 'global_id' => 'acme', 'name' => 'John Foo', // changed 'email' => 'john@foo', // changed @@ -498,7 +498,7 @@ test('when a resource exists in other tenant dbs but is created in a tenant db t expect($centralUser->role)->toBe('commenter'); // role didn't change $t1->run(function () { - $user = TenantUser::first(); + $user = ResourceUser::first(); expect($user->name)->toBe('John Foo'); // name changed expect($user->email)->toBe('john@foo'); // email changed expect($user->role)->toBe('commenter'); // role didn't change, i.e. is the same as from the original copy from central @@ -532,17 +532,17 @@ test('the synced columns are updated in other tenant dbs where the resource exis $centralUser->tenants()->attach('t3'); $t3->run(function () { - TenantUser::first()->update([ + ResourceUser::first()->update([ 'name' => 'John 3', 'role' => 'employee', // unsynced ]); - expect(TenantUser::first()->role)->toBe('employee'); + expect(ResourceUser::first()->role)->toBe('employee'); }); // Check that change was cascaded to other tenants $t1->run($check = function () { - $user = TenantUser::first(); + $user = ResourceUser::first(); expect($user->name)->toBe('John 3'); // synced expect($user->role)->toBe('commenter'); // unsynced @@ -584,7 +584,7 @@ test('when the resource doesnt exist in the tenant db non synced columns will ca $centralUser->tenants()->attach('t1'); $t1->run(function () { - expect(TenantUser::first()->role)->toBe('employee'); + expect(ResourceUser::first()->role)->toBe('employee'); }); }); @@ -596,7 +596,7 @@ test('when the resource doesnt exist in the central db non synced columns will b migrateUsersTableForTenants(); $t1->run(function () { - TenantUser::create([ + ResourceUser::create([ 'name' => 'John Doe', 'email' => 'john@doe', 'password' => 'secret', @@ -620,7 +620,7 @@ test('the listener can be queued', function () { Queue::assertNothingPushed(); $t1->run(function () { - TenantUser::create([ + ResourceUser::create([ 'name' => 'John Doe', 'email' => 'john@doe', 'password' => 'secret', @@ -681,12 +681,12 @@ test('an event is fired for all touched resources', function () { Event::fake([SyncedResourceChangedInForeignDatabase::class]); $t3->run(function () { - TenantUser::first()->update([ + ResourceUser::first()->update([ 'name' => 'John 3', 'role' => 'employee', // unsynced ]); - expect(TenantUser::first()->role)->toBe('employee'); + expect(ResourceUser::first()->role)->toBe('employee'); }); Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) { @@ -732,7 +732,7 @@ test('an event is fired for all touched resources', function () { function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase() { // Assert no user in central DB - expect(TenantUser::all())->toHaveCount(0); + expect(ResourceUser::all())->toHaveCount(0); $tenant = ResourceTenant::create(); migrateUsersTableForTenants(); @@ -740,7 +740,7 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase() tenancy()->initialize($tenant); // Create the same user in tenant DB - TenantUser::create([ + ResourceUser::create([ 'global_id' => 'acme', 'name' => 'John Doe', 'email' => 'john@localhost', @@ -760,7 +760,7 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase() // Assert role change doesn't cascade CentralUser::first()->update(['role' => 'central superadmin']); tenancy()->initialize($tenant); - expect(TenantUser::first()->role)->toBe('commenter'); + expect(ResourceUser::first()->role)->toBe('commenter'); } /** @@ -820,7 +820,7 @@ class CentralUser extends Model implements SyncMaster public function getTenantModelName(): string { - return TenantUser::class; + return ResourceUser::class; } public function getGlobalIdentifierKey(): string|int @@ -849,7 +849,8 @@ class CentralUser extends Model implements SyncMaster } } -class TenantUser extends Model implements Syncable +// Tenant users +class ResourceUser extends Model implements Syncable { use ResourceSyncing; @@ -885,7 +886,7 @@ class TenantUser extends Model implements Syncable } } -class TenantUserProvidingDefaultValues extends TenantUser +class TenantUserProvidingDefaultValues extends ResourceUser { public function getSyncedCreationAttributes(): array { @@ -899,7 +900,7 @@ class TenantUserProvidingDefaultValues extends TenantUser } } -class TenantUserProvidingAttributeNames extends TenantUser +class TenantUserProvidingAttributeNames extends ResourceUser { public function getSyncedCreationAttributes(): array { @@ -914,7 +915,7 @@ class TenantUserProvidingAttributeNames extends TenantUser } -class TenantUserProvidingMixture extends TenantUser +class TenantUserProvidingMixture extends ResourceUser { public function getSyncedCreationAttributes(): array {