Skip to content

Commit

Permalink
[5.6] Execute an Artisan command using either its name or class (#23764)
Browse files Browse the repository at this point in the history
* [5.6] Execute an Artisan command using either it's name or class

* Add assertions

* Use is_subclass_of()
  • Loading branch information
KennedyTedesco authored and taylorotwell committed Apr 2, 2018
1 parent f44c3e7 commit f58c004
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
53 changes: 53 additions & 0 deletions tests/Integration/Console/ConsoleApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Illuminate\Tests\Integration\Console;

use Illuminate\Console\Command;
use Orchestra\Testbench\TestCase;
use Illuminate\Contracts\Console\Kernel;

class ConsoleApplicationTest extends TestCase
{
protected function setUp()
{
parent::setUp();

$this->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()
{
//
}
}

0 comments on commit f58c004

Please sign in to comment.