Skip to content

Commit 74ca920

Browse files
committed
Add support for Invokable Commands in CommandTester
1 parent 50d7b25 commit 74ca920

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
namespace Symfony\Component\Console\Tester;
1313

14+
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Exception\InvalidArgumentException;
17+
use Symfony\Component\Console\Exception\LogicException;
1518
use Symfony\Component\Console\Input\ArrayInput;
1619

1720
/**
@@ -24,9 +27,31 @@ class CommandTester
2427
{
2528
use TesterTrait;
2629

30+
private Command $command;
31+
2732
public function __construct(
28-
private Command $command,
33+
callable|Command $command,
2934
) {
35+
if (!$command instanceof Command) {
36+
if (!\is_object($command) || $command instanceof \Closure) {
37+
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
38+
}
39+
40+
/** @var AsCommand $attribute */
41+
$attribute = ((new \ReflectionObject($command))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
42+
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
43+
44+
$command = (new Command($attribute->name))
45+
->setDescription($attribute->description ?? '')
46+
->setHelp($attribute->help ?? '')
47+
->setCode($command);
48+
49+
foreach ($attribute->usages as $usage) {
50+
$command->addUsage($usage);
51+
}
52+
}
53+
54+
$this->command = $command;
3055
}
3156

3257
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static function setUpBeforeClass(): void
3838
{
3939
self::$fixturesPath = __DIR__.'/../Fixtures/';
4040
require_once self::$fixturesPath.'/TestCommand.php';
41+
require_once self::$fixturesPath.'/InvokableTestCommand.php';
4142
}
4243

4344
public function testConstructor()
@@ -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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Attribute\AsCommand;
4+
use Symfony\Component\Console\Command\Command;
5+
6+
#[AsCommand('invokable:test')]
7+
class InvokableTestCommand
8+
{
9+
public function __invoke(): int
10+
{
11+
return Command::SUCCESS;
12+
}
13+
}

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