Skip to content

Added getter and setter of QuestionHelper to SymfonyStyle #17136

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

Closed
wants to merge 1 commit into from

Conversation

SCIF
Copy link
Contributor

@SCIF SCIF commented Dec 25, 2015

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Current SymfonyStyle implementation disallow to test interactively input of command according docs.

@SCIF
Copy link
Contributor Author

SCIF commented Dec 25, 2015

I did ./phpunit src/Symfony/Component/Console/ according contribute docs and it said «Ok». Also, process fail seems odd and not related to my changes.

@@ -356,6 +357,22 @@ public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
}

/**
* @return QuestionHelper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also be null

@Tobion
Copy link
Contributor

Tobion commented Dec 25, 2015

What do you need the getter for?

@ogizanagi
Copy link
Contributor

IMO, this should be merged in 2.7 and should add SymfonyStyle tests for the questions rendering, as done for other methods in SymfonyStyleTest.

As @Tobion said, I also think the getter won't be useful.

However, even with this PR, commands using the SymfonyStyle helper cannot be easily tested interactively, because on the contrary of the QuestionHelper, SymfonyQuestionHelper isn't registered in the helper set. So it'll require a setInputStream method or whatever in the command class. :/

@SCIF
Copy link
Contributor Author

SCIF commented Jan 14, 2016

@Tobion, here is one example of using getter. Getter required to allow passthrough input stream though different helpers if you want to replace ones.

@ogizanagi

SymfonyQuestionHelper isn't registered in the helper set

It used by laravel framework for example (btw, as main helper, not in helperset). Symfony is component kit and should provide flexible features as i think.

@SCIF SCIF force-pushed the symfonystyle-patch branch from 9057999 to e5087fe Compare January 14, 2016 16:14
@SCIF
Copy link
Contributor Author

SCIF commented Jan 14, 2016

Fixed phpdoc. Thanks!

@xabbuh
Copy link
Member

xabbuh commented Jan 14, 2016

@SCIF The code you linked to doesn't make use of such a getter and I don't see why that would be necessary (the console helper set is something different).

@SCIF
Copy link
Contributor Author

SCIF commented Jan 15, 2016

@xabbuh , that example shown that input stream sometimes required. If i want to replace QuestionHelper via setter — i would want to passtrough input stream hinted to original version of QuestionHelper to my own. It's not only testing purposes. Testing can be done without setter but substitution of QuestionHelper by different implementation can't be done without getter (to take user's input stream from it) and setter (to set new helper with hinted user's input stream).

@localheinz
Copy link
Contributor

@SCIF

Do you want to have a look at #17468? @xabbuh was so friendly to point out that the referenced PR is a duplicate of this one here, do you have an opinion (would you like to follow up with tests here, shall I close mine)?

@xabbuh
Copy link
Member

xabbuh commented Jan 22, 2016

@SCIF Imho then it makes more sense to let the SymfonyStyle class return the input stream directly (via some method like getInputStream()).

@SCIF
Copy link
Contributor Author

SCIF commented Jan 22, 2016

@xabbuh , i really don't know why you think that getter of QuestionHelper is bad idea. But i can replace getter of Helper with InputStream getter if you insist. My arguments are above, you decide please something and it should be closed.

@localheinz , thank you for your tests and work! Let i cherry pick commit with your tests and #17468 can be closed, isn't it?

@localheinz
Copy link
Contributor

@SCIF

👍 If you like, go ahead!

@@ -356,6 +357,22 @@ public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
}

/**
* @return QuestionHelper|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally dislike the idea of creating a getter which can return either an object or null. It will introduce additional complexity above - the need of checking if the given result is an instance of QuestionHelper

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just create a SymfonyQuestionHelper in __construct(),
so getQuestionHelp() can always return an instance of QuestionHelper

for example:

diff --git a/Style/SymfonyStyle.php b/Style/SymfonyStyle.php
index 47d7d12..3402918 100644
--- a/Style/SymfonyStyle.php
+++ b/Style/SymfonyStyle.php
@@ -47,6 +47,7 @@ class SymfonyStyle extends OutputStyle
     public function __construct(InputInterface $input, OutputInterface $output)
     {
         $this->input = $input;
+        $this->questionHelper = new SymfonyQuestionHelper();
         $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
         // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
         $this->lineLength = min($this->getTerminalWidth() - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
@@ -323,10 +324,6 @@ class SymfonyStyle extends OutputStyle
             $this->autoPrependBlock();
         }

-        if (!$this->questionHelper) {
-            $this->questionHelper = new SymfonyQuestionHelper();
-        }
-
         $answer = $this->questionHelper->ask($this->input, $this, $question);

         if ($this->input->isInteractive()) {
@@ -356,6 +353,14 @@ class SymfonyStyle extends OutputStyle
     }

     /**
+     * @return QuestionHelper
+     */
+    public function getQuestionHelper()
+    {
+        return $this->questionHelper;
+    }
+
+    /**
      * {@inheritdoc}
      */
     public function newLine($count = 1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel like creating a SymfonyQuestionHelper object in the constructor is a good idea. There are many use cases where it's not used at all. Still, getQuestionHelper use case is very unclear for me, could you explain your point of view a little bit more please?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i want to test the action of interactively input:

  1. creating a SymfonyQuestionHelper object in the constructor for making code simpler.
  2. use getQuestionHelper() to get the SymfonyQuestionHelper object for test interactively input with mock via setInputStream()

@SCIF
Copy link
Contributor Author

SCIF commented Mar 3, 2016

@javiereguiluz , which style getter do you want to be added by me? I asked that but didn't get any answer. Just don't want to push fixes for getQuestionHelper() and get decline of it by some of symfony members. I want to arrive at any decision and implement it.

@sstok
Copy link
Contributor

sstok commented May 12, 2016

Is there any chance of getting this merged? I'm currently using a hack to make my tests work, but I want to use a custom class to display a help text below the label and remove the "required" validator.

@fabpot
Copy link
Member

fabpot commented Jun 15, 2016

Closing in favor of #18710 and #18999

@fabpot fabpot closed this Jun 15, 2016
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.