Skip to content

Commit 2095f79

Browse files
authored
Merge pull request #32 from elbformat/command-input
Feature: Allow input arguments in commands
2 parents 1089447 + ddd03f3 commit 2095f79

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

doc/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v1.5.6
4+
* Feature: Testing with command inputs
5+
36
## v1.5.5
47
* Bugfix: Documentation and service definition for MockClientCallback to test APIs
58

doc/context/CommandContext.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# CommandContext
22
This context contains steps to test symfony commands
33

4+
## Given
5+
### `Given the next command input is <answer>`
6+
Will answer the next upcoming question with <answer>.
7+
48
## When
59
### `When I run command <command>`
610
Execute the given symfony command, including parsing of arguments.

src/Context/CommandContext.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Behat\Behat\Context\Context;
88
use Behat\Hook\BeforeScenario;
9+
use Behat\Step\Given;
910
use Behat\Step\Then;
1011
use Behat\Step\When;
1112
use DomainException;
@@ -23,6 +24,8 @@ class CommandContext implements Context
2324
{
2425
private ?string $output = null;
2526
private ?int $returnCode = null;
27+
/** @var ?resource */
28+
private $stream = null;
2629

2730
public function __construct(
2831
protected ApplicationFactory $appFactory,
@@ -35,15 +38,32 @@ public function resetDocumentIdStack(): void
3538
{
3639
$this->output = null;
3740
$this->returnCode = null;
41+
$this->stream = null;
42+
}
43+
44+
#[Given('the next command input is :string')]
45+
public function theNextCommandInputIs(string $string)
46+
{
47+
if (null === $this->stream) {
48+
$this->stream = fopen('php://memory', 'r+', false);
49+
}
50+
fwrite($this->stream, $string.\PHP_EOL);
3851
}
3952

4053
#[When('I run command :command')]
4154
public function iRunCommand(string $command): void
4255
{
4356
$params = explode(' ', $command);
44-
array_unshift($params, '-n');
57+
if (null === $this->stream) {
58+
array_unshift($params, '-n');
59+
}
4560
array_unshift($params, 'console');
4661
$input = new ArgvInput($params);
62+
if (null !== $this->stream) {
63+
rewind($this->stream);
64+
$input->setStream($this->stream);
65+
$input->setInteractive(true);
66+
}
4767
$output = new BufferedOutput();
4868

4969
try {

0 commit comments

Comments
 (0)