Browse Source

#11272 Throw an exception early when invalid task is loaded

Florian Krämer 8 years ago
parent
commit
65757db0aa
2 changed files with 32 additions and 6 deletions
  1. 14 1
      src/Console/Shell.php
  2. 18 5
      tests/TestCase/Console/ShellTest.php

+ 14 - 1
src/Console/Shell.php

@@ -16,7 +16,7 @@ namespace Cake\Console;
 
 use Cake\Console\Exception\ConsoleException;
 use Cake\Console\Exception\StopException;
-use Cake\Core\Configure;
+use Cake\Core\App;
 use Cake\Core\Plugin;
 use Cake\Datasource\ModelAwareTrait;
 use Cake\Filesystem\File;
@@ -27,6 +27,7 @@ use Cake\Utility\MergeVariablesTrait;
 use Cake\Utility\Text;
 use ReflectionException;
 use ReflectionMethod;
+use RuntimeException;
 
 /**
  * Base class for command-line utilities for automating programmer chores.
@@ -197,6 +198,8 @@ class Shell
         if (isset($this->modelClass)) {
             $this->loadModel();
         }
+
+        $this->loadTasks();
     }
 
     /**
@@ -301,6 +304,16 @@ class Shell
         $this->_taskMap = $this->Tasks->normalizeArray((array)$this->tasks);
         $this->taskNames = array_merge($this->taskNames, array_keys($this->_taskMap));
 
+        foreach ($this->_taskMap as $taskName => $task) {
+            $class = App::className($task['class'], 'Shell/Task', 'Task');
+            if (!class_exists($class)) {
+                throw new RuntimeException(sprintf(
+                    'Task `%s` not found. Maybe you made a typo or a plugin is missing or not loaded?',
+                    $taskName
+                ));
+            }
+        }
+
         return true;
     }
 

+ 18 - 5
tests/TestCase/Console/ShellTest.php

@@ -28,7 +28,7 @@ use TestApp\Shell\TestingDispatchShell;
 class MergeShell extends Shell
 {
 
-    public $tasks = ['DbConfig', 'Fixture'];
+    public $tasks = ['Command', 'Extract'];
 
     public $modelClass = 'Articles';
 }
@@ -170,7 +170,7 @@ class ShellTest extends TestCase
         static::setAppNamespace();
 
         Plugin::load('TestPlugin');
-        $this->Shell->tasks = ['DbConfig' => ['one', 'two']];
+        $this->Shell->tasks = ['Extract' => ['one', 'two']];
         $this->Shell->plugin = 'TestPlugin';
         $this->Shell->modelClass = 'TestPlugin.TestPluginComments';
         $this->Shell->initialize();
@@ -704,15 +704,28 @@ class ShellTest extends TestCase
      */
     public function testHasTask()
     {
-        $this->Shell->tasks = ['Extract', 'DbConfig'];
+        $this->Shell->tasks = ['Extract', 'Assets'];
         $this->Shell->loadTasks();
 
         $this->assertTrue($this->Shell->hasTask('extract'));
         $this->assertTrue($this->Shell->hasTask('Extract'));
         $this->assertFalse($this->Shell->hasTask('random'));
 
-        $this->assertTrue($this->Shell->hasTask('db_config'));
-        $this->assertTrue($this->Shell->hasTask('DbConfig'));
+        $this->assertTrue($this->Shell->hasTask('assets'));
+        $this->assertTrue($this->Shell->hasTask('Assets'));
+    }
+
+    /**
+     * test task loading exception
+     *
+     * @expectedException \RuntimeException
+     * @expectedExceptionMessage Task `DoesNotExist` not found. Maybe you made a typo or a plugin is missing or not loaded?
+     * @return void
+     */
+    public function testMissingTaskException()
+    {
+        $this->Shell->tasks = ['DoesNotExist'];
+        $this->Shell->loadTasks();
     }
 
     /**