Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 370851e

Browse files
author
Amrouche Hamza
committed
[Console] add setInputs to ApplicationTest and share some code
1 parent 4c1de3f commit 370851e

File tree

4 files changed

+136
-135
lines changed

4 files changed

+136
-135
lines changed

src/Symfony/Component/Console/Tester/ApplicationTester.php

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Console\Application;
1515
use Symfony\Component\Console\Input\ArrayInput;
16-
use Symfony\Component\Console\Input\InputInterface;
1716
use Symfony\Component\Console\Output\ConsoleOutput;
1817
use Symfony\Component\Console\Output\OutputInterface;
1918
use Symfony\Component\Console\Output\StreamOutput;
@@ -30,13 +29,11 @@
3029
*/
3130
class ApplicationTester
3231
{
32+
use TesterTrait;
33+
3334
private $application;
3435
private $input;
3536
private $statusCode;
36-
/**
37-
* @var OutputInterface
38-
*/
39-
private $output;
4037
private $captureStreamsIndependently = false;
4138

4239
public function __construct(Application $application)
@@ -62,10 +59,15 @@ public function __construct(Application $application)
6259
public function run(array $input, $options = array())
6360
{
6461
$this->input = new ArrayInput($input);
62+
6563
if (isset($options['interactive'])) {
6664
$this->input->setInteractive($options['interactive']);
6765
}
6866

67+
if ($this->inputs) {
68+
$this->input->setStream(self::createStream($this->inputs));
69+
}
70+
6971
$this->captureStreamsIndependently = array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
7072
if (!$this->captureStreamsIndependently) {
7173
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
@@ -100,26 +102,6 @@ public function run(array $input, $options = array())
100102
return $this->statusCode = $this->application->run($this->input, $this->output);
101103
}
102104

103-
/**
104-
* Gets the display returned by the last execution of the application.
105-
*
106-
* @param bool $normalize Whether to normalize end of lines to \n or not
107-
*
108-
* @return string The display
109-
*/
110-
public function getDisplay($normalize = false)
111-
{
112-
rewind($this->output->getStream());
113-
114-
$display = stream_get_contents($this->output->getStream());
115-
116-
if ($normalize) {
117-
$display = str_replace(PHP_EOL, "\n", $display);
118-
}
119-
120-
return $display;
121-
}
122-
123105
/**
124106
* Gets the output written to STDERR by the application.
125107
*
@@ -143,34 +125,4 @@ public function getErrorOutput($normalize = false)
143125

144126
return $display;
145127
}
146-
147-
/**
148-
* Gets the input instance used by the last execution of the application.
149-
*
150-
* @return InputInterface The current input instance
151-
*/
152-
public function getInput()
153-
{
154-
return $this->input;
155-
}
156-
157-
/**
158-
* Gets the output instance used by the last execution of the application.
159-
*
160-
* @return OutputInterface The current output instance
161-
*/
162-
public function getOutput()
163-
{
164-
return $this->output;
165-
}
166-
167-
/**
168-
* Gets the status code returned by the last execution of the application.
169-
*
170-
* @return int The status code
171-
*/
172-
public function getStatusCode()
173-
{
174-
return $this->statusCode;
175-
}
176128
}

src/Symfony/Component/Console/Tester/CommandTester.php

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Input\ArrayInput;
1616
use Symfony\Component\Console\Output\StreamOutput;
17-
use Symfony\Component\Console\Input\InputInterface;
18-
use Symfony\Component\Console\Output\OutputInterface;
1917

2018
/**
2119
* Eases the testing of console commands.
@@ -25,10 +23,10 @@
2523
*/
2624
class CommandTester
2725
{
26+
use TesterTrait;
27+
2828
private $command;
2929
private $input;
30-
private $output;
31-
private $inputs = array();
3230
private $statusCode;
3331

3432
public function __construct(Command $command)
@@ -78,79 +76,4 @@ public function execute(array $input, array $options = array())
7876

7977
return $this->statusCode = $this->command->run($this->input, $this->output);
8078
}
81-
82-
/**
83-
* Gets the display returned by the last execution of the command.
84-
*
85-
* @param bool $normalize Whether to normalize end of lines to \n or not
86-
*
87-
* @return string The display
88-
*/
89-
public function getDisplay($normalize = false)
90-
{
91-
rewind($this->output->getStream());
92-
93-
$display = stream_get_contents($this->output->getStream());
94-
95-
if ($normalize) {
96-
$display = str_replace(PHP_EOL, "\n", $display);
97-
}
98-
99-
return $display;
100-
}
101-
102-
/**
103-
* Gets the input instance used by the last execution of the command.
104-
*
105-
* @return InputInterface The current input instance
106-
*/
107-
public function getInput()
108-
{
109-
return $this->input;
110-
}
111-
112-
/**
113-
* Gets the output instance used by the last execution of the command.
114-
*
115-
* @return OutputInterface The current output instance
116-
*/
117-
public function getOutput()
118-
{
119-
return $this->output;
120-
}
121-
122-
/**
123-
* Gets the status code returned by the last execution of the application.
124-
*
125-
* @return int The status code
126-
*/
127-
public function getStatusCode()
128-
{
129-
return $this->statusCode;
130-
}
131-
132-
/**
133-
* Sets the user inputs.
134-
*
135-
* @param array $inputs An array of strings representing each input
136-
* passed to the command input stream
137-
*
138-
* @return CommandTester
139-
*/
140-
public function setInputs(array $inputs)
141-
{
142-
$this->inputs = $inputs;
143-
144-
return $this;
145-
}
146-
147-
private static function createStream(array $inputs)
148-
{
149-
$stream = fopen('php://memory', 'r+', false);
150-
151-
fwrite($stream, implode(PHP_EOL, $inputs));
152-
rewind($stream);
153-
154-
return $stream;
155-
}
15679
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tester;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\Console\Output\StreamOutput;
17+
18+
/**
19+
* @author Amrouche Hamza <[email protected]>
20+
*
21+
* @internal
22+
*/
23+
trait TesterTrait
24+
{
25+
/** @var StreamOutput */
26+
private $output;
27+
private $inputs = array();
28+
29+
/**
30+
* Gets the display returned by the last execution of the application.
31+
*
32+
* @param bool $normalize Whether to normalize end of lines to \n or not
33+
*
34+
* @return string The display
35+
*/
36+
public function getDisplay($normalize = false)
37+
{
38+
rewind($this->output->getStream());
39+
40+
$display = stream_get_contents($this->output->getStream());
41+
42+
if ($normalize) {
43+
$display = str_replace(PHP_EOL, "\n", $display);
44+
}
45+
46+
return $display;
47+
}
48+
49+
/**
50+
* Gets the input instance used by the last execution of the application.
51+
*
52+
* @return InputInterface The current input instance
53+
*/
54+
public function getInput()
55+
{
56+
return $this->input;
57+
}
58+
59+
/**
60+
* Gets the output instance used by the last execution of the application.
61+
*
62+
* @return OutputInterface The current output instance
63+
*/
64+
public function getOutput()
65+
{
66+
return $this->output;
67+
}
68+
69+
/**
70+
* Gets the status code returned by the last execution of the application.
71+
*
72+
* @return int The status code
73+
*/
74+
public function getStatusCode()
75+
{
76+
return $this->statusCode;
77+
}
78+
79+
/**
80+
* Sets the user inputs.
81+
*
82+
* @param $inputs array An array of strings representing each input
83+
* passed to the command input stream
84+
*
85+
* @return self
86+
*/
87+
public function setInputs(array $inputs)
88+
{
89+
$this->inputs = $inputs;
90+
91+
return $this;
92+
}
93+
94+
public static function createStream(array $inputs)
95+
{
96+
$stream = fopen('php://memory', 'r+', false);
97+
98+
fwrite($stream, implode(PHP_EOL, $inputs));
99+
rewind($stream);
100+
101+
return $stream;
102+
}
103+
}

src/Symfony/Component/Console/Tests/Tester/ApplicationTesterTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Helper\QuestionHelper;
1617
use Symfony\Component\Console\Output\Output;
18+
use Symfony\Component\Console\Question\Question;
1719
use Symfony\Component\Console\Tester\ApplicationTester;
1820

1921
class ApplicationTesterTest extends TestCase
@@ -27,7 +29,9 @@ protected function setUp()
2729
$this->application->setAutoExit(false);
2830
$this->application->register('foo')
2931
->addArgument('foo')
30-
->setCode(function ($input, $output) { $output->writeln('foo'); })
32+
->setCode(function ($input, $output) {
33+
$output->writeln('foo');
34+
})
3135
;
3236

3337
$this->tester = new ApplicationTester($this->application);
@@ -63,6 +67,25 @@ public function testGetDisplay()
6367
$this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
6468
}
6569

70+
public function testSetInputs()
71+
{
72+
$application = new Application();
73+
$application->setAutoExit(false);
74+
$application->register('foo')->setCode(function ($input, $output) {
75+
$helper = new QuestionHelper();
76+
$helper->ask($input, $output, new Question('Q1'));
77+
$helper->ask($input, $output, new Question('Q2'));
78+
$helper->ask($input, $output, new Question('Q3'));
79+
});
80+
$tester = new ApplicationTester($application);
81+
82+
$tester->setInputs(array('I1', 'I2', 'I3'));
83+
$tester->run(array('command' => 'foo'));
84+
85+
$this->assertSame(0, $tester->getStatusCode());
86+
$this->assertEquals('Q1Q2Q3', $tester->getDisplay(true));
87+
}
88+
6689
public function testGetStatusCode()
6790
{
6891
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');

0 commit comments

Comments
 (0)