mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 19:14:03 +00:00
run command useable for questions asking commands
This commit is contained in:
parent
ca2eefa30a
commit
50d903feb8
4 changed files with 70 additions and 5 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ class ConsoleKernel extends Kernel
|
|||
{
|
||||
protected $commands = [
|
||||
ExampleCommand::class,
|
||||
ExampleQuestionCommand::class,
|
||||
AddUserCommand::class,
|
||||
];
|
||||
}
|
||||
|
|
|
|||
34
tests/Etc/ExampleQuestionCommand.php
Normal file
34
tests/Etc/ExampleQuestionCommand.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests\Etc;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class ExampleQuestionCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'age:ask {name}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Command description';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$age = $this->ask('What is your age?');
|
||||
|
||||
$this->line($this->argument('name') . "'s age is $age.");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue