diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index f601c4f0..0e376e93 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -115,72 +115,21 @@ test('rollback command works', function () { expect(Schema::hasTable('users'))->toBeFalse(); }); -test('seed command works', function () { - $this->markTestIncomplete(); -}); +// Incomplete test +test('seed command works'); test('database connection is switched to default', function () { - $originalDBName = DB::connection()->getDatabaseName(); - - Artisan::call('tenants:migrate'); - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); - - Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]); - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); - - Artisan::call('tenants:rollback'); - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); - - // $this->run_commands_works(); - $id = Tenant::create()->getTenantKey(); - - Artisan::call('tenants:migrate', ['--tenants' => [$id]]); - - $this->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'") - ->expectsOutput("User's name is Test command") - ->expectsOutput('foo') - ->expectsOutput('xyz'); - - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); + databaseConnectionSwitchedToDefault(); }); test('database connection is switched to default when tenancy has been initialized', function () { tenancy()->initialize(Tenant::create()); - // $this->database_connection_is_switched_to_default(); - $originalDBName = DB::connection()->getDatabaseName(); - - Artisan::call('tenants:migrate'); - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); - - Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]); - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); - - Artisan::call('tenants:rollback'); - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); - - // $this->run_commands_works(); - $id = Tenant::create()->getTenantKey(); - - Artisan::call('tenants:migrate', ['--tenants' => [$id]]); - - $this->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'") - ->expectsOutput("User's name is Test command") - ->expectsOutput('foo') - ->expectsOutput('xyz'); - - expect(DB::connection()->getDatabaseName())->toBe($originalDBName); + databaseConnectionSwitchedToDefault(); }); test('run commands works', function () { - $id = Tenant::create()->getTenantKey(); - - Artisan::call('tenants:migrate', ['--tenants' => [$id]]); - - $this->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'") - ->expectsOutput("User's name is Test command") - ->expectsOutput('foo') - ->expectsOutput('xyz'); + runCommandWorks(); }); test('install command works', function () { @@ -229,3 +178,33 @@ test('run command with array of tenants works', function () { ->expectsOutput('Tenant: ' . $tenantId1) ->expectsOutput('Tenant: ' . $tenantId2); }); + +function runCommandWorks(): void +{ + $id = Tenant::create()->getTenantKey(); + + Artisan::call('tenants:migrate', ['--tenants' => [$id]]); + + test()->artisan("tenants:run foo --tenants=$id --argument='a=foo' --option='b=bar' --option='c=xyz'") + ->expectsOutput("User's name is Test command") + ->expectsOutput('foo') + ->expectsOutput('xyz'); +} + +function databaseConnectionSwitchedToDefault() +{ + $originalDBName = DB::connection()->getDatabaseName(); + + Artisan::call('tenants:migrate'); + expect(DB::connection()->getDatabaseName())->toBe($originalDBName); + + Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]); + expect(DB::connection()->getDatabaseName())->toBe($originalDBName); + + Artisan::call('tenants:rollback'); + expect(DB::connection()->getDatabaseName())->toBe($originalDBName); + + runCommandWorks(); + + expect(DB::connection()->getDatabaseName())->toBe($originalDBName); +} \ No newline at end of file diff --git a/tests/SingleDatabaseTenancyTest.php b/tests/SingleDatabaseTenancyTest.php index 553c801f..b64ff340 100644 --- a/tests/SingleDatabaseTenancyTest.php +++ b/tests/SingleDatabaseTenancyTest.php @@ -37,94 +37,11 @@ beforeEach(function () { }); test('primary models are scoped to the current tenant', function () { - // acme context - tenancy()->initialize($acme = Tenant::create([ - 'id' => 'acme', - ])); - - $post = Post::create(['text' => 'Foo']); - - expect($post->tenant_id)->toBe('acme'); - expect($post->tenant->id)->toBe('acme'); - - $post = Post::first(); - - expect($post->tenant_id)->toBe('acme'); - expect($post->tenant->id)->toBe('acme'); - - // ====================================== - // foobar context - tenancy()->initialize($foobar = Tenant::create([ - 'id' => 'foobar', - ])); - - $post = Post::create(['text' => 'Bar']); - - expect($post->tenant_id)->toBe('foobar'); - expect($post->tenant->id)->toBe('foobar'); - - $post = Post::first(); - - expect($post->tenant_id)->toBe('foobar'); - expect($post->tenant->id)->toBe('foobar'); - - // ====================================== - // acme context again - - tenancy()->initialize($acme); - - $post = Post::first(); - expect($post->tenant_id)->toBe('acme'); - expect($post->tenant->id)->toBe('acme'); - - // Assert foobar models are inaccessible in acme context - expect(Post::count())->toBe(1); + primaryModelsScopedToCurrentTenant(); }); test('primary models are not scoped in the central context', function () { - // $this->primary_models_are_scoped_to_the_current_tenant(); - // acme context - tenancy()->initialize($acme = Tenant::create([ - 'id' => 'acme', - ])); - - $post = Post::create(['text' => 'Foo']); - - expect($post->tenant_id)->toBe('acme'); - expect($post->tenant->id)->toBe('acme'); - - $post = Post::first(); - - expect($post->tenant_id)->toBe('acme'); - expect($post->tenant->id)->toBe('acme'); - - // ====================================== - // foobar context - tenancy()->initialize($foobar = Tenant::create([ - 'id' => 'foobar', - ])); - - $post = Post::create(['text' => 'Bar']); - - expect($post->tenant_id)->toBe('foobar'); - expect($post->tenant->id)->toBe('foobar'); - - $post = Post::first(); - - expect($post->tenant_id)->toBe('foobar'); - expect($post->tenant->id)->toBe('foobar'); - - // ====================================== - // acme context again - - tenancy()->initialize($acme); - - $post = Post::first(); - expect($post->tenant_id)->toBe('acme'); - expect($post->tenant->id)->toBe('acme'); - - // Assert foobar models are inaccessible in acme context - expect(Post::count())->toBe(1); + primaryModelsScopedToCurrentTenant(); tenancy()->end(); @@ -132,54 +49,11 @@ test('primary models are not scoped in the central context', function () { }); test('secondary models are scoped to the current tenant when accessed via primary model', function () { - // acme context - tenancy()->initialize($acme = Tenant::create([ - 'id' => 'acme', - ])); - - $post = Post::create(['text' => 'Foo']); - $post->comments()->create(['text' => 'Comment text']); - - // ================ - // foobar context - tenancy()->initialize($foobar = Tenant::create([ - 'id' => 'foobar', - ])); - - $post = Post::create(['text' => 'Bar']); - $post->comments()->create(['text' => 'Comment text 2']); - - // ================ - // acme context again - tenancy()->initialize($acme); - expect(Post::count())->toBe(1); - expect(Post::first()->comments->count())->toBe(1); + secondaryModelsAreScopedToCurrentTenant(); }); test('secondary models are not scoped to the current tenant when accessed directly', function () { - // $this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model(); - // acme context - tenancy()->initialize($acme = Tenant::create([ - 'id' => 'acme', - ])); - - $post = Post::create(['text' => 'Foo']); - $post->comments()->create(['text' => 'Comment text']); - - // ================ - // foobar context - tenancy()->initialize($foobar = Tenant::create([ - 'id' => 'foobar', - ])); - - $post = Post::create(['text' => 'Bar']); - $post->comments()->create(['text' => 'Comment text 2']); - - // ================ - // acme context again - tenancy()->initialize($acme); - expect(Post::count())->toBe(1); - expect(Post::first()->comments->count())->toBe(1); + secondaryModelsAreScopedToCurrentTenant(); // We're in acme context expect(tenant('id'))->toBe('acme'); @@ -220,29 +94,7 @@ test('secondary models a r e scoped to the current tenant when accessed directly }); test('secondary models are not scoped in the central context', function () { - // $this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model(); - // acme context - tenancy()->initialize($acme = Tenant::create([ - 'id' => 'acme', - ])); - - $post = Post::create(['text' => 'Foo']); - $post->comments()->create(['text' => 'Comment text']); - - // ================ - // foobar context - tenancy()->initialize($foobar = Tenant::create([ - 'id' => 'foobar', - ])); - - $post = Post::create(['text' => 'Bar']); - $post->comments()->create(['text' => 'Comment text 2']); - - // ================ - // acme context again - tenancy()->initialize($acme); - expect(Post::count())->toBe(1); - expect(Post::first()->comments->count())->toBe(1); + secondaryModelsAreScopedToCurrentTenant(); tenancy()->end(); @@ -375,6 +227,78 @@ test('the model returned by the tenant helper has unique and exists validation r expect($existsFails)->toBeFalse(); }); +function primaryModelsScopedToCurrentTenant() +{ + // acme context + tenancy()->initialize($acme = Tenant::create([ + 'id' => 'acme', + ])); + + $post = Post::create(['text' => 'Foo']); + + expect($post->tenant_id)->toBe('acme'); + expect($post->tenant->id)->toBe('acme'); + + $post = Post::first(); + + expect($post->tenant_id)->toBe('acme'); + expect($post->tenant->id)->toBe('acme'); + + // ====================================== + // foobar context + tenancy()->initialize($foobar = Tenant::create([ + 'id' => 'foobar', + ])); + + $post = Post::create(['text' => 'Bar']); + + expect($post->tenant_id)->toBe('foobar'); + expect($post->tenant->id)->toBe('foobar'); + + $post = Post::first(); + + expect($post->tenant_id)->toBe('foobar'); + expect($post->tenant->id)->toBe('foobar'); + + // ====================================== + // acme context again + + tenancy()->initialize($acme); + + $post = Post::first(); + expect($post->tenant_id)->toBe('acme'); + expect($post->tenant->id)->toBe('acme'); + + // Assert foobar models are inaccessible in acme context + expect(Post::count())->toBe(1); +} + +function secondaryModelsAreScopedToCurrentTenant() +{ + // acme context + tenancy()->initialize($acme = Tenant::create([ + 'id' => 'acme', + ])); + + $post = Post::create(['text' => 'Foo']); + $post->comments()->create(['text' => 'Comment text']); + + // ================ + // foobar context + tenancy()->initialize($foobar = Tenant::create([ + 'id' => 'foobar', + ])); + + $post = Post::create(['text' => 'Bar']); + $post->comments()->create(['text' => 'Comment text 2']); + + // ================ + // acme context again + tenancy()->initialize($acme); + expect(Post::count())->toBe(1); + expect(Post::first()->comments->count())->toBe(1); +} + class Tenant extends TestTenant { use HasScopedValidationRules;