Browse Source

Add a test for the Shell::dispatchShell method

Yves P 11 years ago
parent
commit
2991bba1a1

+ 27 - 0
tests/TestCase/Console/ShellTest.php

@@ -23,6 +23,7 @@ use Cake\Filesystem\Folder;
 use Cake\Log\Log;
 use Cake\TestSuite\TestCase;
 use Cake\Utility\Hash;
+use TestApp\Shell\TestingDispatchShell;
 
 /**
  * Class for testing merging vars
@@ -824,6 +825,32 @@ class ShellTest extends TestCase
     }
 
     /**
+     * test calling a shell that dispatch another one
+     *
+     * @return void
+     */
+    public function testDispatchShell()
+    {
+        $this->skipIf(!touch(TEST_APP . 'shell.log'), 'Can\'t write shell test log file');
+        $Shell = new TestingDispatchShell();
+        $Shell->runCommand(['test_task'], true);
+
+        $result = file_get_contents(TEST_APP . 'shell.log');
+        $expected = <<<TEXT
+<info>Welcome to CakePHP v3.0.1 Console</info>
+I am a test task, I dispatch another Shell
+<info>Welcome to CakePHP v3.0.1 Console</info>
+I am a dispatched Shell
+
+TEXT;
+        $this->assertEquals($expected, $result);
+
+        //@codingStandardsIgnoreStart
+        @unlink(TEST_APP . 'shell.log');
+        //@codingStandardsIgnoreEnd
+    }
+
+    /**
      * test wrapBlock wrapping text.
      *
      * @return void

+ 1 - 1
tests/TestCase/Shell/CompletionShellTest.php

@@ -120,7 +120,7 @@ class CompletionShellTest extends TestCase
         $output = $this->out->output;
 
         $expected = "TestPlugin.example TestPlugin.sample " .
-            "TestPluginTwo.example TestPluginTwo.welcome i18n orm_cache plugin server sample\n";
+            "TestPluginTwo.example TestPluginTwo.welcome i18n orm_cache plugin server sample testing_dispatch\n";
         $this->assertTextEquals($expected, $output);
     }
 

+ 51 - 0
tests/test_app/TestApp/Shell/TestingDispatchShell.php

@@ -0,0 +1,51 @@
+<?php
+/**
+ * Testing Dispatch Shell Shell file
+ *
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Shell;
+
+use Cake\Console\Shell;
+use Cake\Core\Configure;
+
+/**
+ * Class for testing dispatchShell functionality
+ */
+class TestingDispatchShell extends Shell
+{
+
+    public $outPath = '';
+
+    protected function _welcome()
+    {
+        $this->out(sprintf('<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
+    }
+
+    public function out($message = null, $newlines = 1, $level = Shell::NORMAL)
+    {
+        file_put_contents(TEST_APP . 'shell.log', $message . "\n", FILE_APPEND);
+    }
+
+    public function testTask()
+    {
+        $this->out('I am a test task, I dispatch another Shell');
+        Configure::write('App.namespace', 'TestApp');
+        $this->dispatchShell('testing_dispatch', 'dispatch_test_task');
+    }
+
+    public function dispatchTestTask()
+    {
+        $this->out('I am a dispatched Shell');
+    }
+}