Skip to content
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

fix: Handle command defined with AsCommand php attribute #24

Closed
wants to merge 1 commit into from
Closed
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
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 <[email protected]>
*
* 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 <[email protected]>
*/
#[AsCommand(
name: 'fixture-attribute:command',
description: 'zenstruck/console-test command for tests'
)]
final class FixtureAttributeCommand extends Command
Copy link
Author

@Cryde Cryde Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have copy past your other FixtureCommand but without cleaning it
Maybe tests should be duplicated for this case (with #[AsCommand] attribute) ?

{
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
Loading