From d22224c945cef030eec61f2b1b8addccc6139284 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Tue, 30 Aug 2022 12:29:52 +0500 Subject: [PATCH] merge default values with sync attributes and tests --- src/Listeners/UpdateSyncedResource.php | 18 +++++++----- tests/ResourceSyncingTest.php | 38 ++++++++++++++------------ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/Listeners/UpdateSyncedResource.php b/src/Listeners/UpdateSyncedResource.php index 8a236761..b517df27 100644 --- a/src/Listeners/UpdateSyncedResource.php +++ b/src/Listeners/UpdateSyncedResource.php @@ -122,14 +122,18 @@ class UpdateSyncedResource extends QueueableListener protected function getAttributesForCreation(Syncable $model): array { - $attributes = $model->getAttributes(); - - if ($model->getResourceCreationAttributes()) { - // 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()); + if (! $model->getResourceCreationAttributes()) { + // Creation attributes are not specified so create the model as 1:1 copy + return $model->getAttributes(); } - 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()); } } diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 350d4c8d..2ba80b92 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -172,10 +172,13 @@ test('creating the resource in tenant database creates it in central database wi tenancy()->end(); - expect(CentralUser::first()->global_id)->toBe('abc-123'); - expect(CentralUser::first()->name)->toBe('John'); - expect(CentralUser::first()->password)->toBe('password'); - expect(CentralUser::first()->email)->toBe('john@demo'); + // Assert model attributes are synced + expect(CentralUser::first()->global_id)->toBe('acme'); + expect(CentralUser::first()->name)->toBe('John Doe'); + 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'); }); @@ -229,6 +232,7 @@ test('creating the resource in central database creates it in tenant database as $centralUser->tenants()->attach('t1'); $centralUser = CentralUser::first(); + expect($centralUser->getResourceCreationAttributes())->toBeNull(); $tenant->run(function () use ($centralUser) { expect(ResourceUser::all())->toHaveCount(1); 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 () { expect(ResourceUser::all())->toHaveCount(1); - expect(ResourceUser::first()->global_id)->toBe('abc-123'); - expect(ResourceUser::first()->name)->toBe('John'); - expect(ResourceUser::first()->password)->toBe('password'); - expect(ResourceUser::first()->email)->toBe('john@demo'); + + // Assert model attributes are synced + expect(ResourceUser::first()->global_id)->toBe('acme'); + 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'); }); }); @@ -766,6 +774,7 @@ class CentralUser extends Model implements SyncMaster public function getSyncedAttributeNames(): array { return [ + 'global_id', 'name', 'password', 'email', @@ -801,6 +810,7 @@ class ResourceUser extends Model implements Syncable public function getSyncedAttributeNames(): array { return [ + 'global_id', 'name', 'password', 'email', @@ -815,11 +825,7 @@ class ResourceUserWithDefaultValues extends ResourceUser { // Attributes default values when creating resources from tenant to central DB return [ - 'global_id' => 'abc-123', - 'name' => 'John', - 'password' => 'password', - 'email' => 'john@demo', - 'role' => 'admin', + 'role' => 'admin', // Provide "role" default value because it is unsynced or does not exist in Resource model ]; } } @@ -850,11 +856,7 @@ class CentralUserWithDefaultValues extends CentralUser { // Attributes default values when creating resources from central to tenant model return [ - 'global_id' => 'abc-123', - 'name' => 'John', - 'password' => 'password', - 'email' => 'john@demo', - 'role' => 'admin', + 'role' => 'admin', // Provide "role" default value because it is unsynced or does not exist in Central model ]; } }