Skip to content

Commit

Permalink
Fix command alias parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Mar 18, 2024
1 parent ae2f941 commit 214f8b4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ protected function addToParent(SymfonyCommand $command)
public function resolve($command)
{
if (is_subclass_of($command, SymfonyCommand::class) && ($commandName = $command::getDefaultName())) {
$this->commandMap[$commandName] = $command;
foreach (explode('|', $commandName) as $name) {
$this->commandMap[$name] = $command;
}

return null;
}
Expand Down
90 changes: 90 additions & 0 deletions tests/Console/ConsoleApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Events\Dispatcher as EventsDispatcher;
use Illuminate\Foundation\Application as FoundationApplication;
use Illuminate\Foundation\Console\Kernel;
use Illuminate\Tests\Console\Fixtures\FakeCommandWithInputPrompting;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Throwable;

class ConsoleApplicationTest extends TestCase
{
Expand Down Expand Up @@ -51,6 +57,68 @@ public function testResolveAddsCommandViaApplicationResolution()
$this->assertEquals($command, $result);
}

public function testResolvingCommandsWithAliasViaAttribute()
{
$container = new FoundationApplication();
$app = new Application($container, new EventsDispatcher($container), $container->version());
$app->resolve(CommandWithAliasViaAttribute::class);
$app->setContainerCommandLoader();

$this->assertInstanceOf(CommandWithAliasViaAttribute::class, $app->get('command-name'));
$this->assertInstanceOf(CommandWithAliasViaAttribute::class, $app->get('command-alias'));
$this->assertArrayHasKey('command-name', $app->all());
$this->assertArrayHasKey('command-alias', $app->all());
}

public function testResolvingCommandsWithAliasViaProperty()
{
$container = new FoundationApplication();
$app = new Application($container, new EventsDispatcher($container), $container->version());
$app->resolve(CommandWithAliasViaProperty::class);
$app->setContainerCommandLoader();

$this->assertInstanceOf(CommandWithAliasViaProperty::class, $app->get('command-name'));
$this->assertInstanceOf(CommandWithAliasViaProperty::class, $app->get('command-alias'));
$this->assertArrayHasKey('command-name', $app->all());
$this->assertArrayHasKey('command-alias', $app->all());
}

public function testResolvingCommandsWithNoAliasViaAttribute()
{
$container = new FoundationApplication();
$app = new Application($container, new EventsDispatcher($container), $container->version());
$app->resolve(CommandWithNoAliasViaAttribute::class);
$app->setContainerCommandLoader();

$this->assertInstanceOf(CommandWithNoAliasViaAttribute::class, $app->get('command-name'));
try {
$app->get('command-alias');
$this->fail();
} catch (Throwable $e) {
$this->assertInstanceOf(CommandNotFoundException::class, $e);
}
$this->assertArrayHasKey('command-name', $app->all());
$this->assertArrayNotHasKey('command-alias', $app->all());
}

public function testResolvingCommandsWithNoAliasViaProperty()
{
$container = new FoundationApplication();
$app = new Application($container, new EventsDispatcher($container), $container->version());
$app->resolve(CommandWithNoAliasViaProperty::class);
$app->setContainerCommandLoader();

$this->assertInstanceOf(CommandWithNoAliasViaProperty::class, $app->get('command-name'));
try {
$app->get('command-alias');
$this->fail();
} catch (Throwable $e) {
$this->assertInstanceOf(CommandNotFoundException::class, $e);
}
$this->assertArrayHasKey('command-name', $app->all());
$this->assertArrayNotHasKey('command-alias', $app->all());
}

public function testCallFullyStringCommandLine()
{
$app = new Application(
Expand Down Expand Up @@ -124,3 +192,25 @@ protected function getMockConsole(array $methods)
])->getMock();
}
}

#[AsCommand('command-name')]
class CommandWithNoAliasViaAttribute extends Command
{
//
}
#[AsCommand('command-name', aliases: ['command-alias'])]
class CommandWithAliasViaAttribute extends Command
{
//
}

class CommandWithNoAliasViaProperty extends Command
{
public $name = 'command-name';
}

class CommandWithAliasViaProperty extends Command
{
public $name = 'command-name';
public $aliases = ['command-alias'];
}

0 comments on commit 214f8b4

Please sign in to comment.