From c515bbfaa14bf2d13ba37111c2b9eb84bdf670af Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 1 Jul 2022 11:37:31 +0500 Subject: [PATCH] resolve reviews --- .github/workflows/ci.yml | 2 +- tests/AutomaticModeTest.php | 34 ++++++------- tests/CommandsTest.php | 2 +- tests/MaintenanceModeTest.php | 2 +- tests/ResourceSyncingTest.php | 96 +++++++++++++---------------------- 5 files changed, 56 insertions(+), 80 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3c77432..3fd09963 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ on: push: branches: [ master ] pull_request: - branches: [ master, shift-64622] # todo0 remove shift-64622 before merge + branches: [ master, shift-64622] # todo0 remove shift-64622 branch before merge jobs: tests: diff --git a/tests/AutomaticModeTest.php b/tests/AutomaticModeTest.php index cb4c5536..6bd5709b 100644 --- a/tests/AutomaticModeTest.php +++ b/tests/AutomaticModeTest.php @@ -15,27 +15,12 @@ beforeEach(function () { Event::listen(TenancyEnded::class, RevertToCentralContext::class); }); -function contextISSwitchedWhenTenancyInitialized() -{ - config(['tenancy.bootstrappers' => [ - MyBootstrapper::class, - ]]); - - $tenant = Tenant::create([ - 'id' => 'acme', - ]); - - tenancy()->initialize($tenant); - - expect(app('tenancy_initialized_for_tenant'))->toBe('acme'); -} - test('context is switched when tenancy is initialized', function () { - contextISSwitchedWhenTenancyInitialized(); + contextIsSwitchedWhenTenancyInitialized(); }); test('context is reverted when tenancy is ended', function () { - contextISSwitchedWhenTenancyInitialized(); + contextIsSwitchedWhenTenancyInitialized(); tenancy()->end(); @@ -104,6 +89,21 @@ test('central helper doesnt change tenancy state when called in central context' expect(tenant())->toBeNull(); }); +function contextIsSwitchedWhenTenancyInitialized() +{ + config(['tenancy.bootstrappers' => [ + MyBootstrapper::class, + ]]); + + $tenant = Tenant::create([ + 'id' => 'acme', + ]); + + tenancy()->initialize($tenant); + + expect(app('tenancy_initialized_for_tenant'))->toBe('acme'); +} + class MyBootstrapper implements TenancyBootstrapper { public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant) diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index d0e025e7..ac4451fb 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -128,7 +128,7 @@ test('database connection is switched to default when tenancy has been initializ databaseConnectionSwitchedToDefault(); }); -test('run commands works', function () { +test('run command works', function () { runCommandWorks(); }); diff --git a/tests/MaintenanceModeTest.php b/tests/MaintenanceModeTest.php index bd459504..dace6c51 100644 --- a/tests/MaintenanceModeTest.php +++ b/tests/MaintenanceModeTest.php @@ -34,4 +34,4 @@ test('tenant can be in maintenance mode', function () { class MaintenanceTenant extends Tenant { use MaintenanceMode; -} \ No newline at end of file +} diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 38609895..fce59bf6 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -127,69 +127,11 @@ test('only the synced columns are updated in the central db', function () { }); test('creating the resource in tenant database creates it in central database and creates the mapping', function () { - // Assert no user in central DB - expect(ResourceUser::all())->toHaveCount(0); - - $tenant = ResourceTenant::create(); - migrateTenantsResource(); - - tenancy()->initialize($tenant); - - // Create the same user in tenant DB - ResourceUser::create([ - 'global_id' => 'acme', - 'name' => 'John Doe', - 'email' => 'john@localhost', - 'password' => 'secret', - 'role' => 'commenter', // unsynced - ]); - - tenancy()->end(); - - // Asset user was created - expect(CentralUser::first()->global_id)->toBe('acme'); - expect(CentralUser::first()->role)->toBe('commenter'); - - // Assert mapping was created - expect(CentralUser::first()->tenants)->toHaveCount(1); - - // Assert role change doesn't cascade - CentralUser::first()->update(['role' => 'central superadmin']); - tenancy()->initialize($tenant); - expect(ResourceUser::first()->role)->toBe('commenter'); + creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase(); }); test('trying to update synced resources from central context using tenant models results in an exception', function () { - // Assert no user in central DB - expect(ResourceUser::all())->toHaveCount(0); - - $tenant = ResourceTenant::create(); - migrateTenantsResource(); - - tenancy()->initialize($tenant); - - // Create the same user in tenant DB - ResourceUser::create([ - 'global_id' => 'acme', - 'name' => 'John Doe', - 'email' => 'john@localhost', - 'password' => 'secret', - 'role' => 'commenter', // unsynced - ]); - - tenancy()->end(); - - // Asset user was created - expect(CentralUser::first()->global_id)->toBe('acme'); - expect(CentralUser::first()->role)->toBe('commenter'); - - // Assert mapping was created - expect(CentralUser::first()->tenants)->toHaveCount(1); - - // Assert role change doesn't cascade - CentralUser::first()->update(['role' => 'central superadmin']); - tenancy()->initialize($tenant); - expect(ResourceUser::first()->role)->toBe('commenter'); + creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase(); tenancy()->end(); expect(tenancy()->initialized)->toBeFalse(); @@ -560,6 +502,40 @@ test('an event is fired for all touched resources', function () { }); }); +function creatingResourceInTenantDatabaseCreatesAndMapInCentralDatabase() +{ + // Assert no user in central DB + expect(ResourceUser::all())->toHaveCount(0); + + $tenant = ResourceTenant::create(); + migrateTenantsResource(); + + tenancy()->initialize($tenant); + + // Create the same user in tenant DB + ResourceUser::create([ + 'global_id' => 'acme', + 'name' => 'John Doe', + 'email' => 'john@localhost', + 'password' => 'secret', + 'role' => 'commenter', // unsynced + ]); + + tenancy()->end(); + + // Asset user was created + expect(CentralUser::first()->global_id)->toBe('acme'); + expect(CentralUser::first()->role)->toBe('commenter'); + + // Assert mapping was created + expect(CentralUser::first()->tenants)->toHaveCount(1); + + // Assert role change doesn't cascade + CentralUser::first()->update(['role' => 'central superadmin']); + tenancy()->initialize($tenant); + expect(ResourceUser::first()->role)->toBe('commenter'); +} + function migrateTenantsResource() { test()->artisan('tenants:migrate', [