From 50d903feb818f723187dd2910db8a43e43d5c0a7 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 26 Aug 2022 15:32:37 +0500 Subject: [PATCH] run command useable for questions asking commands --- src/Commands/Run.php | 29 ++++++++++++++++++++---- tests/CommandsTest.php | 11 +++++++++ tests/Etc/ConsoleKernel.php | 1 + tests/Etc/ExampleQuestionCommand.php | 34 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 tests/Etc/ExampleQuestionCommand.php diff --git a/src/Commands/Run.php b/src/Commands/Run.php index 075f9116..6a357b65 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace Stancl\Tenancy\Commands; 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 { @@ -29,12 +31,29 @@ class Run extends Command */ 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()}"); - Artisan::call($this->argument('commandname')); - $this->comment('Command output:'); - $this->info(Artisan::output()); + $this->getLaravel() + ->make(Kernel::class) + ->handle($argvInput, new ConsoleOutput); }); } + + /** + * Get command as ArgvInput instance + * + * @return ArgvInput + */ + protected function ArgvInput(): ArgvInput + { + // Convert string command to array + $subCommand = explode(' ', $this->argument('commandname')); + + // Add "artisan" as first parameter because ArgvInput expect "artisan" as first parameter and later remove it + array_unshift($subCommand , 'artisan'); + + return new ArgvInput($subCommand); + } } diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 7415b74f..ca6bde08 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -179,6 +179,17 @@ test('run command with array of tenants works', function () { ->expectsOutput('Tenant: ' . $tenantId2); }); +test('run command works when sub command asks question and accepts argument', function () { + $id = Tenant::create()->getTenantKey(); + + Artisan::call('tenants:migrate', ['--tenants' => [$id]]); + + pest()->artisan("tenants:run --tenants=$id 'age:ask Abrar' ") + ->expectsQuestion('What is your age?', 22) + ->expectsOutput("Tenant: $id") + ->expectsOutput("Abrar's age is 22."); +}); + // todo@tests function runCommandWorks(): void { diff --git a/tests/Etc/ConsoleKernel.php b/tests/Etc/ConsoleKernel.php index a548f113..2dc8d87e 100644 --- a/tests/Etc/ConsoleKernel.php +++ b/tests/Etc/ConsoleKernel.php @@ -10,6 +10,7 @@ class ConsoleKernel extends Kernel { protected $commands = [ ExampleCommand::class, + ExampleQuestionCommand::class, AddUserCommand::class, ]; } diff --git a/tests/Etc/ExampleQuestionCommand.php b/tests/Etc/ExampleQuestionCommand.php new file mode 100644 index 00000000..36e5d2ed --- /dev/null +++ b/tests/Etc/ExampleQuestionCommand.php @@ -0,0 +1,34 @@ +ask('What is your age?'); + + $this->line($this->argument('name') . "'s age is $age."); + } +}