Skip to content

Commit 18dd63a

Browse files
author
Robin Chalas
committed
feature #24819 [Console] add setInputs to ApplicationTester and share some code (Simperfit)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Console] add setInputs to ApplicationTester and share some code | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24784 | License | MIT | Doc PR | todo I didn't implemented the tests because I don't know how to write them on ApplicationTester. Commits ------- ea86ed8 [Console] add setInputs to ApplicationTest and share some code
2 parents eb8e2d3 + ea86ed8 commit 18dd63a

File tree

4 files changed

+141
-135
lines changed

4 files changed

+141
-135
lines changed

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

+12-55
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
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;
18-
use Symfony\Component\Console\Output\OutputInterface;
1917
use Symfony\Component\Console\Output\StreamOutput;
2018

2119
/**
@@ -30,13 +28,11 @@
3028
*/
3129
class ApplicationTester
3230
{
31+
use TesterTrait;
32+
3333
private $application;
3434
private $input;
3535
private $statusCode;
36-
/**
37-
* @var OutputInterface
38-
*/
39-
private $output;
4036
private $captureStreamsIndependently = false;
4137

4238
public function __construct(Application $application)
@@ -66,6 +62,13 @@ public function run(array $input, $options = array())
6662
$this->input->setInteractive($options['interactive']);
6763
}
6864

65+
$shellInteractive = getenv('SHELL_INTERACTIVE');
66+
67+
if ($this->inputs) {
68+
$this->input->setStream(self::createStream($this->inputs));
69+
putenv('SHELL_INTERACTIVE=1');
70+
}
71+
6972
$this->captureStreamsIndependently = array_key_exists('capture_stderr_separately', $options) && $options['capture_stderr_separately'];
7073
if (!$this->captureStreamsIndependently) {
7174
$this->output = new StreamOutput(fopen('php://memory', 'w', false));
@@ -97,27 +100,11 @@ public function run(array $input, $options = array())
97100
$streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
98101
}
99102

100-
return $this->statusCode = $this->application->run($this->input, $this->output);
101-
}
102-
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());
103+
$this->statusCode = $this->application->run($this->input, $this->output);
115104

116-
if ($normalize) {
117-
$display = str_replace(PHP_EOL, "\n", $display);
118-
}
105+
putenv($shellInteractive ? "SHELL_INTERACTIVE=$shellInteractive" : 'SHELL_INTERACTIVE');
119106

120-
return $display;
107+
return $this->statusCode;
121108
}
122109

123110
/**
@@ -143,34 +130,4 @@ public function getErrorOutput($normalize = false)
143130

144131
return $display;
145132
}
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-
}
176133
}

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

+2-79
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
}
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 <fabien@symfony.com>
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 <hamza.simperfit@gmail.com>
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 command or 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 command or 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 command or 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 command or 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+
private 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

+24-1
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)