1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 11:44:04 +00:00

Added Default TenancyTestCase

This commit is contained in:
Jesper Jacobsen 2019-10-23 00:13:09 +02:00
parent d899dcfcce
commit f0e4d4a73e
2 changed files with 87 additions and 3 deletions

View file

@ -0,0 +1,77 @@
<?php
namespace Tests;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\TenantManager;
use Illuminate\Foundation\Testing\TestCase;
abstract class TenancyTestCase extends TestCase
{
use CreatesApplication;
public $createTenant = true;
/**
* Setup the test environment
* before each individual test
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
config(['database.default' => '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();
}
}

View file

@ -34,7 +34,7 @@ class Install extends Command
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', '--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'config', '--tag' => 'config',
]); ]);
$this->info('✔️ Created config/tenancy.php'); $this->info('✔️ Created config/tenancy.php.');
$newKernel = str_replace( $newKernel = str_replace(
'protected $middlewarePriority = [', 'protected $middlewarePriority = [',
@ -51,15 +51,22 @@ class Install extends Command
\Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,", $newKernel); \Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,", $newKernel);
file_put_contents(app_path('Http/Kernel.php'), $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'))) { 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')); 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 { } else {
$this->info('Found routes/tenant.php.'); $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->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.'); $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)) { if ($this->confirm('Do you wish to publish the migrations that create these tables?', true)) {