mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:54:03 +00:00
[4.x] Improve tenants:run command to execute Input\Output commands (#923)
* run command useable for questions asking commands * move console classes to Console directory * fix styling * Update src/Commands/Run.php Co-authored-by: Samuel Štancl <samuel@archte.ch> * remove tenant migration line * assert command executed in tenant context * improve test * cleanup code * Update CommandsTest.php * remove irrelevant assertions Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
3bf2c39e1a
commit
409190fae1
7 changed files with 98 additions and 9 deletions
|
|
@ -5,7 +5,9 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Commands;
|
namespace Stancl\Tenancy\Commands;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Contracts\Console\Kernel;
|
||||||
|
use Symfony\Component\Console\Input\ArgvInput;
|
||||||
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||||
|
|
||||||
class Run extends Command
|
class Run extends Command
|
||||||
{
|
{
|
||||||
|
|
@ -29,12 +31,27 @@ class Run extends Command
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
$argvInput = $this->ArgvInput();
|
||||||
|
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) use ($argvInput) {
|
||||||
$this->line("Tenant: {$tenant->getTenantKey()}");
|
$this->line("Tenant: {$tenant->getTenantKey()}");
|
||||||
|
|
||||||
Artisan::call($this->argument('commandname'));
|
$this->getLaravel()
|
||||||
$this->comment('Command output:');
|
->make(Kernel::class)
|
||||||
$this->info(Artisan::output());
|
->handle($argvInput, new ConsoleOutput);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get command as ArgvInput instance.
|
||||||
|
*/
|
||||||
|
protected function ArgvInput(): ArgvInput
|
||||||
|
{
|
||||||
|
// Convert string command to array
|
||||||
|
$subCommand = explode(' ', $this->argument('commandname'));
|
||||||
|
|
||||||
|
// Add "artisan" as first parameter because ArgvInput expects "artisan" as first parameter and later removes it
|
||||||
|
array_unshift($subCommand, 'artisan');
|
||||||
|
|
||||||
|
return new ArgvInput($subCommand);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\User;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
|
@ -179,6 +180,29 @@ test('run command with array of tenants works', function () {
|
||||||
->expectsOutput('Tenant: ' . $tenantId2);
|
->expectsOutput('Tenant: ' . $tenantId2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('run command works when sub command asks questions and accepts arguments', function () {
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
$id = $tenant->getTenantKey();
|
||||||
|
|
||||||
|
Artisan::call('tenants:migrate');
|
||||||
|
|
||||||
|
pest()->artisan("tenants:run --tenants=$id 'user:addwithname Abrar' ")
|
||||||
|
->expectsQuestion('What is your email?', 'email@localhost')
|
||||||
|
->expectsOutput("Tenant: $id")
|
||||||
|
->expectsOutput("User created: Abrar(email@localhost)");
|
||||||
|
|
||||||
|
// Assert we are in central context
|
||||||
|
expect(tenancy()->initialized)->toBeFalse();
|
||||||
|
|
||||||
|
// Assert user was created in tenant context
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
$user = User::first();
|
||||||
|
|
||||||
|
// Assert user is same as provided using the command
|
||||||
|
expect($user->name)->toBe('Abrar');
|
||||||
|
expect($user->email)->toBe('email@localhost');
|
||||||
|
});
|
||||||
|
|
||||||
// todo@tests
|
// todo@tests
|
||||||
function runCommandWorks(): void
|
function runCommandWorks(): void
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,13 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests\Etc;
|
namespace Stancl\Tenancy\Tests\Etc\Console;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||||
use Stancl\Tenancy\Concerns\TenantAwareCommand;
|
use Stancl\Tenancy\Concerns\TenantAwareCommand;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\User;
|
||||||
|
|
||||||
class AddUserCommand extends Command
|
class AddUserCommand extends Command
|
||||||
{
|
{
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests\Etc;
|
namespace Stancl\Tenancy\Tests\Etc\Console;
|
||||||
|
|
||||||
use Orchestra\Testbench\Foundation\Console\Kernel;
|
use Orchestra\Testbench\Foundation\Console\Kernel;
|
||||||
|
|
||||||
|
|
@ -10,6 +10,7 @@ class ConsoleKernel extends Kernel
|
||||||
{
|
{
|
||||||
protected $commands = [
|
protected $commands = [
|
||||||
ExampleCommand::class,
|
ExampleCommand::class,
|
||||||
|
ExampleQuestionCommand::class,
|
||||||
AddUserCommand::class,
|
AddUserCommand::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests\Etc;
|
namespace Stancl\Tenancy\Tests\Etc\Console;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
46
tests/Etc/Console/ExampleQuestionCommand.php
Normal file
46
tests/Etc/Console/ExampleQuestionCommand.php
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\Etc\Console;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\User;
|
||||||
|
|
||||||
|
class ExampleQuestionCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'user:addwithname {name}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Command description';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$email = $this->ask('What is your email?');
|
||||||
|
|
||||||
|
User::create([
|
||||||
|
'name' => $this->argument('name'),
|
||||||
|
'email' => $email,
|
||||||
|
'email_verified_at' => now(),
|
||||||
|
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
|
||||||
|
'remember_token' => Str::random(10),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->line("User created: ". $this->argument('name') . "($email)");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -142,7 +142,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
*/
|
*/
|
||||||
protected function resolveApplicationConsoleKernel($app)
|
protected function resolveApplicationConsoleKernel($app)
|
||||||
{
|
{
|
||||||
$app->singleton('Illuminate\Contracts\Console\Kernel', Etc\ConsoleKernel::class);
|
$app->singleton('Illuminate\Contracts\Console\Kernel', Etc\Console\ConsoleKernel::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function randomString(int $length = 10)
|
public function randomString(int $length = 10)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue