1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-14 17:04:03 +00:00

Rewrite old tests

This commit is contained in:
Samuel Štancl 2020-05-12 23:22:40 +02:00
parent 64383b4c56
commit 89936187ce
71 changed files with 698 additions and 3203 deletions

View file

@ -1,47 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
use Stancl\Tenancy\Tenant;
class CreateTenant extends Command
{
protected $signature = 'tenants:create
{--d|domain=* : The tenant\'s domains.}
{data?* : The tenant\'s data. Separate keys and values by `=`, e.g. `plan=free`.}';
protected $description = 'Create a tenant.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tenant = Tenant::new()
->withDomains($this->getDomains())
->withData($this->getData())
->save();
$this->info($tenant->id);
}
public function getDomains(): array
{
return $this->option('domain');
}
public function getData(): array
{
return array_reduce($this->argument('data'), function ($data, $pair) {
[$key, $value] = explode('=', $pair, 2);
$data[$key] = $value;
return $data;
}, []);
}
}

View file

@ -36,17 +36,6 @@ class Install extends Command
]);
$this->info('✔️ Created config/tenancy.php');
$newKernel = $this->setMiddlewarePriority();
$newKernel = str_replace("'web' => [", "'web' => [
\Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,", $newKernel);
$newKernel = str_replace("'api' => [", "'api' => [
\Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,", $newKernel);
file_put_contents(app_path('Http/Kernel.php'), $newKernel);
$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');
@ -54,15 +43,13 @@ class Install extends Command
$this->info('Found routes/tenant.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)) {
$this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'migrations',
]);
$this->info('✔️ Created migrations. Remember to run [php artisan migrate]!');
}
// todo tenancy SP stub
$this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'migrations',
]);
$this->info('✔️ Created migrations. Remember to run [php artisan migrate]!');
if (! is_dir(database_path('migrations/tenant'))) {
mkdir(database_path('migrations/tenant'));
@ -71,34 +58,4 @@ class Install extends Command
$this->comment('✨️ stancl/tenancy installed successfully.');
}
protected function setMiddlewarePriority(): string
{
if (app()->version()[0] === '6') {
return str_replace(
'protected $middlewarePriority = [',
"protected \$middlewarePriority = [
\Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,
\Stancl\Tenancy\Middleware\InitializeTenancy::class,",
file_get_contents(app_path('Http/Kernel.php'))
);
} else {
return str_replace(
"];\n}",
"];\n\n protected \$middlewarePriority = [
\Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,
\Stancl\Tenancy\Middleware\InitializeTenancy::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
}",
file_get_contents(app_path('Http/Kernel.php'))
);
}
}
}

View file

@ -57,20 +57,13 @@ class Migrate extends MigrateCommand
return;
}
tenancy()
->query()
->when($this->option('tenants'), function ($query) {
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
})
->each(function (TenantWithDatabase $tenant) {
$this->line("Tenant: {$tenant['id']}");
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
// Migrate
parent::handle();
$tenant->run(function () {
// Migrate
parent::handle();
});
event(new DatabaseMigrated($tenant));
});
event(new DatabaseMigrated($tenant));
});
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Traits\DealsWithMigrations;
use Stancl\Tenancy\Traits\HasATenantsOption;
@ -33,22 +34,18 @@ final class MigrateFresh extends Command
*/
public function handle()
{
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant->id}");
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->info('Dropping tables.');
$this->call('db:wipe', array_filter([
'--database' => 'tenant',
'--force' => true,
]));
$tenant->run(function ($tenant) {
$this->info('Dropping tables.');
$this->call('db:wipe', array_filter([
'--database' => 'tenant',
'--force' => true,
]));
$this->info('Migrating.');
$this->callSilent('tenants:migrate', [
'--tenants' => [$tenant->id],
'--force' => true,
]);
});
$this->info('Migrating.');
$this->callSilent('tenants:migrate', [
'--tenants' => [$tenant->id],
'--force' => true,
]);
});
$this->info('Done.');

View file

@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Console\Migrations\RollbackCommand;
use Illuminate\Database\Migrations\Migrator;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\DatabaseManager;
use Stancl\Tenancy\Traits\DealsWithMigrations;
use Stancl\Tenancy\Traits\HasATenantsOption;
@ -55,13 +56,13 @@ class Rollback extends RollbackCommand
return;
}
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
$tenant->run(function () {
// Rollback
parent::handle();
});
// Rollback
parent::handle();
// todo DatabaseRolledBack event
});
}
}

View file

@ -32,8 +32,7 @@ class Run extends Command
*/
public function handle()
{
$originalTenant = tenancy()->getTenant();
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
tenancy()->initialize($tenant);
@ -54,12 +53,6 @@ class Run extends Command
// Run command
$this->call($this->argument('commandname'), array_merge($arguments, $options));
tenancy()->endTenancy();
});
if ($originalTenant) {
tenancy()->initialize($originalTenant);
}
}
}

View file

@ -55,18 +55,11 @@ class Seed extends SeedCommand
return;
}
tenancy()
->query()
->when($this->option('tenants'), function ($query) {
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
})
->each(function (TenantWithDatabase $tenant) {
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
$tenant->run(function () {
// Seed
parent::handle();
});
// Seed
parent::handle();
event(new DatabaseSeeded($tenant));
});

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
use Stancl\Tenancy\Contracts\Tenant;
class TenantList extends Command
{
@ -30,8 +31,14 @@ class TenantList extends Command
public function handle()
{
$this->info('Listing all tenants.');
tenancy()->all()->each(function ($tenant) {
$this->line("[Tenant] id: {$tenant['id']} @ " . implode('; ', $tenant->domains));
tenancy()
->query()
->when($this->option('tenants'), function ($query) {
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
})
->cursor()
->each(function (Tenant $tenant) {
$this->line("[Tenant] id: {$tenant['id']} @ " . implode('; ', $tenant->domains ?? []));
});
}
}