Browse Source

Added input mocking to cli integration test case

Jeremy Harris 8 years ago
parent
commit
8be905e60a

+ 23 - 1
src/TestSuite/ConsoleIntegrationTestCase.php

@@ -62,11 +62,21 @@ class ConsoleIntegrationTestCase extends TestCase
      * Runs cli integration test
      *
      * @param string $command Command to run
+     * @param array $input Input values to pass to an interactive shell
      * @return void
      */
-    public function cli($command)
+    public function cli($command, array $input = [])
     {
         $dispatcher = $this->_makeDispatcher("bin/cake $command");
+
+        $i = 0;
+        foreach ($input as $in) {
+            $this->_in
+                ->expects($this->at($i++))
+                ->method('read')
+                ->will($this->returnValue($in));
+        }
+
         $this->_exitCode = $dispatcher->dispatch();
     }
 
@@ -122,6 +132,18 @@ class ConsoleIntegrationTestCase extends TestCase
     }
 
     /**
+     * Asserts `stderr` contains expected output
+     *
+     * @param string $expected Expected output
+     * @return void
+     */
+    public function assertErrorContains($expected)
+    {
+        $output = implode(PHP_EOL, $this->_err->messages());
+        $this->assertContains($expected, $output);
+    }
+
+    /**
      * Builds the appropriate command dispatcher
      *
      * @param string $command Command

+ 39 - 0
tests/TestCase/TestSuite/ConsoleIntegrationTestCaseTest.php

@@ -2,12 +2,25 @@
 namespace Cake\Test\TestCase\TestSuite;
 
 use Cake\Console\Shell;
+use Cake\Core\Configure;
 use Cake\TestSuite\ConsoleIntegrationTestCase;
 
 class ConsoleIntegrationTestCaseTest extends ConsoleIntegrationTestCase
 {
 
     /**
+     * setUp
+     *
+     * @return void
+     */
+    public function setUp()
+    {
+        parent::setUp();
+
+        Configure::write('App.namespace', 'TestApp');
+    }
+
+    /**
      * tests cli
      *
      * @return void
@@ -34,6 +47,32 @@ class ConsoleIntegrationTestCaseTest extends ConsoleIntegrationTestCase
     }
 
     /**
+     * tests cli with input
+     *
+     * @return void
+     */
+    public function testCliWithInput()
+    {
+        $this->cli('sample bridge', ['javascript']);
+
+        $this->assertErrorContains('No!');
+        $this->assertExitCode(Shell::CODE_ERROR);
+    }
+
+    /**
+     * tests cli with multiple inputs
+     *
+     * @return void
+     */
+    public function testCliWithMultipleInput()
+    {
+        $this->cli('sample bridge', ['cake', 'blue']);
+
+        $this->assertOutputContains('You may pass');
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+    }
+
+    /**
      * tests _commandStringToArgs
      *
      * @return void

+ 24 - 0
tests/test_app/TestApp/Shell/SampleShell.php

@@ -56,4 +56,28 @@ class SampleShell extends Shell
     {
         return 99;
     }
+
+    /**
+     * Bridge of Death question
+     *
+     * @return void
+     */
+    public function bridge()
+    {
+        $name = $this->in('What is your name');
+
+        if ($name !== 'cake') {
+            $this->err('No!');
+            $this->_stop(Shell::CODE_ERROR);
+        }
+
+        $color = $this->in('What is your favorite color?');
+
+        if ($color !== 'blue') {
+            $this->err('Wrong! <blink>Aaaahh</blink>');
+            $this->_stop(Shell::CODE_ERROR);
+        }
+
+        $this->out('You may pass.');
+    }
 }