diff --git a/assets/tenancy_test_case.php.stub b/assets/tenancy_test_case.php.stub new file mode 100644 index 00000000..f9a35f3f --- /dev/null +++ b/assets/tenancy_test_case.php.stub @@ -0,0 +1,77 @@ + 'sqlite']); + config(['tenancy.database.suffix' => '.sqlite']); + + $this->artisan('migrate'); + + if ($this->createTenant) { + $tenant = $this->createTenant(); + $this->initTenancy($tenant); + } + } + + /** + * Create a new Tenant + * + * @param array $domains + * @return Tenant + */ + protected function createTenant($domains = ['test.localhost']): Tenant + { + return Tenant::new() + ->withDomains($domains) + ->withData(['name' => 'test', 'host' => 'test']) + ->save(); + } + + /** + * Change to the new Tenant + * + * @param Tenant $tenant + * @return TenantManager + */ + protected function initTenancy(Tenant $tenant): TenantManager + { + return tenancy()->initialize($tenant); + } + + + /** + * Destroy any tenant and their database + * between each individual test-case + * to ensure data separation + * + * @return void + */ + protected function tearDown(): void + { + config(['tenancy.delete_database_after_tenant_deletion' => true]); + + tenancy()->all()->each->delete(); + + parent::tearDown(); + } +} diff --git a/src/Commands/Install.php b/src/Commands/Install.php index c1012d94..18be063e 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -34,7 +34,7 @@ class Install extends Command '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', '--tag' => 'config', ]); - $this->info('✔️ Created config/tenancy.php'); + $this->info('✔️ Created config/tenancy.php.'); $newKernel = str_replace( 'protected $middlewarePriority = [', @@ -51,15 +51,22 @@ class Install extends Command \Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,", $newKernel); file_put_contents(app_path('Http/Kernel.php'), $newKernel); - $this->info('✔️ Set middleware priority'); + $this->info('✔️ Set middleware priority.'); if (! file_exists(base_path('routes/tenant.php'))) { file_put_contents(base_path('routes/tenant.php'), file_get_contents(__DIR__ . '/../../assets/tenant_routes.php.stub')); - $this->info('✔️ Created routes/tenant.php'); + $this->info('✔️ Created routes/tenant.php.'); } else { $this->info('Found routes/tenant.php.'); } + if (! file_exists(base_path('tests/TenancyTestCase.php'))) { + file_put_contents(base_path('tests/TenancyTestCase.php'), file_get_contents(__DIR__ . '/../../assets/tenancy_test_case.php.stub')); + $this->info('✔️ Created tests/TenancyTestCase.php.'); + } else { + $this->info('Found tests/TenancyTestCase.php.'); + } + $this->line(''); $this->line('This package lets you store data about tenants either in Redis or in a relational database like MySQL. To store data about tenants in a relational database, you need a few database tables.'); if ($this->confirm('Do you wish to publish the migrations that create these tables?', true)) {