From f58c0045241ba90309184cf71a07a0b4195d6148 Mon Sep 17 00:00:00 2001 From: Kennedy Tedesco Date: Mon, 2 Apr 2018 09:58:45 -0300 Subject: [PATCH] [5.6] Execute an Artisan command using either its name or class (#23764) * [5.6] Execute an Artisan command using either it's name or class * Add assertions * Use is_subclass_of() --- src/Illuminate/Console/Application.php | 8 ++- .../Console/ConsoleApplicationTest.php | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/Integration/Console/ConsoleApplicationTest.php diff --git a/src/Illuminate/Console/Application.php b/src/Illuminate/Console/Application.php index 9b9b517b79eb..07549f8920e9 100755 --- a/src/Illuminate/Console/Application.php +++ b/src/Illuminate/Console/Application.php @@ -168,13 +168,17 @@ public static function forgetBootstrappers() */ public function call($command, array $parameters = [], $outputBuffer = null) { - $parameters = collect($parameters)->prepend($command); + if (is_subclass_of($command, SymfonyCommand::class)) { + $command = $this->laravel->make($command)->getName(); + } + + array_unshift($parameters, $command); $this->lastOutput = $outputBuffer ?: new BufferedOutput; $this->setCatchExceptions(false); - $result = $this->run(new ArrayInput($parameters->toArray()), $this->lastOutput); + $result = $this->run(new ArrayInput($parameters), $this->lastOutput); $this->setCatchExceptions(true); diff --git a/tests/Integration/Console/ConsoleApplicationTest.php b/tests/Integration/Console/ConsoleApplicationTest.php new file mode 100644 index 000000000000..02a0d72b8ad7 --- /dev/null +++ b/tests/Integration/Console/ConsoleApplicationTest.php @@ -0,0 +1,53 @@ +app[Kernel::class]->registerCommand(new FooCommandStub); + } + + public function test_artisan_call_using_command_name() + { + $exitCode = $this->artisan('foo:bar', [ + 'id' => 1, + ]); + + $this->assertEquals($exitCode, 0); + } + + public function test_artisan_call_using_command_class() + { + $exitCode = $this->artisan(FooCommandStub::class, [ + 'id' => 1, + ]); + + $this->assertEquals($exitCode, 0); + } + + /** + * @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException + */ + public function test_artisan_call_invalid_command_name() + { + $this->artisan('foo:bars'); + } +} + +class FooCommandStub extends Command +{ + protected $signature = 'foo:bar {id}'; + + public function handle() + { + // + } +}