Переглянути джерело

[Console] added unit tests for DialogHelper

Fabien Potencier 14 роки тому
батько
коміт
5744b520f7

+ 15 - 7
src/Symfony/Component/Console/Helper/DialogHelper.php

@@ -20,6 +20,8 @@ use Symfony\Component\Console\Output\OutputInterface;
  */
  */
 class DialogHelper extends Helper
 class DialogHelper extends Helper
 {
 {
+    private $inputStream;
+
     /**
     /**
      * Asks a question to the user.
      * Asks a question to the user.
      *
      *
@@ -31,13 +33,11 @@ class DialogHelper extends Helper
      */
      */
     public function ask(OutputInterface $output, $question, $default = null)
     public function ask(OutputInterface $output, $question, $default = null)
     {
     {
-        // @codeCoverageIgnoreStart
         $output->write($question);
         $output->write($question);
 
 
-        $ret = trim(fgets(STDIN));
+        $ret = trim(fgets(null === $this->inputStream ? STDIN : $this->inputStream));
 
 
         return $ret ? $ret : $default;
         return $ret ? $ret : $default;
-        // @codeCoverageIgnoreEnd
     }
     }
 
 
     /**
     /**
@@ -53,7 +53,6 @@ class DialogHelper extends Helper
      */
      */
     public function askConfirmation(OutputInterface $output, $question, $default = true)
     public function askConfirmation(OutputInterface $output, $question, $default = true)
     {
     {
-        // @codeCoverageIgnoreStart
         $answer = 'z';
         $answer = 'z';
         while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
         while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
             $answer = $this->ask($output, $question);
             $answer = $this->ask($output, $question);
@@ -64,7 +63,6 @@ class DialogHelper extends Helper
         }
         }
 
 
         return !$answer || 'y' == strtolower($answer[0]);
         return !$answer || 'y' == strtolower($answer[0]);
-        // @codeCoverageIgnoreEnd
     }
     }
 
 
     /**
     /**
@@ -86,7 +84,6 @@ class DialogHelper extends Helper
      */
      */
     public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
     public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
     {
     {
-        // @codeCoverageIgnoreStart
         $error = null;
         $error = null;
         while (false === $attempts || $attempts--) {
         while (false === $attempts || $attempts--) {
             if (null !== $error) {
             if (null !== $error) {
@@ -102,7 +99,18 @@ class DialogHelper extends Helper
         }
         }
 
 
         throw $error;
         throw $error;
-        // @codeCoverageIgnoreEnd
+    }
+
+    /**
+     * Sets the input stream to read from when interacting with the user.
+     *
+     * This is mainly useful for testing purpose.
+     *
+     * @param resource $stream The input stream
+     */
+    public function setInputStream($stream)
+    {
+        $this->inputStream = $stream;
     }
     }
 
 
     /**
     /**

+ 93 - 0
tests/Symfony/Tests/Component/Console/Helper/DialogHelperTest.php

@@ -0,0 +1,93 @@
+<?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\Tests\Component\Console\Helper;
+
+use Symfony\Component\Console\Helper\DialogHelper;
+use Symfony\Component\Console\Helper\HelperSet;
+use Symfony\Component\Console\Helper\FormatterHelper;
+use Symfony\Component\Console\Output\StreamOutput;
+
+class DialogHelperTest extends \PHPUnit_Framework_TestCase
+{
+    public function testAsk()
+    {
+        $dialog = new DialogHelper();
+
+        $dialog->setInputStream($this->getInputStream("\n8AM\n"));
+
+        $this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
+        $this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
+
+        rewind($output->getStream());
+        $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
+    }
+
+    public function testAskConfirmation()
+    {
+        $dialog = new DialogHelper();
+
+        $dialog->setInputStream($this->getInputStream("\n\n"));
+        $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
+        $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
+
+        $dialog->setInputStream($this->getInputStream("y\nyes\n"));
+        $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
+        $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
+
+        $dialog->setInputStream($this->getInputStream("n\nno\n"));
+        $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
+        $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
+    }
+
+    public function testAskAndValidate()
+    {
+        $dialog = new DialogHelper();
+        $helperSet = new HelperSet(array(new FormatterHelper()));
+        $dialog->setHelperSet($helperSet);
+
+        $question ='What color was the white horse of Henry IV?';
+        $error = 'This is not a color!';
+        $validator = function ($color) use ($error) {
+            if (!in_array($color, array('white', 'black'))) {
+                throw new \InvalidArgumentException($error);
+            }
+
+            return $color;
+        };
+
+        $dialog->setInputStream($this->getInputStream("\nblack\n"));
+        $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
+        $this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
+
+        $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
+        try {
+            $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
+            $this->fail();
+        } catch (\InvalidArgumentException $e) {
+            $this->assertEquals($error, $e->getMessage());
+        }
+    }
+
+    protected function getInputStream($input)
+    {
+        $stream = fopen('php://memory', 'r+', false);
+        fputs($stream, $input);
+        rewind($stream);
+
+        return $stream;
+    }
+
+    protected function getOutputStream()
+    {
+        return new StreamOutput(fopen('php://memory', 'r+', false));
+    }
+}

+ 1 - 1
tests/Symfony/Tests/Component/Console/Helper/FormatterHelperTest.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  * file that was distributed with this source code.
  */
  */
 
 
-namespace Symfony\Tests\Component\Console\Formatter;
+namespace Symfony\Tests\Component\Console\Helper;
 
 
 use Symfony\Component\Console\Helper\FormatterHelper;
 use Symfony\Component\Console\Helper\FormatterHelper;