Skip to content

Commit 13b0b4c

Browse files
committed
Add functional test for the console execution result assertions
runCommand() and assertCommandIsSuccessful/Failed/IsInvalid/ResultEquals are Symfony 8.1+ only, so they are covered on this branch.
1 parent 41cb98b commit 13b0b4c

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎src/Command/ResultCommand.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Command;
6+
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
#[AsCommand(ResultCommand::COMMAND_NAME, 'A command exercising the execution result paths.')]
15+
final class ResultCommand extends Command
16+
{
17+
public const COMMAND_NAME = 'app:result-command';
18+
19+
protected function configure(): void
20+
{
21+
$this->addOption('fail', null, InputOption::VALUE_NONE);
22+
$this->addOption('invalid', null, InputOption::VALUE_NONE);
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output): int
26+
{
27+
if ($input->getOption('invalid')) {
28+
$output->writeln('Invalid input.');
29+
30+
return Command::INVALID;
31+
}
32+
33+
if ($input->getOption('fail')) {
34+
if ($output instanceof ConsoleOutputInterface) {
35+
$output->getErrorOutput()->writeln('Something failed.');
36+
}
37+
38+
return Command::FAILURE;
39+
}
40+
41+
$output->writeln('All good.');
42+
43+
return Command::SUCCESS;
44+
}
45+
}

‎tests/Functional/ConsoleCest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Tests\Functional;
66

77
use App\Command\ExampleCommand;
8+
use App\Command\ResultCommand;
89
use App\Tests\Support\FunctionalTester;
910

1011
final class ConsoleCest
@@ -29,4 +30,15 @@ public function runSymfonyConsoleCommand(FunctionalTester $I)
2930
);
3031
$I->assertStringContainsString('Bye world!', $output);
3132
}
33+
34+
public function consoleExecutionResult(FunctionalTester $I): void
35+
{
36+
$I->assertCommandIsSuccessful($I->runCommand(ResultCommand::COMMAND_NAME));
37+
$I->assertCommandFailed($I->runCommand(ResultCommand::COMMAND_NAME, ['--fail' => true]));
38+
$I->assertCommandIsInvalid($I->runCommand(ResultCommand::COMMAND_NAME, ['--invalid' => true]));
39+
40+
$result = $I->runCommand(ResultCommand::COMMAND_NAME, ['--fail' => true]);
41+
$I->assertStringContainsString('Something failed.', $result->getErrorOutput());
42+
$I->assertCommandResultEquals($result, expectedStatusCode: 1);
43+
}
3244
}

0 commit comments

Comments
 (0)