Skip to content

Commit

Permalink
fix: Handle command defined with AsCommand php attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémy committed Dec 24, 2024
1 parent a3a7e4a commit c648f8f
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Zenstruck\Assert;
use Zenstruck\Console\Test\Assert\CompletionExpectation;
Expand Down Expand Up @@ -56,6 +57,9 @@ public static function for(Command $command): self
public static function from(Application $application, string $cli): self
{
foreach ($application->all() as $commandObject) {
if (class_exists(LazyCommand::class) && $commandObject instanceof LazyCommand) {
$commandObject = $commandObject->getCommand();
}
if ($cli === $commandObject::class) {
return self::for($commandObject);
}
Expand Down
94 changes: 94 additions & 0 deletions tests/Fixture/FixtureAttributeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the zenstruck/console-test package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Console\Test\Tests\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
#[AsCommand(
name: 'fixture:command',
description: 'zenstruck/console-test command for tests'
)]
final class FixtureAttributeCommand extends Command
{
protected function configure(): void
{
$this
->addArgument('arg1', InputArgument::OPTIONAL)
->addOption('opt1', null, InputOption::VALUE_NONE)
->addOption('opt2', null, InputOption::VALUE_REQUIRED)
->addOption('opt3', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY)
->addOption('throw', null, InputOption::VALUE_NONE)
->addOption('code', null, InputOption::VALUE_REQUIRED, '', 0)
;
}

/**
* @param ConsoleOutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$errOutput = $output->getErrorOutput();

$output->writeln('Executing <info>command</info>...');
$output->writeln("verbosity: {$output->getVerbosity()}");
$output->writeln('decorated: '.($output->isDecorated() ? 'yes' : 'no'));
$errOutput->writeln('Error output.');

if ($input->getOption('throw')) {
throw new \RuntimeException('Exception thrown!');
}

if ($arg1 = $input->getArgument('arg1')) {
$output->writeln("arg1 value: {$arg1}");
}

if ($input->getOption('opt1')) {
$output->writeln('opt1 option set');
}

if ($opt2 = $input->getOption('opt2')) {
$output->writeln("opt2 value: {$opt2}");
}

foreach ($input->getOption('opt3') as $value) {
$output->writeln("opt3 value: {$value}");
}

(new SymfonyStyle($input, $output))->success('Long link: https://github.com/zenstruck/console-test/blob/997ee1f66743342ffd9cd00a77613ebfa2efd2b8/src/CommandResult.php');

$table = new Table($output->section());
$table->addRow(['table row 1']);
$table->render();
$table->appendRow(['table row 2']);

return (int) $input->getOption('code');
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
$value = $this->getHelper('question')->ask($input, $output, new Question('Arg1 value?'));

$input->setArgument('arg1', $value);
}
}
4 changes: 4 additions & 0 deletions tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
->setAutoconfigured(true)
->setAutowired(true)
;
$c->register(FixtureAttributeCommand::class)
->setAutoconfigured(true)
->setAutowired(true)
;
$c->register('logger', NullLogger::class);
$c->loadFromExtension('framework', [
'secret' => 'S3CRET',
Expand Down
14 changes: 14 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Helper\OutputWrapper;
use Zenstruck\Assert;
use Zenstruck\Console\Test\InteractsWithConsole;
use Zenstruck\Console\Test\Tests\Fixture\FixtureAttributeCommand;
use Zenstruck\Console\Test\Tests\Fixture\FixtureCommand;

/**
Expand Down Expand Up @@ -71,6 +72,19 @@ public function class_name_command_with_no_arguments(): void
;
}

/**
* @test
*/
public function class_name_command_with_no_arguments_defined_with_attribute(): void
{
$this->executeConsoleCommand(FixtureAttributeCommand::class)
->assertSuccessful()
->assertOutputContains('Executing command')
->assertOutputNotContains('arg1')
->assertOutputNotContains('opt1')
;
}

/**
* @test
*/
Expand Down

0 comments on commit c648f8f

Please sign in to comment.