1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:14:04 +00:00

rename UserTenant to ResourceTenant

This commit is contained in:
Abrar Ahmad 2022-11-04 17:47:50 +05:00
parent 99c26e086c
commit f9ba778e1b

View file

@ -60,7 +60,7 @@ beforeEach(function () {
test('an event is triggered when a synced resource is changed', function () { test('an event is triggered when a synced resource is changed', function () {
Event::fake([SyncedResourceSaved::class]); Event::fake([SyncedResourceSaved::class]);
$user = TenantUser::create([ $user = ResourceUser::create([
'name' => 'Foo', 'name' => 'Foo',
'email' => 'foo@email.com', 'email' => 'foo@email.com',
'password' => 'secret', 'password' => 'secret',
@ -89,7 +89,7 @@ test('only the synced columns are updated in the central db', function () {
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
// Create the same user in tenant DB // Create the same user in tenant DB
$user = TenantUser::create([ $user = ResourceUser::create([
'global_id' => 'acme', 'global_id' => 'acme',
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@localhost', 'email' => 'john@localhost',
@ -124,7 +124,7 @@ test('only the synced columns are updated in the central db', function () {
'email' => 'john@foreignhost', // synced 'email' => 'john@foreignhost', // synced
'password' => 'secret', // no changes 'password' => 'secret', // no changes
'role' => 'superadmin', // unsynced 'role' => 'superadmin', // unsynced
], TenantUser::first()->getAttributes()); ], ResourceUser::first()->getAttributes());
}); });
// This tests attribute list on the central side, and default values on the tenant side // This tests attribute list on the central side, and default values on the tenant side
@ -145,14 +145,14 @@ test('sync resource creation works when central model provides attributes and te
]); ]);
$tenant1->run(function () { $tenant1->run(function () {
expect(TenantUserProvidingDefaultValues::all())->toHaveCount(0); expect(ResourceUserProvidingDefaultValues::all())->toHaveCount(0);
}); });
// When central model provides the list of attributes, resource model will be created from the provided list of attributes' values // When central model provides the list of attributes, resource model will be created from the provided list of attributes' values
$centralUser->tenants()->attach('t1'); $centralUser->tenants()->attach('t1');
$tenant1->run(function () { $tenant1->run(function () {
$resourceUser = TenantUserProvidingDefaultValues::all(); $resourceUser = ResourceUserProvidingDefaultValues::all();
expect($resourceUser)->toHaveCount(1); expect($resourceUser)->toHaveCount(1);
expect($resourceUser->first()->global_id)->toBe('acme'); expect($resourceUser->first()->global_id)->toBe('acme');
expect($resourceUser->first()->email)->toBe('john@localhost'); expect($resourceUser->first()->email)->toBe('john@localhost');
@ -163,7 +163,7 @@ test('sync resource creation works when central model provides attributes and te
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
// When resource model provides the list of default values, central model will be created from the provided list of default values // When resource model provides the list of default values, central model will be created from the provided list of default values
TenantUserProvidingDefaultValues::create([ ResourceUserProvidingDefaultValues::create([
'global_id' => 'asdf', 'global_id' => 'asdf',
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@localhost', 'email' => 'john@localhost',
@ -201,7 +201,7 @@ test('sync resource creation works when central model provides default values an
]); ]);
$tenant1->run(function () { $tenant1->run(function () {
expect(TenantUserProvidingDefaultValues::all())->toHaveCount(0); expect(ResourceUserProvidingDefaultValues::all())->toHaveCount(0);
}); });
// When central model provides the list of default values, resource model will be created from the provided list of default values // When central model provides the list of default values, resource model will be created from the provided list of default values
@ -209,7 +209,7 @@ test('sync resource creation works when central model provides default values an
$tenant1->run(function () { $tenant1->run(function () {
// Assert resource user was created using the list of default values // Assert resource user was created using the list of default values
$resourceUser = TenantUserProvidingDefaultValues::first(); $resourceUser = ResourceUserProvidingDefaultValues::first();
expect($resourceUser)->not()->toBeNull(); expect($resourceUser)->not()->toBeNull();
expect($resourceUser->global_id)->toBe('acme'); expect($resourceUser->global_id)->toBe('acme');
expect($resourceUser->email)->toBe('default@localhost'); expect($resourceUser->email)->toBe('default@localhost');
@ -220,7 +220,7 @@ test('sync resource creation works when central model provides default values an
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
// When resource model provides the list of attributes, central model will be created from the provided list of attributes' values // When resource model provides the list of attributes, central model will be created from the provided list of attributes' values
TenantUserProvidingAttributeNames::create([ ResourceUserProvidingAttributeNames::create([
'global_id' => 'asdf', 'global_id' => 'asdf',
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@localhost', 'email' => 'john@localhost',
@ -253,14 +253,14 @@ test('sync resource creation works when central model provides mixture and tenan
]); ]);
$tenant1->run(function () { $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) // 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'); $centralUser->tenants()->attach('t1');
$tenant1->run(function () { $tenant1->run(function () {
$resourceUser = TenantUser::first(); $resourceUser = ResourceUser::first();
// Assert resource user was created using the provided attributes and default values // Assert resource user was created using the provided attributes and default values
expect($resourceUser->global_id)->toBe('acme'); 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); tenancy()->initialize($tenant2);
// When resource model provides nothing/null, the central model will be created as a 1:1 copy of resource model // 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', 'global_id' => 'acmey',
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@localhost', 'email' => 'john@localhost',
@ -311,7 +311,7 @@ test('sync resource creation works when central model provides nothing and tenan
]); ]);
$tenant1->run(function () { $tenant1->run(function () {
expect(TenantUserProvidingMixture::all())->toHaveCount(0); expect(ResourceUserProvidingMixture::all())->toHaveCount(0);
}); });
// When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model // When central model provides nothing/null, the resource model will be created as a 1:1 copy of central model
@ -319,7 +319,7 @@ test('sync resource creation works when central model provides nothing and tenan
expect($centralUser->getSyncedCreationAttributes())->toBeNull(); expect($centralUser->getSyncedCreationAttributes())->toBeNull();
$tenant1->run(function () use ($centralUser) { $tenant1->run(function () use ($centralUser) {
$resourceUser = TenantUserProvidingMixture::first(); $resourceUser = ResourceUserProvidingMixture::first();
expect($resourceUser)->not()->toBeNull(); expect($resourceUser)->not()->toBeNull();
$resourceUser = $resourceUser->toArray(); $resourceUser = $resourceUser->toArray();
$centralUser = $centralUser->withoutRelations()->toArray(); $centralUser = $centralUser->withoutRelations()->toArray();
@ -332,7 +332,7 @@ test('sync resource creation works when central model provides nothing and tenan
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
// When resource model provides the list of a mixture (attributes and default values), central model will be created from the provided list of mixture (attributes and default values) // When resource model provides the list of a mixture (attributes and default values), central model will be created from the provided list of mixture (attributes and default values)
TenantUserProvidingMixture::create([ ResourceUserProvidingMixture::create([
'global_id' => 'absd', 'global_id' => 'absd',
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@localhost', 'email' => 'john@localhost',
@ -363,7 +363,7 @@ test('trying to update synced resources from central context using tenant models
expect(tenancy()->initialized)->toBeFalse(); expect(tenancy()->initialized)->toBeFalse();
pest()->expectException(ModelNotSyncMasterException::class); 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 () { 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(); migrateUsersTableForTenants();
$tenant->run(function () { $tenant->run(function () {
expect(TenantUser::all())->toHaveCount(0); expect(ResourceUser::all())->toHaveCount(0);
}); });
$centralUser->tenants()->attach('t1'); $centralUser->tenants()->attach('t1');
$tenant->run(function () { $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(); migrateUsersTableForTenants();
$tenant->run(function () { $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. // 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 () { $tenant->run(function () {
// Still zero // 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 () { $t1->run(function () {
// assert user exists // assert user exists
expect(TenantUser::all())->toHaveCount(1); expect(ResourceUser::all())->toHaveCount(1);
}); });
$t2->run(function () { $t2->run(function () {
// assert user exists // assert user exists
expect(TenantUser::all())->toHaveCount(1); expect(ResourceUser::all())->toHaveCount(1);
}); });
$t3->run(function () { $t3->run(function () {
// assert user does NOT exist // 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 () { $t2->run(function () {
// Create user with the same global ID in t2 database // Create user with the same global ID in t2 database
TenantUser::create([ ResourceUser::create([
'global_id' => 'acme', 'global_id' => 'acme',
'name' => 'John Foo', // changed 'name' => 'John Foo', // changed
'email' => '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 expect($centralUser->role)->toBe('commenter'); // role didn't change
$t1->run(function () { $t1->run(function () {
$user = TenantUser::first(); $user = ResourceUser::first();
expect($user->name)->toBe('John Foo'); // name changed expect($user->name)->toBe('John Foo'); // name changed
expect($user->email)->toBe('john@foo'); // email 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 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'); $centralUser->tenants()->attach('t3');
$t3->run(function () { $t3->run(function () {
TenantUser::first()->update([ ResourceUser::first()->update([
'name' => 'John 3', 'name' => 'John 3',
'role' => 'employee', // unsynced 'role' => 'employee', // unsynced
]); ]);
expect(TenantUser::first()->role)->toBe('employee'); expect(ResourceUser::first()->role)->toBe('employee');
}); });
// Check that change was cascaded to other tenants // Check that change was cascaded to other tenants
$t1->run($check = function () { $t1->run($check = function () {
$user = TenantUser::first(); $user = ResourceUser::first();
expect($user->name)->toBe('John 3'); // synced expect($user->name)->toBe('John 3'); // synced
expect($user->role)->toBe('commenter'); // unsynced 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'); $centralUser->tenants()->attach('t1');
$t1->run(function () { $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(); migrateUsersTableForTenants();
$t1->run(function () { $t1->run(function () {
TenantUser::create([ ResourceUser::create([
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@doe', 'email' => 'john@doe',
'password' => 'secret', 'password' => 'secret',
@ -620,7 +620,7 @@ test('the listener can be queued', function () {
Queue::assertNothingPushed(); Queue::assertNothingPushed();
$t1->run(function () { $t1->run(function () {
TenantUser::create([ ResourceUser::create([
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@doe', 'email' => 'john@doe',
'password' => 'secret', 'password' => 'secret',
@ -681,12 +681,12 @@ test('an event is fired for all touched resources', function () {
Event::fake([SyncedResourceChangedInForeignDatabase::class]); Event::fake([SyncedResourceChangedInForeignDatabase::class]);
$t3->run(function () { $t3->run(function () {
TenantUser::first()->update([ ResourceUser::first()->update([
'name' => 'John 3', 'name' => 'John 3',
'role' => 'employee', // unsynced 'role' => 'employee', // unsynced
]); ]);
expect(TenantUser::first()->role)->toBe('employee'); expect(ResourceUser::first()->role)->toBe('employee');
}); });
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) { Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {
@ -732,7 +732,7 @@ test('an event is fired for all touched resources', function () {
function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase() function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
{ {
// Assert no user in central DB // Assert no user in central DB
expect(TenantUser::all())->toHaveCount(0); expect(ResourceUser::all())->toHaveCount(0);
$tenant = ResourceTenant::create(); $tenant = ResourceTenant::create();
migrateUsersTableForTenants(); migrateUsersTableForTenants();
@ -740,7 +740,7 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
// Create the same user in tenant DB // Create the same user in tenant DB
TenantUser::create([ ResourceUser::create([
'global_id' => 'acme', 'global_id' => 'acme',
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@localhost', 'email' => 'john@localhost',
@ -760,7 +760,7 @@ function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase()
// Assert role change doesn't cascade // Assert role change doesn't cascade
CentralUser::first()->update(['role' => 'central superadmin']); CentralUser::first()->update(['role' => 'central superadmin']);
tenancy()->initialize($tenant); 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 public function getTenantModelName(): string
{ {
return TenantUser::class; return ResourceUser::class;
} }
public function getGlobalIdentifierKey(): string|int 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; use ResourceSyncing;
@ -885,7 +886,7 @@ class TenantUser extends Model implements Syncable
} }
} }
class TenantUserProvidingDefaultValues extends TenantUser class ResourceUserProvidingDefaultValues extends ResourceUser
{ {
public function getSyncedCreationAttributes(): array public function getSyncedCreationAttributes(): array
{ {
@ -899,7 +900,7 @@ class TenantUserProvidingDefaultValues extends TenantUser
} }
} }
class TenantUserProvidingAttributeNames extends TenantUser class ResourceUserProvidingAttributeNames extends ResourceUser
{ {
public function getSyncedCreationAttributes(): array public function getSyncedCreationAttributes(): array
{ {
@ -914,7 +915,7 @@ class TenantUserProvidingAttributeNames extends TenantUser
} }
class TenantUserProvidingMixture extends TenantUser class ResourceUserProvidingMixture extends ResourceUser
{ {
public function getSyncedCreationAttributes(): array public function getSyncedCreationAttributes(): array
{ {