first commit
This commit is contained in:
85
vendor/symfony/console/Tester/ApplicationTester.php
vendored
Normal file
85
vendor/symfony/console/Tester/ApplicationTester.php
vendored
Normal file
@ -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;
|
||||
|
||||
private Application $application;
|
||||
|
||||
public function __construct(Application $application)
|
||||
{
|
||||
$this->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
|
||||
{
|
||||
$prevShellVerbosity = getenv('SHELL_VERBOSITY');
|
||||
|
||||
try {
|
||||
$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);
|
||||
|
||||
return $this->statusCode = $this->application->run($this->input, $this->output);
|
||||
} finally {
|
||||
// SHELL_VERBOSITY is set by Application::configureIO so we need to unset/reset it
|
||||
// to its previous value to avoid one test's verbosity to spread to the following tests
|
||||
if (false === $prevShellVerbosity) {
|
||||
if (\function_exists('putenv')) {
|
||||
@putenv('SHELL_VERBOSITY');
|
||||
}
|
||||
unset($_ENV['SHELL_VERBOSITY']);
|
||||
unset($_SERVER['SHELL_VERBOSITY']);
|
||||
} else {
|
||||
if (\function_exists('putenv')) {
|
||||
@putenv('SHELL_VERBOSITY='.$prevShellVerbosity);
|
||||
}
|
||||
$_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity;
|
||||
$_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
vendor/symfony/console/Tester/CommandCompletionTester.php
vendored
Normal file
56
vendor/symfony/console/Tester/CommandCompletionTester.php
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
<?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
|
||||
{
|
||||
private Command $command;
|
||||
|
||||
public function __construct(Command $command)
|
||||
{
|
||||
$this->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()));
|
||||
}
|
||||
}
|
76
vendor/symfony/console/Tester/CommandTester.php
vendored
Normal file
76
vendor/symfony/console/Tester/CommandTester.php
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
<?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\Input\ArrayInput;
|
||||
|
||||
/**
|
||||
* Eases the testing of console commands.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
class CommandTester
|
||||
{
|
||||
use TesterTrait;
|
||||
|
||||
private Command $command;
|
||||
|
||||
public function __construct(Command $command)
|
||||
{
|
||||
$this->command = $command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the command.
|
||||
*
|
||||
* 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
|
||||
{
|
||||
// set the command name automatically if the application requires
|
||||
// this argument and no command name was passed
|
||||
if (!isset($input['command'])
|
||||
&& (null !== $application = $this->command->getApplication())
|
||||
&& $application->getDefinition()->hasArgument('command')
|
||||
) {
|
||||
$input = array_merge(['command' => $this->command->getName()], $input);
|
||||
}
|
||||
|
||||
$this->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.
|
||||
$this->input->setStream(self::createStream($this->inputs));
|
||||
|
||||
if (isset($options['interactive'])) {
|
||||
$this->input->setInteractive($options['interactive']);
|
||||
}
|
||||
|
||||
if (!isset($options['decorated'])) {
|
||||
$options['decorated'] = false;
|
||||
}
|
||||
|
||||
$this->initOutput($options);
|
||||
|
||||
return $this->statusCode = $this->command->run($this->input, $this->output);
|
||||
}
|
||||
}
|
43
vendor/symfony/console/Tester/Constraint/CommandIsSuccessful.php
vendored
Normal file
43
vendor/symfony/console/Tester/Constraint/CommandIsSuccessful.php
vendored
Normal file
@ -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);
|
||||
}
|
||||
}
|
178
vendor/symfony/console/Tester/TesterTrait.php
vendored
Normal file
178
vendor/symfony/console/Tester/TesterTrait.php
vendored
Normal file
@ -0,0 +1,178 @@
|
||||
<?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\CommandIsSuccessful;
|
||||
|
||||
/**
|
||||
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
|
||||
*/
|
||||
trait TesterTrait
|
||||
{
|
||||
private StreamOutput $output;
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user inputs.
|
||||
*
|
||||
* @param array $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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return resource
|
||||
*/
|
||||
private static function createStream(array $inputs)
|
||||
{
|
||||
$stream = fopen('php://memory', 'r+', false);
|
||||
|
||||
foreach ($inputs as $input) {
|
||||
fwrite($stream, $input.\PHP_EOL);
|
||||
}
|
||||
|
||||
rewind($stream);
|
||||
|
||||
return $stream;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user