|
16 | 16 | use Symfony\Component\Console\Attribute\AsCommand;
|
17 | 17 | use Symfony\Component\Console\Command\Command;
|
18 | 18 | use Symfony\Component\Console\Command\HelpCommand;
|
| 19 | +use Symfony\Component\Console\Command\InvokableCommand; |
19 | 20 | use Symfony\Component\Console\Command\LazyCommand;
|
20 | 21 | use Symfony\Component\Console\Command\SignalableCommandInterface;
|
21 | 22 | use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
|
|
28 | 29 | use Symfony\Component\Console\Event\ConsoleSignalEvent;
|
29 | 30 | use Symfony\Component\Console\Event\ConsoleTerminateEvent;
|
30 | 31 | use Symfony\Component\Console\Exception\CommandNotFoundException;
|
| 32 | +use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 33 | +use Symfony\Component\Console\Exception\LogicException; |
31 | 34 | use Symfony\Component\Console\Exception\NamespaceNotFoundException;
|
32 | 35 | use Symfony\Component\Console\Helper\FormatterHelper;
|
33 | 36 | use Symfony\Component\Console\Helper\HelperSet;
|
@@ -239,6 +242,59 @@ public function testAddCommandWithEmptyConstructor()
|
239 | 242 | (new Application())->add(new \Foo5Command());
|
240 | 243 | }
|
241 | 244 |
|
| 245 | + public function testAddCommandWithExtendedCommand() |
| 246 | + { |
| 247 | + $application = new Application(); |
| 248 | + $application->add($foo = new \FooCommand()); |
| 249 | + $commands = $application->all(); |
| 250 | + |
| 251 | + $this->assertEquals($foo, $commands['foo:bar']); |
| 252 | + } |
| 253 | + |
| 254 | + public function testAddCommandWithInvokableCommand() |
| 255 | + { |
| 256 | + $application = new Application(); |
| 257 | + $application->addCommand($foo = new InvokableTestCommand()); |
| 258 | + $commands = $application->all(); |
| 259 | + |
| 260 | + $this->assertInstanceOf(Command::class, $command = $commands['invokable']); |
| 261 | + $this->assertEquals(new InvokableCommand($command, $foo), (new \ReflectionObject($command))->getProperty('code')->getValue($command)); |
| 262 | + } |
| 263 | + |
| 264 | + public function testAddCommandWithInvokableExtendedCommand() |
| 265 | + { |
| 266 | + $application = new Application(); |
| 267 | + $application->addCommand($foo = new InvokableExtendedTestCommand()); |
| 268 | + $commands = $application->all(); |
| 269 | + |
| 270 | + $this->assertEquals($foo, $commands['invokable-extended']); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * @dataProvider provideInvalidInvokableCommands |
| 275 | + */ |
| 276 | + public function testAddCommandThrowsExceptionOnInvalidCommand(callable $command, string $expectedException, string $expectedExceptionMessage) |
| 277 | + { |
| 278 | + $application = new Application(); |
| 279 | + |
| 280 | + $this->expectException($expectedException); |
| 281 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 282 | + |
| 283 | + $application->addCommand($command); |
| 284 | + } |
| 285 | + |
| 286 | + public static function provideInvalidInvokableCommands(): iterable |
| 287 | + { |
| 288 | + yield 'a function' => ['strlen', InvalidArgumentException::class, \sprintf('The command must be an instance of "%s" or an invokable object.', Command::class)]; |
| 289 | + yield 'a closure' => [function () { |
| 290 | + }, InvalidArgumentException::class, \sprintf('The command must be an instance of "%s" or an invokable object.', Command::class)]; |
| 291 | + yield 'without the #[AsCommand] attribute' => [new class { |
| 292 | + public function __invoke() |
| 293 | + { |
| 294 | + } |
| 295 | + }, LogicException::class, \sprintf('The command must use the "%s" attribute.', AsCommand::class)]; |
| 296 | + } |
| 297 | + |
242 | 298 | public function testHasGet()
|
243 | 299 | {
|
244 | 300 | $application = new Application();
|
@@ -2514,6 +2570,22 @@ public function isEnabled(): bool
|
2514 | 2570 | }
|
2515 | 2571 | }
|
2516 | 2572 |
|
| 2573 | +#[AsCommand(name: 'invokable')] |
| 2574 | +class InvokableTestCommand |
| 2575 | +{ |
| 2576 | + public function __invoke(): int |
| 2577 | + { |
| 2578 | + } |
| 2579 | +} |
| 2580 | + |
| 2581 | +#[AsCommand(name: 'invokable-extended')] |
| 2582 | +class InvokableExtendedTestCommand extends Command |
| 2583 | +{ |
| 2584 | + public function __invoke(): int |
| 2585 | + { |
| 2586 | + } |
| 2587 | +} |
| 2588 | + |
2517 | 2589 | #[AsCommand(name: 'signal')]
|
2518 | 2590 | class BaseSignableCommand extends Command
|
2519 | 2591 | {
|
|
0 commit comments