Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit74ca920

Browse files
committed
Add support for Invokable Commands in CommandTester
1 parent50d7b25 commit74ca920

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
namespaceSymfony\Component\Console\Tester;
1313

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

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

30+
privateCommand$command;
31+
2732
publicfunction__construct(
28-
privateCommand$command,
33+
callable|Command$command,
2934
) {
35+
if (!$commandinstanceof Command) {
36+
if (!\is_object($command) ||$commandinstanceof \Closure) {
37+
thrownewInvalidArgumentException(\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+
??thrownewLogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
43+
44+
$command = (newCommand($attribute->name))
45+
->setDescription($attribute->description ??'')
46+
->setHelp($attribute->help ??'')
47+
->setCode($command);
48+
49+
foreach ($attribute->usagesas$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_onceself::$fixturesPath.'/TestCommand.php';
41+
require_onceself::$fixturesPath.'/InvokableTestCommand.php';
4142
}
4243

4344
publicfunctiontestConstructor()
@@ -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+
publicfunctiontestInvokableCommand()
309+
{
310+
$tester =newCommandTester(new \InvokableTestCommand());
311+
312+
$this->assertSame(Command::SUCCESS,$tester->execute([]));
313+
}
314+
307315
publicfunctiontestRunNonInteractive()
308316
{
309317
$tester =newCommandTester(new \TestCommand());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
useSymfony\Component\Console\Attribute\AsCommand;
4+
useSymfony\Component\Console\Command\Command;
5+
6+
#[AsCommand('invokable:test')]
7+
class InvokableTestCommand
8+
{
9+
publicfunction__invoke():int
10+
{
11+
return Command::SUCCESS;
12+
}
13+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp