Skip to content

Commit a10ae7f

Browse files
committed
Add support for Invokable Commands in CommandTester
1 parent e191195 commit a10ae7f

File tree

9 files changed

+92
-28
lines changed

9 files changed

+92
-28
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -552,22 +552,7 @@ public function addCommand(callable|Command $command): ?Command
552552
$this->init();
553553

554554
if (!$command instanceof Command) {
555-
if (!\is_object($command) || $command instanceof \Closure) {
556-
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
557-
}
558-
559-
/** @var AsCommand $attribute */
560-
$attribute = ((new \ReflectionObject($command))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
561-
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
562-
563-
$command = (new Command($attribute->name))
564-
->setDescription($attribute->description ?? '')
565-
->setHelp($attribute->help ?? '')
566-
->setCode($command);
567-
568-
foreach ($attribute->usages as $usage) {
569-
$command->addUsage($usage);
570-
}
555+
$command = new Command(null, $command);
571556
}
572557

573558
$command->setApplication($this);

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CHANGELOG
88
* Introduce `Symfony\Component\Console\Application::addCommand()` to simplify using invokable commands when the component is used standalone
99
* Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()`
1010
* Add `BackedEnum` support with `#[Argument]` and `#[Option]` inputs in invokable commands
11-
* Allow Usages to be specified via #[AsCommand] attribute.
11+
* Allow Usages to be specified via `#[AsCommand]` attribute.
12+
* Allow passing invokable commands to `Symfony\Component\Console\Tester\CommandTester`
1213

1314
7.3
1415
---

src/Symfony/Component/Console/Command/Command.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,31 @@ public static function getDefaultDescription(): ?string
8787
*
8888
* @throws LogicException When the command name is empty
8989
*/
90-
public function __construct(?string $name = null)
90+
public function __construct(?string $name = null, ?callable $code = null)
9191
{
9292
$this->definition = new InputDefinition();
9393

94+
if ($code !== null) {
95+
if (!\is_object($code) || $code instanceof \Closure) {
96+
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
97+
}
98+
99+
/** @var AsCommand $attribute */
100+
$attribute = ((new \ReflectionObject($code))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
101+
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
102+
103+
$this->setName($name ?? $attribute->name)
104+
->setDescription($attribute->description ?? '')
105+
->setHelp($attribute->help ?? '')
106+
->setCode($code);
107+
108+
foreach ($attribute->usages as $usage) {
109+
$this->addUsage($usage);
110+
}
111+
112+
return;
113+
}
114+
94115
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
95116

96117
if (null === $name) {

src/Symfony/Component/Console/Tester/CommandTester.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ class CommandTester
2424
{
2525
use TesterTrait;
2626

27+
private Command $command;
28+
2729
public function __construct(
28-
private Command $command,
30+
callable|Command $command,
2931
) {
32+
$this->command = $command instanceof Command ? $command : new Command(null, $command);
3033
}
3134

3235
/**

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
4949
use Symfony\Component\Console\Terminal;
5050
use Symfony\Component\Console\Tester\ApplicationTester;
51+
use Symfony\Component\Console\Tests\Fixtures\InvokableExtendingCommandTestCommand;
52+
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
5153
use Symfony\Component\Console\Tests\Fixtures\MockableAppliationWithTerminalWidth;
5254
use Symfony\Component\DependencyInjection\ContainerBuilder;
5355
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -257,7 +259,7 @@ public function testAddCommandWithInvokableCommand()
257259
$application->addCommand($foo = new InvokableTestCommand());
258260
$commands = $application->all();
259261

260-
$this->assertInstanceOf(Command::class, $command = $commands['invokable']);
262+
$this->assertInstanceOf(Command::class, $command = $commands['invokable:test']);
261263
$this->assertEquals(new InvokableCommand($command, $foo), (new \ReflectionObject($command))->getProperty('code')->getValue($command));
262264
}
263265

@@ -2570,14 +2572,6 @@ public function isEnabled(): bool
25702572
}
25712573
}
25722574

2573-
#[AsCommand(name: 'invokable')]
2574-
class InvokableTestCommand
2575-
{
2576-
public function __invoke(): int
2577-
{
2578-
}
2579-
}
2580-
25812575
#[AsCommand(name: 'invokable-extended')]
25822576
class InvokableExtendedTestCommand extends Command
25832577
{

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Console\Output\NullOutput;
2828
use Symfony\Component\Console\Output\OutputInterface;
2929
use Symfony\Component\Console\Tester\CommandTester;
30+
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
3031

3132
class CommandTest extends TestCase
3233
{
@@ -304,6 +305,13 @@ public function testRunInteractive()
304305
$this->assertEquals('interact called'.\PHP_EOL.'execute called'.\PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
305306
}
306307

308+
public function testInvokableCommand()
309+
{
310+
$tester = new CommandTester(new InvokableTestCommand());
311+
312+
$this->assertSame(Command::SUCCESS, $tester->execute([]));
313+
}
314+
307315
public function testRunNonInteractive()
308316
{
309317
$tester = new CommandTester(new \TestCommand());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\Console\Tests\Fixtures;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[AsCommand('invokable:test')]
9+
class InvokableExtendingCommandTestCommand extends Command
10+
{
11+
public function __invoke(): int
12+
{
13+
return Command::SUCCESS;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\Console\Tests\Fixtures;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[AsCommand('invokable:test')]
9+
class InvokableTestCommand
10+
{
11+
public function __invoke(): int
12+
{
13+
return Command::SUCCESS;
14+
}
15+
}

src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Symfony\Component\Console\Question\Question;
2424
use Symfony\Component\Console\Style\SymfonyStyle;
2525
use Symfony\Component\Console\Tester\CommandTester;
26+
use Symfony\Component\Console\Tests\Fixtures\InvokableExtendingCommandTestCommand;
27+
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
2628

2729
class CommandTesterTest extends TestCase
2830
{
@@ -267,4 +269,24 @@ public function testErrorOutput()
267269

268270
$this->assertSame('foo', $tester->getErrorOutput());
269271
}
272+
273+
public function testAInvokableCommand()
274+
{
275+
$command = new InvokableTestCommand();
276+
277+
$tester = new CommandTester($command);
278+
$tester->execute([]);
279+
280+
$tester->assertCommandIsSuccessful();
281+
}
282+
283+
public function testAInvokableExtendedCommand()
284+
{
285+
$command = new InvokableExtendingCommandTestCommand();
286+
287+
$tester = new CommandTester($command);
288+
$tester->execute([]);
289+
290+
$tester->assertCommandIsSuccessful();
291+
}
270292
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy