|
6 | 6 | use Illuminate\Console\Command;
|
7 | 7 | use Illuminate\Contracts\Events\Dispatcher;
|
8 | 8 | use Illuminate\Contracts\Foundation\Application as ApplicationContract;
|
| 9 | +use Illuminate\Events\Dispatcher as EventsDispatcher; |
| 10 | +use Illuminate\Foundation\Application as FoundationApplication; |
9 | 11 | use Illuminate\Tests\Console\Fixtures\FakeCommandWithInputPrompting;
|
10 | 12 | use Mockery as m;
|
11 | 13 | use PHPUnit\Framework\TestCase;
|
| 14 | +use Symfony\Component\Console\Attribute\AsCommand; |
12 | 15 | use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
| 16 | +use Symfony\Component\Console\Exception\CommandNotFoundException; |
| 17 | +use Throwable; |
13 | 18 |
|
14 | 19 | class ConsoleApplicationTest extends TestCase
|
15 | 20 | {
|
@@ -51,6 +56,68 @@ public function testResolveAddsCommandViaApplicationResolution()
|
51 | 56 | $this->assertEquals($command, $result);
|
52 | 57 | }
|
53 | 58 |
|
| 59 | + public function testResolvingCommandsWithAliasViaAttribute() |
| 60 | + { |
| 61 | + $container = new FoundationApplication(); |
| 62 | + $app = new Application($container, new EventsDispatcher($container), $container->version()); |
| 63 | + $app->resolve(CommandWithAliasViaAttribute::class); |
| 64 | + $app->setContainerCommandLoader(); |
| 65 | + |
| 66 | + $this->assertInstanceOf(CommandWithAliasViaAttribute::class, $app->get('command-name')); |
| 67 | + $this->assertInstanceOf(CommandWithAliasViaAttribute::class, $app->get('command-alias')); |
| 68 | + $this->assertArrayHasKey('command-name', $app->all()); |
| 69 | + $this->assertArrayHasKey('command-alias', $app->all()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testResolvingCommandsWithAliasViaProperty() |
| 73 | + { |
| 74 | + $container = new FoundationApplication(); |
| 75 | + $app = new Application($container, new EventsDispatcher($container), $container->version()); |
| 76 | + $app->resolve(CommandWithAliasViaProperty::class); |
| 77 | + $app->setContainerCommandLoader(); |
| 78 | + |
| 79 | + $this->assertInstanceOf(CommandWithAliasViaProperty::class, $app->get('command-name')); |
| 80 | + $this->assertInstanceOf(CommandWithAliasViaProperty::class, $app->get('command-alias')); |
| 81 | + $this->assertArrayHasKey('command-name', $app->all()); |
| 82 | + $this->assertArrayHasKey('command-alias', $app->all()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testResolvingCommandsWithNoAliasViaAttribute() |
| 86 | + { |
| 87 | + $container = new FoundationApplication(); |
| 88 | + $app = new Application($container, new EventsDispatcher($container), $container->version()); |
| 89 | + $app->resolve(CommandWithNoAliasViaAttribute::class); |
| 90 | + $app->setContainerCommandLoader(); |
| 91 | + |
| 92 | + $this->assertInstanceOf(CommandWithNoAliasViaAttribute::class, $app->get('command-name')); |
| 93 | + try { |
| 94 | + $app->get('command-alias'); |
| 95 | + $this->fail(); |
| 96 | + } catch (Throwable $e) { |
| 97 | + $this->assertInstanceOf(CommandNotFoundException::class, $e); |
| 98 | + } |
| 99 | + $this->assertArrayHasKey('command-name', $app->all()); |
| 100 | + $this->assertArrayNotHasKey('command-alias', $app->all()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testResolvingCommandsWithNoAliasViaProperty() |
| 104 | + { |
| 105 | + $container = new FoundationApplication(); |
| 106 | + $app = new Application($container, new EventsDispatcher($container), $container->version()); |
| 107 | + $app->resolve(CommandWithNoAliasViaProperty::class); |
| 108 | + $app->setContainerCommandLoader(); |
| 109 | + |
| 110 | + $this->assertInstanceOf(CommandWithNoAliasViaProperty::class, $app->get('command-name')); |
| 111 | + try { |
| 112 | + $app->get('command-alias'); |
| 113 | + $this->fail(); |
| 114 | + } catch (Throwable $e) { |
| 115 | + $this->assertInstanceOf(CommandNotFoundException::class, $e); |
| 116 | + } |
| 117 | + $this->assertArrayHasKey('command-name', $app->all()); |
| 118 | + $this->assertArrayNotHasKey('command-alias', $app->all()); |
| 119 | + } |
| 120 | + |
54 | 121 | public function testCallFullyStringCommandLine()
|
55 | 122 | {
|
56 | 123 | $app = new Application(
|
@@ -124,3 +191,25 @@ protected function getMockConsole(array $methods)
|
124 | 191 | ])->getMock();
|
125 | 192 | }
|
126 | 193 | }
|
| 194 | + |
| 195 | +#[AsCommand('command-name')] |
| 196 | +class CommandWithNoAliasViaAttribute extends Command |
| 197 | +{ |
| 198 | + // |
| 199 | +} |
| 200 | +#[AsCommand('command-name', aliases: ['command-alias'])] |
| 201 | +class CommandWithAliasViaAttribute extends Command |
| 202 | +{ |
| 203 | + // |
| 204 | +} |
| 205 | + |
| 206 | +class CommandWithNoAliasViaProperty extends Command |
| 207 | +{ |
| 208 | + public $name = 'command-name'; |
| 209 | +} |
| 210 | + |
| 211 | +class CommandWithAliasViaProperty extends Command |
| 212 | +{ |
| 213 | + public $name = 'command-name'; |
| 214 | + public $aliases = ['command-alias']; |
| 215 | +} |
0 commit comments