First Commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester;
|
||||
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
|
||||
/**
|
||||
* Eases the testing of console applications.
|
||||
*
|
||||
* When testing an application, don't forget to disable the auto exit flag:
|
||||
*
|
||||
* $application = new Application();
|
||||
* $application->setAutoExit(false);
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class ApplicationTester
|
||||
{
|
||||
use TesterTrait;
|
||||
|
||||
public function __construct(
|
||||
private Application $application,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the application.
|
||||
*
|
||||
* Available options:
|
||||
*
|
||||
* * interactive: Sets the input interactive flag
|
||||
* * decorated: Sets the output decorated flag
|
||||
* * verbosity: Sets the output verbosity flag
|
||||
* * capture_stderr_separately: Make output of stdOut and stdErr separately available
|
||||
*
|
||||
* @return int The command exit code
|
||||
*/
|
||||
public function run(array $input, array $options = []): int
|
||||
{
|
||||
$this->input = new ArrayInput($input);
|
||||
if (isset($options['interactive'])) {
|
||||
$this->input->setInteractive($options['interactive']);
|
||||
}
|
||||
|
||||
if ($this->inputs) {
|
||||
$this->input->setStream(self::createStream($this->inputs));
|
||||
}
|
||||
|
||||
$this->initOutput($options);
|
||||
|
||||
// Temporarily clear SHELL_VERBOSITY to prevent Application::configureIO
|
||||
// from overriding the interactive and verbosity settings set above
|
||||
$prevShellVerbosity = [getenv('SHELL_VERBOSITY'), $_ENV['SHELL_VERBOSITY'] ?? false, $_SERVER['SHELL_VERBOSITY'] ?? false];
|
||||
if (\function_exists('putenv')) {
|
||||
@putenv('SHELL_VERBOSITY');
|
||||
}
|
||||
unset($_ENV['SHELL_VERBOSITY'], $_SERVER['SHELL_VERBOSITY']);
|
||||
|
||||
try {
|
||||
return $this->statusCode = $this->application->run($this->input, $this->output);
|
||||
} finally {
|
||||
if (false !== $prevShellVerbosity[0]) {
|
||||
if (\function_exists('putenv')) {
|
||||
@putenv('SHELL_VERBOSITY='.$prevShellVerbosity[0]);
|
||||
}
|
||||
}
|
||||
if (false !== $prevShellVerbosity[1]) {
|
||||
$_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity[1];
|
||||
}
|
||||
if (false !== $prevShellVerbosity[2]) {
|
||||
$_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Completion\CompletionInput;
|
||||
use Symfony\Component\Console\Completion\CompletionSuggestions;
|
||||
|
||||
/**
|
||||
* Eases the testing of command completion.
|
||||
*
|
||||
* @author Jérôme Tamarelle <jerome@tamarelle.net>
|
||||
*/
|
||||
class CommandCompletionTester
|
||||
{
|
||||
public function __construct(
|
||||
private Command $command,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create completion suggestions from input tokens.
|
||||
*/
|
||||
public function complete(array $input): array
|
||||
{
|
||||
$currentIndex = \count($input);
|
||||
if ('' === end($input)) {
|
||||
array_pop($input);
|
||||
}
|
||||
array_unshift($input, $this->command->getName());
|
||||
|
||||
$completionInput = CompletionInput::fromTokens($input, $currentIndex);
|
||||
$completionInput->bind($this->command->getDefinition());
|
||||
$suggestions = new CompletionSuggestions();
|
||||
|
||||
$this->command->complete($completionInput, $suggestions);
|
||||
|
||||
$options = [];
|
||||
foreach ($suggestions->getOptionSuggestions() as $option) {
|
||||
$options[] = '--'.$option->getName();
|
||||
}
|
||||
|
||||
return array_map('strval', array_merge($options, $suggestions->getValueSuggestions()));
|
||||
}
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatter;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Output\TestOutput;
|
||||
|
||||
/**
|
||||
* Eases the testing of console commands.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
* @author Théo FIDRY <theo.fidry@gmail.com>
|
||||
*/
|
||||
class CommandTester
|
||||
{
|
||||
use TesterTrait;
|
||||
|
||||
private Command $command;
|
||||
private OutputFormatterInterface $outputFormatter;
|
||||
|
||||
/**
|
||||
* @param OutputInterface::VERBOSITY_* $verbosity
|
||||
*/
|
||||
public function __construct(
|
||||
callable|Command $command,
|
||||
private ?bool $interactive = null,
|
||||
private bool $decorated = false,
|
||||
private int $verbosity = OutputInterface::VERBOSITY_NORMAL,
|
||||
?OutputFormatterInterface $outputFormatter = null,
|
||||
) {
|
||||
$this->command = $command instanceof Command ? $command : new Command(null, $command);
|
||||
$this->outputFormatter = $outputFormatter ?? new OutputFormatter();
|
||||
}
|
||||
|
||||
public function setInteractive(bool $interactive): void
|
||||
{
|
||||
$this->interactive = $interactive;
|
||||
}
|
||||
|
||||
public function setDecorated(bool $decorated): void
|
||||
{
|
||||
$this->decorated = $decorated;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OutputInterface::VERBOSITY_* $level
|
||||
*/
|
||||
public function setVerbosity(int $level): void
|
||||
{
|
||||
$this->verbosity = $level;
|
||||
}
|
||||
|
||||
public function setOutputFormatter(OutputFormatterInterface $outputFormatter): void
|
||||
{
|
||||
$this->outputFormatter = $outputFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the command with the result-based testing API.
|
||||
*
|
||||
* This method is intended for new tests and returns an ExecutionResult,
|
||||
* which exposes output, error output and combined display in a single object.
|
||||
*
|
||||
* Unlike execute(), this method does not rely on state read back from TesterTrait.
|
||||
*
|
||||
* @param array $input An array of command arguments and options
|
||||
* @param string[] $interactiveInputs An array of strings representing each input passed to the command input stream
|
||||
* @param OutputInterface::VERBOSITY_* $verbosity
|
||||
* @param array<\Closure(string): string> $normalizers
|
||||
*/
|
||||
public function run(array $input = [], array $interactiveInputs = [], ?bool $interactive = null, ?bool $decorated = null, ?int $verbosity = null, array $normalizers = []): ExecutionResult
|
||||
{
|
||||
$input = $this->createInput($input, $interactiveInputs, $interactive);
|
||||
$testOutput = new TestOutput($decorated ?? $this->decorated, $verbosity ?? $this->verbosity, $this->outputFormatter);
|
||||
$statusCode = $this->command->run($input, $testOutput);
|
||||
|
||||
return new ExecutionResult($input, $statusCode, $testOutput, $normalizers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the command with the legacy stateful testing API.
|
||||
*
|
||||
* Use this method when interacting with the historical TesterTrait-based API,
|
||||
* e.g. getDisplay(), getErrorOutput(), getStatusCode() and assertCommandIsSuccessful().
|
||||
*
|
||||
* Prefer run() for new tests, as it returns an ExecutionResult object with
|
||||
* explicit output streams and dedicated assertions.
|
||||
*
|
||||
* Available execution options:
|
||||
*
|
||||
* * interactive: Sets the input interactive flag
|
||||
* * decorated: Sets the output decorated flag
|
||||
* * verbosity: Sets the output verbosity flag
|
||||
* * capture_stderr_separately: Make output of stdOut and stdErr separately available
|
||||
*
|
||||
* @param array $input An array of command arguments and options
|
||||
* @param array $options An array of execution options
|
||||
*
|
||||
* @return int The command exit code
|
||||
*/
|
||||
public function execute(array $input, array $options = []): int
|
||||
{
|
||||
$this->input = $this->createInput($input, $this->inputs, $options['interactive'] ?? $this->interactive);
|
||||
|
||||
if (!isset($options['decorated'])) {
|
||||
$options['decorated'] = $this->decorated;
|
||||
}
|
||||
|
||||
$this->initOutput($options);
|
||||
|
||||
return $this->statusCode = $this->command->run($this->input, $this->output);
|
||||
}
|
||||
|
||||
private function createInput(array $input, array $interactiveInputs = [], ?bool $interactive = null): InputInterface
|
||||
{
|
||||
if (!isset($input['command']) && $this->command->getApplication()?->getDefinition()->hasArgument('command')) {
|
||||
$input = array_merge(['command' => $this->command->getName()], $input);
|
||||
}
|
||||
|
||||
$input = new ArrayInput($input);
|
||||
// Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
|
||||
$input->setStream(self::createStream($interactiveInputs));
|
||||
|
||||
if (null !== $interactive ??= $this->interactive) {
|
||||
$input->setInteractive($interactive);
|
||||
}
|
||||
|
||||
return $input;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester;
|
||||
|
||||
use Symfony\Component\Console\Tester\Constraint\CommandFailed;
|
||||
use Symfony\Component\Console\Tester\Constraint\CommandIsInvalid;
|
||||
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;
|
||||
|
||||
/**
|
||||
* @psalm-require-extends \PHPUnit\Framework\TestCase
|
||||
*
|
||||
* @author Théo FIDRY <theo.fidry@gmail.com>
|
||||
*/
|
||||
trait ConsoleAssertionsTrait
|
||||
{
|
||||
public function assertCommandIsSuccessful(ExecutionResult $result, string $message = ''): void
|
||||
{
|
||||
$this->assertThat($result->statusCode, new CommandIsSuccessful(), $message);
|
||||
}
|
||||
|
||||
public function assertCommandFailed(ExecutionResult $result, string $message = ''): void
|
||||
{
|
||||
$this->assertThat($result->statusCode, new CommandFailed(), $message);
|
||||
}
|
||||
|
||||
public function assertCommandIsInvalid(ExecutionResult $result, string $message = ''): void
|
||||
{
|
||||
$this->assertThat($result->statusCode, new CommandIsInvalid(), $message);
|
||||
}
|
||||
|
||||
public function assertCommandResultEquals(ExecutionResult $result, ?int $expectedStatusCode = null, ?string $expectedOutput = null, ?string $expectedErrorOutput = null, ?string $expectedDisplay = null, string $message = ''): void
|
||||
{
|
||||
$expected = [];
|
||||
$actual = [];
|
||||
|
||||
if (null !== $expectedStatusCode) {
|
||||
$expected['statusCode'] = $expectedStatusCode;
|
||||
$actual['statusCode'] = $result->statusCode;
|
||||
}
|
||||
if (null !== $expectedOutput) {
|
||||
$expected['output'] = $expectedOutput;
|
||||
$actual['output'] = $result->getOutput();
|
||||
}
|
||||
if (null !== $expectedErrorOutput) {
|
||||
$expected['errorOutput'] = $expectedErrorOutput;
|
||||
$actual['errorOutput'] = $result->getErrorOutput();
|
||||
}
|
||||
if (null !== $expectedDisplay) {
|
||||
$expected['display'] = $expectedDisplay;
|
||||
$actual['display'] = $result->getDisplay();
|
||||
}
|
||||
|
||||
$this->assertEquals($expected, $actual, $message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester\Constraint;
|
||||
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
final class CommandFailed extends Constraint
|
||||
{
|
||||
public function toString(): string
|
||||
{
|
||||
return 'failed';
|
||||
}
|
||||
|
||||
protected function matches($other): bool
|
||||
{
|
||||
return Command::FAILURE === $other;
|
||||
}
|
||||
|
||||
protected function failureDescription($other): string
|
||||
{
|
||||
return 'the command '.$this->toString();
|
||||
}
|
||||
|
||||
protected function additionalFailureDescription($other): string
|
||||
{
|
||||
$mapping = [
|
||||
Command::SUCCESS => 'Command was successful.',
|
||||
Command::INVALID => 'Command was invalid.',
|
||||
];
|
||||
|
||||
return $mapping[$other] ?? \sprintf('Command returned exit status %d.', $other);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester\Constraint;
|
||||
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
final class CommandIsInvalid extends Constraint
|
||||
{
|
||||
public function toString(): string
|
||||
{
|
||||
return 'is invalid';
|
||||
}
|
||||
|
||||
protected function matches($other): bool
|
||||
{
|
||||
return Command::INVALID === $other;
|
||||
}
|
||||
|
||||
protected function failureDescription($other): string
|
||||
{
|
||||
return 'the command '.$this->toString();
|
||||
}
|
||||
|
||||
protected function additionalFailureDescription($other): string
|
||||
{
|
||||
$mapping = [
|
||||
Command::SUCCESS => 'Command was successful.',
|
||||
Command::FAILURE => 'Command failed.',
|
||||
];
|
||||
|
||||
return $mapping[$other] ?? \sprintf('Command returned exit status %d.', $other);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester\Constraint;
|
||||
|
||||
use PHPUnit\Framework\Constraint\Constraint;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
final class CommandIsSuccessful extends Constraint
|
||||
{
|
||||
public function toString(): string
|
||||
{
|
||||
return 'is successful';
|
||||
}
|
||||
|
||||
protected function matches($other): bool
|
||||
{
|
||||
return Command::SUCCESS === $other;
|
||||
}
|
||||
|
||||
protected function failureDescription($other): string
|
||||
{
|
||||
return 'the command '.$this->toString();
|
||||
}
|
||||
|
||||
protected function additionalFailureDescription($other): string
|
||||
{
|
||||
$mapping = [
|
||||
Command::FAILURE => 'Command failed.',
|
||||
Command::INVALID => 'Command was invalid.',
|
||||
];
|
||||
|
||||
return $mapping[$other] ?? \sprintf('Command returned exit status %d.', $other);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester;
|
||||
|
||||
use Symfony\Component\Console\Output\TestOutput;
|
||||
|
||||
/**
|
||||
* @author Théo FIDRY <theo.fidry@gmail.com>
|
||||
*/
|
||||
final class ExecutionResult
|
||||
{
|
||||
// This is purely for memoizing purposes
|
||||
private array $results = [];
|
||||
|
||||
/**
|
||||
* @param array<\Closure(string): string> $normalizers
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly string $input,
|
||||
public readonly int $statusCode,
|
||||
private readonly TestOutput $output,
|
||||
private readonly array $normalizers = [],
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the display returned by the execution of the command or application. The display combines what was
|
||||
* written on both the output and error output.
|
||||
*/
|
||||
public function getDisplay(bool $normalize = true): string
|
||||
{
|
||||
return $this->results['display'][$normalize] ??= $this->normalize($this->output->getDisplayContents(), $normalize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the output written to the output by the command or application.
|
||||
*/
|
||||
public function getOutput(bool $normalize = false): string
|
||||
{
|
||||
return $this->results['output'][$normalize] ??= $this->normalize($this->output->getOutputContents(), $normalize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the output written to the error output by the command or application.
|
||||
*/
|
||||
public function getErrorOutput(bool $normalize = false): string
|
||||
{
|
||||
return $this->results['errorOutput'][$normalize] ??= $this->normalize($this->output->getErrorOutputContents(), $normalize);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function dump(): static
|
||||
{
|
||||
$summary = "CLI: {$this->input}, Status: {$this->statusCode}";
|
||||
$output = [
|
||||
$summary,
|
||||
$this->getOutput(true),
|
||||
$this->getErrorOutput(true),
|
||||
$summary,
|
||||
];
|
||||
|
||||
\call_user_func(
|
||||
\function_exists('dump') ? 'dump' : 'var_dump',
|
||||
implode("\n\n", array_filter($output)),
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function dd(): never
|
||||
{
|
||||
$this->dump();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
private function normalize(string $value, bool $normalize): string
|
||||
{
|
||||
if (!$normalize) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
foreach ($this->normalizers as $normalizer) {
|
||||
$value = $normalizer($value);
|
||||
}
|
||||
|
||||
return str_replace(\PHP_EOL, "\n", $value);
|
||||
}
|
||||
}
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tester;
|
||||
|
||||
use PHPUnit\Framework\Assert;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
use Symfony\Component\Console\Tester\Constraint\CommandFailed;
|
||||
use Symfony\Component\Console\Tester\Constraint\CommandIsInvalid;
|
||||
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;
|
||||
|
||||
/**
|
||||
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
|
||||
*/
|
||||
trait TesterTrait
|
||||
{
|
||||
private StreamOutput $output;
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private array $inputs = [];
|
||||
private bool $captureStreamsIndependently = false;
|
||||
private InputInterface $input;
|
||||
private int $statusCode;
|
||||
|
||||
/**
|
||||
* Gets the display returned by the last execution of the command or application.
|
||||
*
|
||||
* @throws \RuntimeException If it's called before the execute method
|
||||
*/
|
||||
public function getDisplay(bool $normalize = false): string
|
||||
{
|
||||
if (!isset($this->output)) {
|
||||
throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?');
|
||||
}
|
||||
|
||||
rewind($this->output->getStream());
|
||||
|
||||
$display = stream_get_contents($this->output->getStream());
|
||||
|
||||
if ($normalize) {
|
||||
$display = str_replace(\PHP_EOL, "\n", $display);
|
||||
}
|
||||
|
||||
return $display;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the output written to STDERR by the application.
|
||||
*
|
||||
* @param bool $normalize Whether to normalize end of lines to \n or not
|
||||
*/
|
||||
public function getErrorOutput(bool $normalize = false): string
|
||||
{
|
||||
if (!$this->captureStreamsIndependently) {
|
||||
throw new \LogicException('The error output is not available when the tester is run without "capture_stderr_separately" option set.');
|
||||
}
|
||||
|
||||
rewind($this->output->getErrorOutput()->getStream());
|
||||
|
||||
$display = stream_get_contents($this->output->getErrorOutput()->getStream());
|
||||
|
||||
if ($normalize) {
|
||||
$display = str_replace(\PHP_EOL, "\n", $display);
|
||||
}
|
||||
|
||||
return $display;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the input instance used by the last execution of the command or application.
|
||||
*/
|
||||
public function getInput(): InputInterface
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the output instance used by the last execution of the command or application.
|
||||
*/
|
||||
public function getOutput(): OutputInterface
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the status code returned by the last execution of the command or application.
|
||||
*
|
||||
* @throws \RuntimeException If it's called before the execute method
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode ?? throw new \RuntimeException('Status code not initialized, did you execute the command before requesting the status code?');
|
||||
}
|
||||
|
||||
public function assertCommandIsSuccessful(string $message = ''): void
|
||||
{
|
||||
Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message);
|
||||
}
|
||||
|
||||
public function assertCommandFailed(string $message = ''): void
|
||||
{
|
||||
Assert::assertThat($this->statusCode, new CommandFailed(), $message);
|
||||
}
|
||||
|
||||
public function assertCommandIsInvalid(string $message = ''): void
|
||||
{
|
||||
Assert::assertThat($this->statusCode, new CommandIsInvalid(), $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user inputs.
|
||||
*
|
||||
* @param list<string> $inputs An array of strings representing each input
|
||||
* passed to the command input stream
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setInputs(array $inputs): static
|
||||
{
|
||||
$this->inputs = $inputs;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the output property.
|
||||
*
|
||||
* Available options:
|
||||
*
|
||||
* * decorated: Sets the output decorated flag
|
||||
* * verbosity: Sets the output verbosity flag
|
||||
* * capture_stderr_separately: Make output of stdOut and stdErr separately available
|
||||
*/
|
||||
private function initOutput(array $options): void
|
||||
{
|
||||
$this->captureStreamsIndependently = $options['capture_stderr_separately'] ?? false;
|
||||
if (!$this->captureStreamsIndependently) {
|
||||
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
|
||||
if (isset($options['decorated'])) {
|
||||
$this->output->setDecorated($options['decorated']);
|
||||
}
|
||||
if (isset($options['verbosity'])) {
|
||||
$this->output->setVerbosity($options['verbosity']);
|
||||
}
|
||||
} else {
|
||||
$this->output = new ConsoleOutput(
|
||||
$options['verbosity'] ?? ConsoleOutput::VERBOSITY_NORMAL,
|
||||
$options['decorated'] ?? null
|
||||
);
|
||||
|
||||
$errorOutput = new StreamOutput(fopen('php://memory', 'w', false));
|
||||
$errorOutput->setFormatter($this->output->getFormatter());
|
||||
$errorOutput->setVerbosity($this->output->getVerbosity());
|
||||
$errorOutput->setDecorated($this->output->isDecorated());
|
||||
|
||||
$reflectedOutput = new \ReflectionObject($this->output);
|
||||
$strErrProperty = $reflectedOutput->getProperty('stderr');
|
||||
$strErrProperty->setValue($this->output, $errorOutput);
|
||||
|
||||
$reflectedParent = $reflectedOutput->getParentClass();
|
||||
$streamProperty = $reflectedParent->getProperty('stream');
|
||||
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $inputs
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
private static function createStream(array $inputs)
|
||||
{
|
||||
$stream = fopen('php://memory', 'r+', false);
|
||||
|
||||
foreach ($inputs as $input) {
|
||||
fwrite($stream, $input);
|
||||
|
||||
if (!str_ends_with($input, "\x4")) {
|
||||
fwrite($stream, \PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
rewind($stream);
|
||||
|
||||
return $stream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user