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

Skip to content

Commit dbbf3fb

Browse files
author
Amrouche Hamza
committed
[Console] add setInputs to ApplicationTest and share some code
1 parent a522e04 commit dbbf3fb

File tree

4 files changed

+95
-119
lines changed

4 files changed

+95
-119
lines changed

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

Lines changed: 2 additions & 51 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,6 +29,8 @@
3029
*/
3130
class ApplicationTester
3231
{
32+
use TesterTrait;
33+
3334
private $application;
3435
private $input;
3536
private $statusCode;
@@ -100,26 +101,6 @@ public function run(array $input, $options = array())
100101
return $this->statusCode = $this->application->run($this->input, $this->output);
101102
}
102103

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-
123104
/**
124105
* Gets the output written to STDERR by the application.
125106
*
@@ -143,34 +124,4 @@ public function getErrorOutput($normalize = false)
143124

144125
return $display;
145126
}
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-
}
176127
}

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

Lines changed: 2 additions & 67 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,6 +23,8 @@
2523
*/
2624
class CommandTester
2725
{
26+
use TesterTrait;
27+
2828
private $command;
2929
private $input;
3030
private $output;
@@ -79,71 +79,6 @@ public function execute(array $input, array $options = array())
7979
return $this->statusCode = $this->command->run($this->input, $this->output);
8080
}
8181

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 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-
14782
private static function createStream(array $inputs)
14883
{
14984
$stream = fopen('php://memory', 'r+', false);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
17+
/**
18+
* @author Amrouche Hamza <[email protected]>
19+
*
20+
* @internal
21+
*/
22+
trait TesterTrait
23+
{
24+
/**
25+
* Gets the display returned by the last execution of the application.
26+
*
27+
* @param bool $normalize Whether to normalize end of lines to \n or not
28+
*
29+
* @return string The display
30+
*/
31+
public function getDisplay($normalize = false)
32+
{
33+
rewind($this->output->getStream());
34+
35+
$display = stream_get_contents($this->output->getStream());
36+
37+
if ($normalize) {
38+
$display = str_replace(PHP_EOL, "\n", $display);
39+
}
40+
41+
return $display;
42+
}
43+
44+
/**
45+
* Gets the input instance used by the last execution of the application.
46+
*
47+
* @return InputInterface The current input instance
48+
*/
49+
public function getInput()
50+
{
51+
return $this->input;
52+
}
53+
54+
/**
55+
* Gets the output instance used by the last execution of the application.
56+
*
57+
* @return OutputInterface The current output instance
58+
*/
59+
public function getOutput()
60+
{
61+
return $this->output;
62+
}
63+
64+
/**
65+
* Gets the status code returned by the last execution of the application.
66+
*
67+
* @return int The status code
68+
*/
69+
public function getStatusCode()
70+
{
71+
return $this->statusCode;
72+
}
73+
74+
/**
75+
* Sets the user inputs.
76+
*
77+
* @param $inputs array an array of strings representing each input
78+
* passed to the command input stream
79+
*
80+
* @return self
81+
*/
82+
public function setInputs(array $inputs)
83+
{
84+
$this->inputs = $inputs;
85+
86+
return $this;
87+
}
88+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ protected function setUp()
2727
$this->application->setAutoExit(false);
2828
$this->application->register('foo')
2929
->addArgument('foo')
30-
->setCode(function ($input, $output) { $output->writeln('foo'); })
30+
->setCode(function ($input, $output) {
31+
$output->writeln('foo');
32+
})
3133
;
3234

3335
$this->tester = new ApplicationTester($this->application);

0 commit comments

Comments
 (0)