-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] add setInputs to ApplicationTester and share some code #24819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this to work in ApplicationTester, the tester needs to set the input stream from this value. |
||
{ | ||
$this->inputs = $inputs; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the property does not exist in ApplicationTester, it should be created There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or moved to the trait along with: /** @var StreamOutput */
private $output; |
||
|
||
return $this; | ||
} | ||
|
||
private static function createStream(array $inputs) | ||
{ | ||
$stream = fopen('php://memory', 'r+', false); | ||
|
||
fwrite($stream, implode(PHP_EOL, $inputs)); | ||
rewind($stream); | ||
|
||
return $stream; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not useful and differs with
getOutput()
return type, I would drop itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me it is useful actually (for autocompletion and SCA). This trait needs a
StreamOutput
instance here, not anyOutputInterface
. That's why I suggested this docblock in #24819 (comment) :)