Skip to content
Merged
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
3 changes: 3 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v1.5.6
* Feature: Testing with command inputs

## v1.5.5
* Bugfix: Documentation and service definition for MockClientCallback to test APIs

Expand Down
4 changes: 4 additions & 0 deletions doc/context/CommandContext.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CommandContext
This context contains steps to test symfony commands

## Given
### `Given the next command input is <answer>`
Will answer the next upcoming question with <answer>.

## When
### `When I run command <command>`
Execute the given symfony command, including parsing of arguments.
Expand Down
22 changes: 21 additions & 1 deletion src/Context/CommandContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Behat\Behat\Context\Context;
use Behat\Hook\BeforeScenario;
use Behat\Step\Given;
use Behat\Step\Then;
use Behat\Step\When;
use DomainException;
Expand All @@ -23,6 +24,8 @@ class CommandContext implements Context
{
private ?string $output = null;
private ?int $returnCode = null;
/** @var ?resource */
private $stream = null;

public function __construct(
protected ApplicationFactory $appFactory,
Expand All @@ -35,15 +38,32 @@ public function resetDocumentIdStack(): void
{
$this->output = null;
$this->returnCode = null;
$this->stream = null;
}

#[Given('the next command input is :string')]
public function theNextCommandInputIs(string $string)
{
if (null === $this->stream) {
$this->stream = fopen('php://memory', 'r+', false);
}
fwrite($this->stream, $string.\PHP_EOL);
}

#[When('I run command :command')]
public function iRunCommand(string $command): void
{
$params = explode(' ', $command);
array_unshift($params, '-n');
if (null === $this->stream) {
array_unshift($params, '-n');
}
array_unshift($params, 'console');
$input = new ArgvInput($params);
if (null !== $this->stream) {
rewind($this->stream);
$input->setStream($this->stream);
$input->setInteractive(true);
}
$output = new BufferedOutput();

try {
Expand Down