Browse Source

Improve console testing with interactive input

Currently testing shells/commands that use interactive input is tedious
as if you don't provide enough input values the process will lock
waiting for input. This change will raise an exception when you don't
have enough input values making it easier to build tests.
Mark Story 6 years ago
parent
commit
eca8751c32

+ 2 - 13
src/TestSuite/ConsoleIntegrationTestTrait.php

@@ -15,7 +15,6 @@ namespace Cake\TestSuite;
 
 use Cake\Console\Command;
 use Cake\Console\CommandRunner;
-use Cake\Console\ConsoleInput;
 use Cake\Console\ConsoleIo;
 use Cake\Console\Exception\StopException;
 use Cake\Core\Configure;
@@ -25,6 +24,7 @@ use Cake\TestSuite\Constraint\Console\ContentsEmpty;
 use Cake\TestSuite\Constraint\Console\ContentsNotContain;
 use Cake\TestSuite\Constraint\Console\ContentsRegExp;
 use Cake\TestSuite\Constraint\Console\ExitCode;
+use Cake\TestSuite\Stub\ConsoleInput;
 use Cake\TestSuite\Stub\ConsoleOutput;
 
 /**
@@ -81,18 +81,7 @@ trait ConsoleIntegrationTestTrait
 
         $this->_out = new ConsoleOutput();
         $this->_err = new ConsoleOutput();
-        $this->_in = $this->getMockBuilder(ConsoleInput::class)
-            ->disableOriginalConstructor()
-            ->setMethods(['read'])
-            ->getMock();
-
-        $i = 0;
-        foreach ($input as $in) {
-            $this->_in
-                ->expects($this->at($i++))
-                ->method('read')
-                ->will($this->returnValue($in));
-        }
+        $this->_in = new ConsoleInput($input);
 
         $args = $this->commandStringToArgs("cake $command");
         $io = new ConsoleIo($this->_out, $this->_err, $this->_in);

+ 13 - 0
tests/TestCase/TestSuite/ConsoleIntegrationTestTraitTest.php

@@ -13,6 +13,7 @@
  */
 namespace Cake\Test\TestCase\TestSuite;
 
+use Cake\Console\Exception\ConsoleException;
 use Cake\Console\Shell;
 use Cake\Core\Configure;
 use Cake\TestSuite\ConsoleIntegrationTestCase;
@@ -143,6 +144,18 @@ class ConsoleIntegrationTestTraitTest extends ConsoleIntegrationTestCase
     }
 
     /**
+     * tests exec with fewer inputs than questions
+     *
+     * @return void
+     */
+    public function testExecWithMissingInput()
+    {
+        $this->expectException(ConsoleException::class);
+        $this->expectExceptionMessage('no more input');
+        $this->exec('integration bridge', ['cake']);
+    }
+
+    /**
      * tests exec with multiple inputs
      *
      * @return void