Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fix command alias registration and usage. #50617

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/Console/CacheTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\MigrationGeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'make:cache-table')]
#[AsCommand(name: 'make:cache-table', aliases: ['cache:table'])]
class CacheTableCommand extends MigrationGeneratorCommand
{
/**
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Illuminate\Console\Scheduling;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'schedule:clear-cache')]
class ScheduleClearCacheCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/DbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Console\Command;
use Illuminate\Support\ConfigurationUrlParser;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\Process;
use UnexpectedValueException;

#[AsCommand(name: 'db')]
class DbCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/Migrations/FreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\DatabaseRefreshed;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'migrate:fresh')]
class FreshCommand extends Command
{
use ConfirmableTrait;
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/Migrations/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Console\Command;
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'migrate:install')]
class InstallCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/Migrations/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use Illuminate\Database\SQLiteDatabaseDoesNotExistException;
use Illuminate\Database\SqlServerConnection;
use PDOException;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;

use function Laravel\Prompts\confirm;

#[AsCommand(name: 'migrate')]
class MigrateCommand extends BaseCommand implements Isolatable
{
use ConfirmableTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Database\Migrations\MigrationCreator;
use Illuminate\Support\Composer;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'make:migration')]
class MigrateMakeCommand extends BaseCommand implements PromptsForMissingInput
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/Migrations/RefreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\DatabaseRefreshed;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'migrate:refresh')]
class RefreshCommand extends Command
{
use ConfirmableTrait;
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/Migrations/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\Migrations\Migrator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'migrate:reset')]
class ResetCommand extends BaseCommand
{
use ConfirmableTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\Migrations\Migrator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand('migrate:rollback')]
class RollbackCommand extends BaseCommand
{
use ConfirmableTrait;
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/Migrations/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'migrate:status')]
class StatusCommand extends BaseCommand
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use Illuminate\Database\Events\ModelsPruned;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Finder\Finder;

#[AsCommand(name: 'model:prune')]
class PruneCommand extends Command
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\MigrationGeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'make:notifications-table')]
#[AsCommand(name: 'make:notifications-table', aliases: ['notifications:table'])]
class NotificationTableCommand extends MigrationGeneratorCommand
{
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Console/BatchesTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use function Illuminate\Filesystem\join_paths;

#[AsCommand(name: 'make:queue-batches-table')]
#[AsCommand(name: 'make:queue-batches-table', aliases: ['queue:batches-table'])]
class BatchesTableCommand extends MigrationGeneratorCommand
{
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Console/FailedTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use function Illuminate\Filesystem\join_paths;

#[AsCommand(name: 'make:queue-failed-table')]
#[AsCommand(name: 'make:queue-failed-table', aliases: ['queue:failed-table'])]
class FailedTableCommand extends MigrationGeneratorCommand
{
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Console/TableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use function Illuminate\Filesystem\join_paths;

#[AsCommand(name: 'make:queue-table')]
#[AsCommand(name: 'make:queue-table', aliases: ['queue:table'])]
class TableCommand extends MigrationGeneratorCommand
{
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Session/Console/SessionTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use function Illuminate\Filesystem\join_paths;

#[AsCommand(name: 'make:session-table')]
#[AsCommand(name: 'make:session-table', aliases: ['session:table'])]
class SessionTableCommand extends MigrationGeneratorCommand
{
/**
Expand Down
89 changes: 89 additions & 0 deletions tests/Console/ConsoleApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
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\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 +56,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 +191,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'];
}