annotate vendor/symfony/console/Tester/CommandTester.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Tester;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Command\Command;
Chris@0 15 use Symfony\Component\Console\Input\ArrayInput;
Chris@0 16 use Symfony\Component\Console\Input\InputInterface;
Chris@0 17 use Symfony\Component\Console\Output\OutputInterface;
Chris@17 18 use Symfony\Component\Console\Output\StreamOutput;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Eases the testing of console commands.
Chris@0 22 *
Chris@0 23 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 24 * @author Robin Chalas <robin.chalas@gmail.com>
Chris@0 25 */
Chris@0 26 class CommandTester
Chris@0 27 {
Chris@0 28 private $command;
Chris@0 29 private $input;
Chris@0 30 private $output;
Chris@17 31 private $inputs = [];
Chris@0 32 private $statusCode;
Chris@0 33
Chris@0 34 public function __construct(Command $command)
Chris@0 35 {
Chris@0 36 $this->command = $command;
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Executes the command.
Chris@0 41 *
Chris@0 42 * Available execution options:
Chris@0 43 *
Chris@0 44 * * interactive: Sets the input interactive flag
Chris@0 45 * * decorated: Sets the output decorated flag
Chris@0 46 * * verbosity: Sets the output verbosity flag
Chris@0 47 *
Chris@0 48 * @param array $input An array of command arguments and options
Chris@0 49 * @param array $options An array of execution options
Chris@0 50 *
Chris@0 51 * @return int The command exit code
Chris@0 52 */
Chris@17 53 public function execute(array $input, array $options = [])
Chris@0 54 {
Chris@0 55 // set the command name automatically if the application requires
Chris@0 56 // this argument and no command name was passed
Chris@0 57 if (!isset($input['command'])
Chris@0 58 && (null !== $application = $this->command->getApplication())
Chris@0 59 && $application->getDefinition()->hasArgument('command')
Chris@0 60 ) {
Chris@17 61 $input = array_merge(['command' => $this->command->getName()], $input);
Chris@0 62 }
Chris@0 63
Chris@0 64 $this->input = new ArrayInput($input);
Chris@18 65 // Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
Chris@18 66 $this->input->setStream(self::createStream($this->inputs));
Chris@0 67
Chris@0 68 if (isset($options['interactive'])) {
Chris@0 69 $this->input->setInteractive($options['interactive']);
Chris@0 70 }
Chris@0 71
Chris@0 72 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
Chris@0 73 $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
Chris@0 74 if (isset($options['verbosity'])) {
Chris@0 75 $this->output->setVerbosity($options['verbosity']);
Chris@0 76 }
Chris@0 77
Chris@0 78 return $this->statusCode = $this->command->run($this->input, $this->output);
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * Gets the display returned by the last execution of the command.
Chris@0 83 *
Chris@0 84 * @param bool $normalize Whether to normalize end of lines to \n or not
Chris@0 85 *
Chris@0 86 * @return string The display
Chris@0 87 */
Chris@0 88 public function getDisplay($normalize = false)
Chris@0 89 {
Chris@18 90 if (null === $this->output) {
Chris@18 91 throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?');
Chris@18 92 }
Chris@18 93
Chris@0 94 rewind($this->output->getStream());
Chris@0 95
Chris@0 96 $display = stream_get_contents($this->output->getStream());
Chris@0 97
Chris@0 98 if ($normalize) {
Chris@0 99 $display = str_replace(PHP_EOL, "\n", $display);
Chris@0 100 }
Chris@0 101
Chris@0 102 return $display;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * Gets the input instance used by the last execution of the command.
Chris@0 107 *
Chris@0 108 * @return InputInterface The current input instance
Chris@0 109 */
Chris@0 110 public function getInput()
Chris@0 111 {
Chris@0 112 return $this->input;
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * Gets the output instance used by the last execution of the command.
Chris@0 117 *
Chris@0 118 * @return OutputInterface The current output instance
Chris@0 119 */
Chris@0 120 public function getOutput()
Chris@0 121 {
Chris@0 122 return $this->output;
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Gets the status code returned by the last execution of the application.
Chris@0 127 *
Chris@0 128 * @return int The status code
Chris@0 129 */
Chris@0 130 public function getStatusCode()
Chris@0 131 {
Chris@0 132 return $this->statusCode;
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * Sets the user inputs.
Chris@0 137 *
Chris@14 138 * @param array $inputs An array of strings representing each input
Chris@14 139 * passed to the command input stream
Chris@0 140 *
Chris@0 141 * @return CommandTester
Chris@0 142 */
Chris@0 143 public function setInputs(array $inputs)
Chris@0 144 {
Chris@0 145 $this->inputs = $inputs;
Chris@0 146
Chris@0 147 return $this;
Chris@0 148 }
Chris@0 149
Chris@0 150 private static function createStream(array $inputs)
Chris@0 151 {
Chris@0 152 $stream = fopen('php://memory', 'r+', false);
Chris@0 153
Chris@17 154 foreach ($inputs as $input) {
Chris@17 155 fwrite($stream, $input.PHP_EOL);
Chris@17 156 }
Chris@17 157
Chris@0 158 rewind($stream);
Chris@0 159
Chris@0 160 return $stream;
Chris@0 161 }
Chris@0 162 }