Skip to content
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

Simperfit
Copy link
Contributor

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.

@Simperfit Simperfit force-pushed the feature/refactor-application-and-command-tester-to-share-code branch from be6fbc2 to dbbf3fb Compare November 4, 2017 18:11
@Simperfit Simperfit changed the title [Console] add setInputs to ApplicationTest and share some code [Console] add setInputs to ApplicationTester and share some code Nov 5, 2017
@ogizanagi ogizanagi added this to the 4.1 milestone Nov 5, 2017
Copy link
Member

@chalasr chalasr left a 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)
Copy link
Member

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;
Copy link
Member

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

Copy link
Member

@ogizanagi ogizanagi Nov 5, 2017

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;

@Simperfit
Copy link
Contributor Author

@chalasr So it seems that the test is failling. Is here something the modify directly in the Application to make this work properly ?

@Simperfit Simperfit force-pushed the feature/refactor-application-and-command-tester-to-share-code branch 2 times, most recently from 370851e to efe61cc Compare November 5, 2017 16:38
*/
trait TesterTrait
{
/** @var StreamOutput */
Copy link
Member

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

Copy link
Member

@ogizanagi ogizanagi Nov 5, 2017

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) :)

screenshot 2017-11-05 a 19 27 06

if (isset($options['interactive'])) {
$this->input->setInteractive($options['interactive']);
}

if ($this->inputs) {
$this->input->setStream(self::createStream($this->inputs));
Copy link
Member

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

@Simperfit Simperfit force-pushed the feature/refactor-application-and-command-tester-to-share-code branch from efe61cc to 8b59c06 Compare November 5, 2017 19:44
@Simperfit
Copy link
Contributor Author

Thanks for the review guys, tests are now green ;).

private $inputs = array();

/**
* Gets the display returned by the last execution of the application.
Copy link
Member

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)
Copy link
Member

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');
Copy link
Member

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

@Simperfit Simperfit force-pushed the feature/refactor-application-and-command-tester-to-share-code branch from 8b59c06 to 3513236 Compare November 5, 2017 20:20
Copy link
Member

@chalasr chalasr left a 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);

Copy link
Member

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 ^^

@Simperfit Simperfit force-pushed the feature/refactor-application-and-command-tester-to-share-code branch from 3513236 to ea86ed8 Compare November 12, 2017 06:57
@chalasr
Copy link
Member

chalasr commented Nov 26, 2017

Thank you @Simperfit.

@chalasr chalasr merged commit ea86ed8 into symfony:master Nov 26, 2017
chalasr pushed a commit that referenced this pull request Nov 26, 2017
… 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
@Simperfit Simperfit deleted the feature/refactor-application-and-command-tester-to-share-code branch December 20, 2017 15:42
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants