-
-
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
[Console] add setInputs to ApplicationTester and share some code #24819
Conversation
be6fbc2
to
dbbf3fb
Compare
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.
here is a test case
diff --git a/src/Symfony/Component/Console/Tests/Tester/ApplicationTesterTest.php b/src/Symfony/Component/Console/Tests/Tester/ApplicationTesterTest.php
index 062d414..0cc002d 100644
--- a/src/Symfony/Component/Console/Tests/Tester/ApplicationTesterTest.php
+++ b/src/Symfony/Component/Console/Tests/Tester/ApplicationTesterTest.php
@@ -13,7 +13,9 @@ namespace Symfony\Component\Console\Tests\Tester;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
+use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Output\Output;
+use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Tester\ApplicationTester;
class ApplicationTesterTest extends TestCase
@@ -65,6 +67,22 @@ class ApplicationTesterTest extends TestCase
$this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
}
+ public function testSetInputs()
+ {
+ $this->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'));
+ });
+
+ $this->tester->setInputs(array('I1', 'I2', 'I3'));
+ $this->tester->run(array('command' => 'foo'));
+
+ $this->assertSame(0, $this->tester->getStatusCode());
+ $this->assertEquals('Q1Q2Q3', $this->tester->getDisplay(true));
+ }
+
public function testGetStatusCode()
{
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
* | ||
* @return self | ||
*/ | ||
public function setInputs(array $inputs) |
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.
For this to work in ApplicationTester, the tester needs to set the input stream from this value.
So CommandTester::createInputStream()
should be shared between both testers (that's my motivation for a trait) and this line should be added to ApplicationTester::run()
.
*/ | ||
public function setInputs(array $inputs) | ||
{ | ||
$this->inputs = $inputs; |
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.
the property does not exist in ApplicationTester, it should be created
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.
or moved to the trait along with:
/** @var StreamOutput */
private $output;
@chalasr So it seems that the test is failling. Is here something the modify directly in the Application to make this work properly ? |
370851e
to
efe61cc
Compare
*/ | ||
trait TesterTrait | ||
{ | ||
/** @var StreamOutput */ |
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 it
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.
To me it is useful actually (for autocompletion and SCA). This trait needs a StreamOutput
instance here, not any OutputInterface
. That's why I suggested this docblock in #24819 (comment) :)
if (isset($options['interactive'])) { | ||
$this->input->setInteractive($options['interactive']); | ||
} | ||
|
||
if ($this->inputs) { | ||
$this->input->setStream(self::createStream($this->inputs)); |
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.
Here is the failure culprit, I suggest to putenv('SHELL_INTERACTIVE=1')
here and, after run()
, either destroy it or restore its previous value if any
efe61cc
to
8b59c06
Compare
Thanks for the review guys, tests are now green ;). |
private $inputs = array(); | ||
|
||
/** | ||
* Gets the display returned by the last execution of the application. |
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.
command or application
, same below
return $this; | ||
} | ||
|
||
public static function createStream(array $inputs) |
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.
this one must stay private
rewind($this->output->getStream()); | ||
|
||
$display = stream_get_contents($this->output->getStream()); | ||
putenv('SHELL_INTERACTIVE'); |
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.
putenv($shellInteractive ? "SHELL_INTERACTIVE=$shellInteractive" : 'SHELL_INTERACTIVE');
instead, removing the condition below
8b59c06
to
3513236
Compare
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.
👍
@@ -62,10 +58,18 @@ public function __construct(Application $application) | |||
public function run(array $input, $options = array()) | |||
{ | |||
$this->input = new ArrayInput($input); | |||
|
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.
nitpicking but should be reverted ^^
3513236
to
ea86ed8
Compare
Thank you @Simperfit. |
… 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
I didn't implemented the tests because I don't know how to write them on ApplicationTester.