1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 02:24:04 +00:00

merge default values with sync attributes and tests

This commit is contained in:
Abrar Ahmad 2022-08-30 12:29:52 +05:00
parent 80496fb0f0
commit d22224c945
2 changed files with 31 additions and 25 deletions

View file

@ -122,14 +122,18 @@ class UpdateSyncedResource extends QueueableListener
protected function getAttributesForCreation(Syncable $model): array protected function getAttributesForCreation(Syncable $model): array
{ {
$attributes = $model->getAttributes(); if (! $model->getResourceCreationAttributes()) {
// Creation attributes are not specified so create the model as 1:1 copy
if ($model->getResourceCreationAttributes()) { return $model->getAttributes();
// If the developer provided a key-value array, we'd use them as it
// If the developer provided a plain array, we'd use them to pick model attributes
$attributes = Arr::isAssoc($model->getResourceCreationAttributes()) ? $model->getResourceCreationAttributes() : $model->only($model->getResourceCreationAttributes());
} }
return $attributes; if (Arr::isAssoc($model->getResourceCreationAttributes())) {
// Developer provided the default values
// We will merge the default values with sync attributes
return array_merge($model->getResourceCreationAttributes(), $model->only($model->getSyncedAttributeNames()));
}
// Developer provided the attribute names, so we'd use them to pick model attributes
return $model->only($model->getResourceCreationAttributes());
} }
} }

View file

@ -172,10 +172,13 @@ test('creating the resource in tenant database creates it in central database wi
tenancy()->end(); tenancy()->end();
expect(CentralUser::first()->global_id)->toBe('abc-123'); // Assert model attributes are synced
expect(CentralUser::first()->name)->toBe('John'); expect(CentralUser::first()->global_id)->toBe('acme');
expect(CentralUser::first()->password)->toBe('password'); expect(CentralUser::first()->name)->toBe('John Doe');
expect(CentralUser::first()->email)->toBe('john@demo'); expect(CentralUser::first()->password)->toBe('secret');
expect(CentralUser::first()->email)->toBe('john@localhost');
// Assert the "role" attribute is unsynced and we are using the default values
expect(CentralUser::first()->role)->toBe('admin'); expect(CentralUser::first()->role)->toBe('admin');
}); });
@ -229,6 +232,7 @@ test('creating the resource in central database creates it in tenant database as
$centralUser->tenants()->attach('t1'); $centralUser->tenants()->attach('t1');
$centralUser = CentralUser::first(); $centralUser = CentralUser::first();
expect($centralUser->getResourceCreationAttributes())->toBeNull();
$tenant->run(function () use ($centralUser) { $tenant->run(function () use ($centralUser) {
expect(ResourceUser::all())->toHaveCount(1); expect(ResourceUser::all())->toHaveCount(1);
expect(ResourceUser::first()->toArray())->toEqual($centralUser->toArray()); expect(ResourceUser::first()->toArray())->toEqual($centralUser->toArray());
@ -257,10 +261,14 @@ test('creating the resource in central database creates it in tenant database wi
$tenant->run(function () { $tenant->run(function () {
expect(ResourceUser::all())->toHaveCount(1); expect(ResourceUser::all())->toHaveCount(1);
expect(ResourceUser::first()->global_id)->toBe('abc-123');
expect(ResourceUser::first()->name)->toBe('John'); // Assert model attributes are synced
expect(ResourceUser::first()->password)->toBe('password'); expect(ResourceUser::first()->global_id)->toBe('acme');
expect(ResourceUser::first()->email)->toBe('john@demo'); expect(ResourceUser::first()->name)->toBe('John Doe');
expect(ResourceUser::first()->password)->toBe('secret');
expect(ResourceUser::first()->email)->toBe('john@localhost');
// Assert the "role" attribute is unsynced and we are using the default values
expect(ResourceUser::first()->role)->toBe('admin'); expect(ResourceUser::first()->role)->toBe('admin');
}); });
}); });
@ -766,6 +774,7 @@ class CentralUser extends Model implements SyncMaster
public function getSyncedAttributeNames(): array public function getSyncedAttributeNames(): array
{ {
return [ return [
'global_id',
'name', 'name',
'password', 'password',
'email', 'email',
@ -801,6 +810,7 @@ class ResourceUser extends Model implements Syncable
public function getSyncedAttributeNames(): array public function getSyncedAttributeNames(): array
{ {
return [ return [
'global_id',
'name', 'name',
'password', 'password',
'email', 'email',
@ -815,11 +825,7 @@ class ResourceUserWithDefaultValues extends ResourceUser {
// Attributes default values when creating resources from tenant to central DB // Attributes default values when creating resources from tenant to central DB
return return
[ [
'global_id' => 'abc-123', 'role' => 'admin', // Provide "role" default value because it is unsynced or does not exist in Resource model
'name' => 'John',
'password' => 'password',
'email' => 'john@demo',
'role' => 'admin',
]; ];
} }
} }
@ -850,11 +856,7 @@ class CentralUserWithDefaultValues extends CentralUser {
// Attributes default values when creating resources from central to tenant model // Attributes default values when creating resources from central to tenant model
return return
[ [
'global_id' => 'abc-123', 'role' => 'admin', // Provide "role" default value because it is unsynced or does not exist in Central model
'name' => 'John',
'password' => 'password',
'email' => 'john@demo',
'role' => 'admin',
]; ];
} }
} }