|
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,49 @@ public function testAddCommandWithEmptyConstructor()
|
239 | 242 | (new Application())->add(new \Foo5Command());
|
240 | 243 | }
|
241 | 244 |
|
| 245 | + public function testAddInvokable() |
| 246 | + { |
| 247 | + $application = new Application(); |
| 248 | + $application->addInvokable($foo = new InvokableTestCommand()); |
| 249 | + $commands = $application->all(); |
| 250 | + |
| 251 | + $this->assertInstanceOf(Command::class, $command = $commands['invokable']); |
| 252 | + $this->assertEquals(new InvokableCommand($command, $foo), (new \ReflectionObject($command))->getProperty('code')->getValue($command)); |
| 253 | + } |
| 254 | + |
| 255 | + public function testAddInvokableWithExtendedCommand() |
| 256 | + { |
| 257 | + $application = new Application(); |
| 258 | + $application->addInvokable($foo = new InvokableExtendedTestCommand()); |
| 259 | + $commands = $application->all(); |
| 260 | + |
| 261 | + $this->assertEquals($foo, $commands['invokable-extended']); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * @dataProvider provideInvalidInvokableCommands |
| 266 | + */ |
| 267 | + public function testAddInvokableThrowsExceptionOnInvalidCommand(object $command, string $expectedException, string $expectedExceptionMessage) |
| 268 | + { |
| 269 | + $application = new Application(); |
| 270 | + |
| 271 | + $this->expectException($expectedException); |
| 272 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 273 | + |
| 274 | + $application->addInvokable($command); |
| 275 | + } |
| 276 | + |
| 277 | + public static function provideInvalidInvokableCommands(): iterable |
| 278 | + { |
| 279 | + yield 'not a callable' => [new class {}, InvalidArgumentException::class, 'The command must be an invokable object.']; |
| 280 | + yield 'a closure' => [function () {}, InvalidArgumentException::class, 'The command cannot be an anonymous function.']; |
| 281 | + yield 'without the #[AsCommand] attribute' => [new class { |
| 282 | + public function __invoke() |
| 283 | + { |
| 284 | + } |
| 285 | + }, LogicException::class, \sprintf('The command must use the "%s" attribute.', AsCommand::class)]; |
| 286 | + } |
| 287 | + |
242 | 288 | public function testHasGet()
|
243 | 289 | {
|
244 | 290 | $application = new Application();
|
@@ -2514,6 +2560,22 @@ public function isEnabled(): bool
|
2514 | 2560 | }
|
2515 | 2561 | }
|
2516 | 2562 |
|
| 2563 | +#[AsCommand(name: 'invokable')] |
| 2564 | +class InvokableTestCommand |
| 2565 | +{ |
| 2566 | + public function __invoke(): int |
| 2567 | + { |
| 2568 | + } |
| 2569 | +} |
| 2570 | + |
| 2571 | +#[AsCommand(name: 'invokable-extended')] |
| 2572 | +class InvokableExtendedTestCommand extends Command |
| 2573 | +{ |
| 2574 | + public function __invoke(): int |
| 2575 | + { |
| 2576 | + } |
| 2577 | +} |
| 2578 | + |
2517 | 2579 | #[AsCommand(name: 'signal')]
|
2518 | 2580 | class BaseSignableCommand extends Command
|
2519 | 2581 | {
|
|
0 commit comments