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

Update ResourceSyncingTest.php

This commit is contained in:
Abrar Ahmad 2022-09-28 11:32:11 +05:00
parent f4f692dd4d
commit 5b34b0283a

View file

@ -142,7 +142,7 @@ test('creating the resource in tenant database creates it in central database as
'name' => 'John Doe', 'name' => 'John Doe',
'email' => 'john@localhost', 'email' => 'john@localhost',
'password' => 'secret', 'password' => 'secret',
'role' => 'commenter', // unsynced 'role' => 'commenter',
]); ]);
tenancy()->end(); tenancy()->end();
@ -211,6 +211,39 @@ test('creating the resource in tenant database creates it in central database wi
expect(CentralUser::first()->code)->toBeNull(); expect(CentralUser::first()->code)->toBeNull();
}); });
test('creating the resource in tenant database creates it in central database with a mix of attributes names and default values', function () {
// Assert no user exists in central DB
expect(ResourceUserWithAttributeNamesAndDefaultValues::all())->toHaveCount(0);
$tenant = ResourceTenant::create();
pest()->artisan('tenants:migrate', [
'--path' => __DIR__ . '/Etc/synced_resource_migrations/custom',
'--realpath' => true,
])->assertExitCode(0);
tenancy()->initialize($tenant);
// Create the user in tenant DB
ResourceUserWithAttributeNames::create([
'global_id' => 'acme',
'name' => 'John Doe',
'email' => 'john@localhost',
'password' => 'secret',
'role' => 'commenter',
'code' => 'bar' // extra column which does not exist in central users table
]);
tenancy()->end();
// Assert central user was created without `code` property
expect(CentralUser::first()->global_id)->toBe('acme');
expect(CentralUser::first()->name)->toBe('John Doe');
expect(CentralUser::first()->email)->toBe('john@localhost');
expect(CentralUser::first()->password)->toBe('secret');
expect(CentralUser::first()->code)->toBeNull();
expect(CentralUser::first()->role)->toBe('admin'); // unsynced so it should be default value
})->skip('mixing attribute names and values is not possible with current implementation');
test('creating the resource in central database creates it in tenant database as 1:1 copy when creation attributes are not specified', function () { test('creating the resource in central database creates it in tenant database as 1:1 copy when creation attributes are not specified', function () {
$centralUser = CentralUser::create([ $centralUser = CentralUser::create([
'global_id' => 'acme', 'global_id' => 'acme',
@ -849,6 +882,23 @@ class ResourceUserWithAttributeNames extends ResourceUser {
} }
// override method in ResourceUser class to return attribute names + default values
class ResourceUserWithAttributeNamesAndDefaultValues extends ResourceUser {
public function getSyncedCreationAttributes(): array
{
// Sync name, email and password but provide default value for role
return
[
'global_id',
'name',
'password',
'email',
'role' => 'admin' // default value
];
}
}
// override method in CentralUser class to return attribute default values // override method in CentralUser class to return attribute default values
class CentralUserWithDefaultValues extends CentralUser { class CentralUserWithDefaultValues extends CentralUser {
public function getSyncedCreationAttributes(): array public function getSyncedCreationAttributes(): array