Skip to content

Commit c85f2b6

Browse files
committed
Improve console tests
1 parent 5de0703 commit c85f2b6

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Command/ExampleCommand.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@
66

77
use Symfony\Component\Console\Command\Command;
88
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
910
use Symfony\Component\Console\Output\OutputInterface;
1011
use Symfony\Component\Console\Style\SymfonyStyle;
1112

1213
final class ExampleCommand extends Command
1314
{
15+
private const OPTION_SOMETHING = 'something';
16+
private const OPTION_SHORT_SOMETHING = 's';
17+
1418
private $ioStream;
1519

1620
protected static $defaultName = 'app:example-command';
1721

1822
protected function configure()
1923
{
2024
$this->setDescription('An example command.');
25+
26+
$this->addOption(
27+
self::OPTION_SOMETHING,
28+
self::OPTION_SHORT_SOMETHING,
29+
InputOption::VALUE_NONE,
30+
'Give some output'
31+
);
2132
}
2233

2334
protected function initialize(InputInterface $input, OutputInterface $output): void
@@ -27,8 +38,11 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
2738

2839
protected function execute(InputInterface $input, OutputInterface $output): int
2940
{
30-
$this->ioStream->text('Hello world!');
31-
41+
if ($input->getOption(self::OPTION_SOMETHING)) {
42+
$this->ioStream->text('Bye world!');
43+
} else {
44+
$this->ioStream->text('Hello world!');
45+
}
3246
return Command::SUCCESS;
3347
}
3448
}

tests/Functional/SymfonyModuleCest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,23 @@ public function logout(FunctionalTester $I)
134134

135135
public function runSymfonyConsoleCommand(FunctionalTester $I)
136136
{
137+
// Call Symfony console without option
137138
$output = $I->runSymfonyConsoleCommand(ExampleCommand::getDefaultName());
138139
$I->assertStringContainsString('Hello world!', $output);
140+
141+
// Call Symfony console with short option
142+
$output = $I->runSymfonyConsoleCommand(
143+
ExampleCommand::getDefaultName(),
144+
['-s' => true]
145+
);
146+
$I->assertStringContainsString('Bye world!', $output);
147+
148+
// Call Symfony console with long option
149+
$output = $I->runSymfonyConsoleCommand(
150+
ExampleCommand::getDefaultName(),
151+
['--something' => true]
152+
);
153+
$I->assertStringContainsString('Bye world!', $output);
139154
}
140155

141156
public function seeAuthentication(FunctionalTester $I)

0 commit comments

Comments
 (0)