[Console] add setInputs to ApplicationTest and share some code
This commit is contained in:
parent
4c1de3fbff
commit
ea86ed8a0a
@ -13,9 +13,7 @@ namespace Symfony\Component\Console\Tester;
|
|||||||
|
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
use Symfony\Component\Console\Input\ArrayInput;
|
use Symfony\Component\Console\Input\ArrayInput;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
|
||||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
|
||||||
use Symfony\Component\Console\Output\StreamOutput;
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,13 +28,11 @@ use Symfony\Component\Console\Output\StreamOutput;
|
|||||||
*/
|
*/
|
||||||
class ApplicationTester
|
class ApplicationTester
|
||||||
{
|
{
|
||||||
|
use TesterTrait;
|
||||||
|
|
||||||
private $application;
|
private $application;
|
||||||
private $input;
|
private $input;
|
||||||
private $statusCode;
|
private $statusCode;
|
||||||
/**
|
|
||||||
* @var OutputInterface
|
|
||||||
*/
|
|
||||||
private $output;
|
|
||||||
private $captureStreamsIndependently = false;
|
private $captureStreamsIndependently = false;
|
||||||
|
|
||||||
public function __construct(Application $application)
|
public function __construct(Application $application)
|
||||||
@ -66,6 +62,13 @@ class ApplicationTester
|
|||||||
$this->input->setInteractive($options['interactive']);
|
$this->input->setInteractive($options['interactive']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$shellInteractive = getenv('SHELL_INTERACTIVE');
|
||||||
|
|
||||||
|
if ($this->inputs) {
|
||||||
|
$this->input->setStream(self::createStream($this->inputs));
|
||||||
|
putenv('SHELL_INTERACTIVE=1');
|
||||||
|
}
|
||||||
|
|
||||||
$this->captureStreamsIndependently = array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
|
$this->captureStreamsIndependently = array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
|
||||||
if (!$this->captureStreamsIndependently) {
|
if (!$this->captureStreamsIndependently) {
|
||||||
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
|
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
|
||||||
@ -97,27 +100,11 @@ class ApplicationTester
|
|||||||
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
|
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->statusCode = $this->application->run($this->input, $this->output);
|
$this->statusCode = $this->application->run($this->input, $this->output);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
putenv($shellInteractive ? "SHELL_INTERACTIVE=$shellInteractive" : 'SHELL_INTERACTIVE');
|
||||||
* Gets the display returned by the last execution of the application.
|
|
||||||
*
|
|
||||||
* @param bool $normalize Whether to normalize end of lines to \n or not
|
|
||||||
*
|
|
||||||
* @return string The display
|
|
||||||
*/
|
|
||||||
public function getDisplay($normalize = false)
|
|
||||||
{
|
|
||||||
rewind($this->output->getStream());
|
|
||||||
|
|
||||||
$display = stream_get_contents($this->output->getStream());
|
return $this->statusCode;
|
||||||
|
|
||||||
if ($normalize) {
|
|
||||||
$display = str_replace(PHP_EOL, "\n", $display);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $display;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,34 +130,4 @@ class ApplicationTester
|
|||||||
|
|
||||||
return $display;
|
return $display;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the input instance used by the last execution of the application.
|
|
||||||
*
|
|
||||||
* @return InputInterface The current input instance
|
|
||||||
*/
|
|
||||||
public function getInput()
|
|
||||||
{
|
|
||||||
return $this->input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the output instance used by the last execution of the application.
|
|
||||||
*
|
|
||||||
* @return OutputInterface The current output instance
|
|
||||||
*/
|
|
||||||
public function getOutput()
|
|
||||||
{
|
|
||||||
return $this->output;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the status code returned by the last execution of the application.
|
|
||||||
*
|
|
||||||
* @return int The status code
|
|
||||||
*/
|
|
||||||
public function getStatusCode()
|
|
||||||
{
|
|
||||||
return $this->statusCode;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,6 @@ namespace Symfony\Component\Console\Tester;
|
|||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\ArrayInput;
|
use Symfony\Component\Console\Input\ArrayInput;
|
||||||
use Symfony\Component\Console\Output\StreamOutput;
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Eases the testing of console commands.
|
* Eases the testing of console commands.
|
||||||
@ -25,10 +23,10 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
*/
|
*/
|
||||||
class CommandTester
|
class CommandTester
|
||||||
{
|
{
|
||||||
|
use TesterTrait;
|
||||||
|
|
||||||
private $command;
|
private $command;
|
||||||
private $input;
|
private $input;
|
||||||
private $output;
|
|
||||||
private $inputs = array();
|
|
||||||
private $statusCode;
|
private $statusCode;
|
||||||
|
|
||||||
public function __construct(Command $command)
|
public function __construct(Command $command)
|
||||||
@ -78,79 +76,4 @@ class CommandTester
|
|||||||
|
|
||||||
return $this->statusCode = $this->command->run($this->input, $this->output);
|
return $this->statusCode = $this->command->run($this->input, $this->output);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the display returned by the last execution of the command.
|
|
||||||
*
|
|
||||||
* @param bool $normalize Whether to normalize end of lines to \n or not
|
|
||||||
*
|
|
||||||
* @return string The display
|
|
||||||
*/
|
|
||||||
public function getDisplay($normalize = false)
|
|
||||||
{
|
|
||||||
rewind($this->output->getStream());
|
|
||||||
|
|
||||||
$display = stream_get_contents($this->output->getStream());
|
|
||||||
|
|
||||||
if ($normalize) {
|
|
||||||
$display = str_replace(PHP_EOL, "\n", $display);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $display;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the input instance used by the last execution of the command.
|
|
||||||
*
|
|
||||||
* @return InputInterface The current input instance
|
|
||||||
*/
|
|
||||||
public function getInput()
|
|
||||||
{
|
|
||||||
return $this->input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the output instance used by the last execution of the command.
|
|
||||||
*
|
|
||||||
* @return OutputInterface The current output instance
|
|
||||||
*/
|
|
||||||
public function getOutput()
|
|
||||||
{
|
|
||||||
return $this->output;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the status code returned by the last execution of the application.
|
|
||||||
*
|
|
||||||
* @return int The status code
|
|
||||||
*/
|
|
||||||
public function getStatusCode()
|
|
||||||
{
|
|
||||||
return $this->statusCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the user inputs.
|
|
||||||
*
|
|
||||||
* @param array $inputs An array of strings representing each input
|
|
||||||
* passed to the command input stream
|
|
||||||
*
|
|
||||||
* @return CommandTester
|
|
||||||
*/
|
|
||||||
public function setInputs(array $inputs)
|
|
||||||
{
|
|
||||||
$this->inputs = $inputs;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function createStream(array $inputs)
|
|
||||||
{
|
|
||||||
$stream = fopen('php://memory', 'r+', false);
|
|
||||||
|
|
||||||
fwrite($stream, implode(PHP_EOL, $inputs));
|
|
||||||
rewind($stream);
|
|
||||||
|
|
||||||
return $stream;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
103
src/Symfony/Component/Console/Tester/TesterTrait.php
Normal file
103
src/Symfony/Component/Console/Tester/TesterTrait.php
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
<?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\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
trait TesterTrait
|
||||||
|
{
|
||||||
|
/** @var StreamOutput */
|
||||||
|
private $output;
|
||||||
|
private $inputs = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the display returned by the last execution of the command or application.
|
||||||
|
*
|
||||||
|
* @param bool $normalize Whether to normalize end of lines to \n or not
|
||||||
|
*
|
||||||
|
* @return string The display
|
||||||
|
*/
|
||||||
|
public function getDisplay($normalize = false)
|
||||||
|
{
|
||||||
|
rewind($this->output->getStream());
|
||||||
|
|
||||||
|
$display = stream_get_contents($this->output->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.
|
||||||
|
*
|
||||||
|
* @return InputInterface The current input instance
|
||||||
|
*/
|
||||||
|
public function getInput()
|
||||||
|
{
|
||||||
|
return $this->input;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the output instance used by the last execution of the command or application.
|
||||||
|
*
|
||||||
|
* @return OutputInterface The current output instance
|
||||||
|
*/
|
||||||
|
public function getOutput()
|
||||||
|
{
|
||||||
|
return $this->output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the status code returned by the last execution of the command or application.
|
||||||
|
*
|
||||||
|
* @return int The status code
|
||||||
|
*/
|
||||||
|
public function getStatusCode()
|
||||||
|
{
|
||||||
|
return $this->statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the user inputs.
|
||||||
|
*
|
||||||
|
* @param $inputs array An array of strings representing each input
|
||||||
|
* passed to the command input stream
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setInputs(array $inputs)
|
||||||
|
{
|
||||||
|
$this->inputs = $inputs;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function createStream(array $inputs)
|
||||||
|
{
|
||||||
|
$stream = fopen('php://memory', 'r+', false);
|
||||||
|
|
||||||
|
fwrite($stream, implode(PHP_EOL, $inputs));
|
||||||
|
rewind($stream);
|
||||||
|
|
||||||
|
return $stream;
|
||||||
|
}
|
||||||
|
}
|
@ -13,7 +13,9 @@ namespace Symfony\Component\Console\Tests\Tester;
|
|||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||||
use Symfony\Component\Console\Output\Output;
|
use Symfony\Component\Console\Output\Output;
|
||||||
|
use Symfony\Component\Console\Question\Question;
|
||||||
use Symfony\Component\Console\Tester\ApplicationTester;
|
use Symfony\Component\Console\Tester\ApplicationTester;
|
||||||
|
|
||||||
class ApplicationTesterTest extends TestCase
|
class ApplicationTesterTest extends TestCase
|
||||||
@ -27,7 +29,9 @@ class ApplicationTesterTest extends TestCase
|
|||||||
$this->application->setAutoExit(false);
|
$this->application->setAutoExit(false);
|
||||||
$this->application->register('foo')
|
$this->application->register('foo')
|
||||||
->addArgument('foo')
|
->addArgument('foo')
|
||||||
->setCode(function ($input, $output) { $output->writeln('foo'); })
|
->setCode(function ($input, $output) {
|
||||||
|
$output->writeln('foo');
|
||||||
|
})
|
||||||
;
|
;
|
||||||
|
|
||||||
$this->tester = new ApplicationTester($this->application);
|
$this->tester = new ApplicationTester($this->application);
|
||||||
@ -63,6 +67,25 @@ class ApplicationTesterTest extends TestCase
|
|||||||
$this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
|
$this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetInputs()
|
||||||
|
{
|
||||||
|
$application = new Application();
|
||||||
|
$application->setAutoExit(false);
|
||||||
|
$application->register('foo')->setCode(function ($input, $output) {
|
||||||
|
$helper = new QuestionHelper();
|
||||||
|
$helper->ask($input, $output, new Question('Q1'));
|
||||||
|
$helper->ask($input, $output, new Question('Q2'));
|
||||||
|
$helper->ask($input, $output, new Question('Q3'));
|
||||||
|
});
|
||||||
|
$tester = new ApplicationTester($application);
|
||||||
|
|
||||||
|
$tester->setInputs(array('I1', 'I2', 'I3'));
|
||||||
|
$tester->run(array('command' => 'foo'));
|
||||||
|
|
||||||
|
$this->assertSame(0, $tester->getStatusCode());
|
||||||
|
$this->assertEquals('Q1Q2Q3', $tester->getDisplay(true));
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetStatusCode()
|
public function testGetStatusCode()
|
||||||
{
|
{
|
||||||
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
|
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
|
||||||
|
Reference in New Issue
Block a user