Skip to content

Commit 7cb3676

Browse files
authored
Add command requiring user input and test passing inputs works (#28)
1 parent 8d47860 commit 7cb3676

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/Command/AskForInputCommand.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\Output\OutputInterface;
11+
use Symfony\Component\Console\Question\ConfirmationQuestion;
12+
use Symfony\Component\Console\Question\Question;
13+
14+
#[AsCommand('app:ask-for-input', 'An example command asking for user input.')]
15+
final class AskForInputCommand extends Command
16+
{
17+
protected function execute(InputInterface $input, OutputInterface $output): int
18+
{
19+
$helper = $this->getHelper('question');
20+
21+
$question = new ConfirmationQuestion('continue?', false);
22+
if (!$helper->ask($input, $output, $question)) {
23+
$output->writeln('bye');
24+
25+
return Command::FAILURE;
26+
}
27+
28+
$question = new Question('input');
29+
$answer = $helper->ask($input, $output, $question);
30+
31+
$output->writeln("user input: '$answer'");
32+
33+
return Command::SUCCESS;
34+
}
35+
}

tests/Functional/ConsoleCest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
namespace App\Tests\Functional;
66

7+
use App\Command\AskForInputCommand;
78
use App\Command\ExampleCommand;
89
use App\Tests\Support\FunctionalTester;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Exception\MissingInputException;
912

1013
final class ConsoleCest
1114
{
12-
public function runSymfonyConsoleCommand(FunctionalTester $I)
15+
public function runSymfonyConsoleCommand(FunctionalTester $I): void
1316
{
1417
// Call Symfony console without option
1518
$output = $I->runSymfonyConsoleCommand(ExampleCommand::getDefaultName());
@@ -29,4 +32,31 @@ public function runSymfonyConsoleCommand(FunctionalTester $I)
2932
);
3033
$I->assertStringContainsString('Bye world!', $output);
3134
}
35+
36+
public function runSymfonyConsoleCommandInput(FunctionalTester $I): void
37+
{
38+
// Confirmation question not confirmed
39+
$output = $I->runSymfonyConsoleCommand(
40+
AskForInputCommand::getDefaultName(),
41+
consoleInputs: ['n'],
42+
expectedExitCode: Command::FAILURE,
43+
);
44+
$I->assertStringContainsString('bye', $output);
45+
46+
// Exception on missing input
47+
$I->expectThrowable(
48+
MissingInputException::class,
49+
fn () => $I->runSymfonyConsoleCommand(
50+
AskForInputCommand::getDefaultName(),
51+
consoleInputs: ['y'],
52+
),
53+
);
54+
55+
// Multiple inputs
56+
$output = $I->runSymfonyConsoleCommand(
57+
AskForInputCommand::getDefaultName(),
58+
consoleInputs: ['y', 'foobar'],
59+
);
60+
$I->assertStringContainsString("user input: 'foobar'", $output);
61+
}
3262
}

0 commit comments

Comments
 (0)