Browse Source

Backport the 3.1 console output stub class

AD7six 10 years ago
parent
commit
c862069ab4
2 changed files with 68 additions and 18 deletions
  1. 62 0
      src/TestSuite/Stub/ConsoleOutput.php
  2. 6 18
      tests/TestCase/Shell/CommandListShellTest.php

+ 62 - 0
src/TestSuite/Stub/ConsoleOutput.php

@@ -0,0 +1,62 @@
+<?php
+/**
+ * CakePHP :  Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP Project
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite\Stub;
+
+use Cake\Console\ConsoleOutput as ConsoleOutputBase;
+
+/**
+ * StubOutput makes testing shell commands/shell helpers easier.
+ *
+ * You can use this class by injecting it into a ConsoleIo instance
+ * that your command/task/helper uses:
+ *
+ * ```
+ * use Cake\Console\ConsoleIo;
+ * use Cake\TestSuite\Stub\ConsoleOutput;
+ *
+ * $output = new ConsoleOutput();
+ * $io = new ConsoleIo($output);
+ * ```
+ */
+class ConsoleOutput extends ConsoleOutputBase
+{
+    /**
+     * Buffered messages.
+     *
+     * @var array
+     */
+    protected $_out = [];
+
+    /**
+     * Write output to the buffer.
+     *
+     * @param string $message The message to write.
+     * @param int $newlines Unused.
+     * @return void
+     */
+    public function write($message, $newlines = 1)
+    {
+        $this->_out[] = $message;
+    }
+
+    /**
+     * Get the buffered output.
+     *
+     * @return array
+     */
+    public function messages()
+    {
+        return $this->_out;
+    }
+}

+ 6 - 18
tests/TestCase/Shell/CommandListShellTest.php

@@ -17,28 +17,14 @@
 namespace Cake\Test\TestCase\Shell;
 
 use Cake\Console\ConsoleIo;
-use Cake\Console\ConsoleOutput;
 use Cake\Core\App;
 use Cake\Core\Plugin;
 use Cake\Shell\CommandListShell;
 use Cake\Shell\Task\CommandTask;
+use Cake\TestSuite\Stub\ConsoleOutput;
 use Cake\TestSuite\TestCase;
 
 /**
- * Class TestStringOutput
- */
-class TestStringOutput extends ConsoleOutput
-{
-
-    public $output = '';
-
-    protected function _write($message)
-    {
-        $this->output .= $message;
-    }
-}
-
-/**
  * Class CommandListShellTest
  *
  */
@@ -55,7 +41,7 @@ class CommandListShellTest extends TestCase
         parent::setUp();
         Plugin::load(['TestPlugin', 'TestPluginTwo']);
 
-        $this->out = new TestStringOutput();
+        $this->out = new ConsoleOutput();
         $io = new ConsoleIo($this->out);
 
         $this->Shell = $this->getMock(
@@ -91,7 +77,8 @@ class CommandListShellTest extends TestCase
     public function testMain()
     {
         $this->Shell->main();
-        $output = $this->out->output;
+        $output = $this->out->messages();
+        $output = implode("\n", $output);
 
         $expected = "/\[.*TestPlugin.*\] example/";
         $this->assertRegExp($expected, $output);
@@ -116,7 +103,8 @@ class CommandListShellTest extends TestCase
         $this->Shell->params['xml'] = true;
         $this->Shell->main();
 
-        $output = $this->out->output;
+        $output = $this->out->messages();
+        $output = implode("\n", $output);
 
         $find = '<shell name="sample" call_as="sample" provider="app" help="sample -h"';
         $this->assertContains($find, $output);