Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Add support for Invokable Commands in CommandTester #60823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,22 +552,7 @@ public function addCommand(callable|Command $command): ?Command
$this->init();

if (!$command instanceof Command) {
if (!\is_object($command) || $command instanceof \Closure) {
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
}

/** @var AsCommand $attribute */
$attribute = ((new \ReflectionObject($command))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));

$command = (new Command($attribute->name))
->setDescription($attribute->description ?? '')
->setHelp($attribute->help ?? '')
->setCode($command);

foreach ($attribute->usages as $usage) {
$command->addUsage($usage);
}
$command = new Command(null, $command);
}

$command->setApplication($this);
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ CHANGELOG
* Introduce `Symfony\Component\Console\Application::addCommand()` to simplify using invokable commands when the component is used standalone
* Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()`
* Add `BackedEnum` support with `#[Argument]` and `#[Option]` inputs in invokable commands
* Allow Usages to be specified via #[AsCommand] attribute.
* Allow Usages to be specified via `#[AsCommand]` attribute.
* Allow passing invokable commands to `Symfony\Component\Console\Tester\CommandTester`

7.3
---
Expand Down
23 changes: 22 additions & 1 deletion src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,31 @@ public static function getDefaultDescription(): ?string
*
* @throws LogicException When the command name is empty
*/
public function __construct(?string $name = null)
public function __construct(?string $name = null, ?callable $code = null)
{
$this->definition = new InputDefinition();

if ($code !== null) {
if (!\is_object($code) || $code instanceof \Closure) {
throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class));
}

/** @var AsCommand $attribute */
$attribute = ((new \ReflectionObject($code))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));

$this->setName($name ?? $attribute->name)
->setDescription($attribute->description ?? '')
->setHelp($attribute->help ?? '')
->setCode($code);

foreach ($attribute->usages as $usage) {
$this->addUsage($usage);
}

return;
}

$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();

if (null === $name) {
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Console/Tester/CommandTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ class CommandTester
{
use TesterTrait;

private Command $command;

public function __construct(
private Command $command,
callable|Command $command,
) {
$this->command = $command instanceof Command ? $command : new Command(null, $command);
}

/**
Expand Down
12 changes: 3 additions & 9 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Console\Tests\Fixtures\InvokableExtendingCommandTestCommand;
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
use Symfony\Component\Console\Tests\Fixtures\MockableAppliationWithTerminalWidth;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -257,7 +259,7 @@ public function testAddCommandWithInvokableCommand()
$application->addCommand($foo = new InvokableTestCommand());
$commands = $application->all();

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

Expand Down Expand Up @@ -2570,14 +2572,6 @@ public function isEnabled(): bool
}
}

#[AsCommand(name: 'invokable')]
class InvokableTestCommand
{
public function __invoke(): int
{
}
}

#[AsCommand(name: 'invokable-extended')]
class InvokableExtendedTestCommand extends Command
{
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;

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

public function testInvokableCommand()
{
$tester = new CommandTester(new InvokableTestCommand());

$this->assertSame(Command::SUCCESS, $tester->execute([]));
}

public function testRunNonInteractive()
{
$tester = new CommandTester(new \TestCommand());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\Console\Tests\Fixtures;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

#[AsCommand('invokable:test')]
class InvokableExtendingCommandTestCommand extends Command
{
public function __invoke(): int
{
return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\Console\Tests\Fixtures;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

#[AsCommand('invokable:test')]
class InvokableTestCommand
{
public function __invoke(): int
{
return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Tests\Fixtures\InvokableExtendingCommandTestCommand;
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;

class CommandTesterTest extends TestCase
{
Expand Down Expand Up @@ -267,4 +269,24 @@ public function testErrorOutput()

$this->assertSame('foo', $tester->getErrorOutput());
}

public function testAInvokableCommand()
{
$command = new InvokableTestCommand();

$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
}

public function testAInvokableExtendedCommand()
{
$command = new InvokableExtendingCommandTestCommand();

$tester = new CommandTester($command);
$tester->execute([]);

$tester->assertCommandIsSuccessful();
}
}
Loading