From 65757db0aa453dacc88a6cce4f786dd81b2b3f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Fri, 6 Oct 2017 15:51:32 +0200 Subject: [PATCH] #11272 Throw an exception early when invalid task is loaded --- src/Console/Shell.php | 15 ++++++++++++++- tests/TestCase/Console/ShellTest.php | 23 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Console/Shell.php b/src/Console/Shell.php index 72d99af318c..e35a9d58a7f 100644 --- a/src/Console/Shell.php +++ b/src/Console/Shell.php @@ -16,7 +16,7 @@ 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\Text; use ReflectionException; use ReflectionMethod; +use RuntimeException; /** * Base class for command-line utilities for automating programmer chores. @@ -197,6 +198,8 @@ public function __construct(ConsoleIo $io = null) if (isset($this->modelClass)) { $this->loadModel(); } + + $this->loadTasks(); } /** @@ -301,6 +304,16 @@ public function loadTasks() $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; } diff --git a/tests/TestCase/Console/ShellTest.php b/tests/TestCase/Console/ShellTest.php index 16ca9ada558..995a80bc794 100644 --- a/tests/TestCase/Console/ShellTest.php +++ b/tests/TestCase/Console/ShellTest.php @@ -28,7 +28,7 @@ class MergeShell extends Shell { - public $tasks = ['DbConfig', 'Fixture']; + public $tasks = ['Command', 'Extract']; public $modelClass = 'Articles'; } @@ -170,7 +170,7 @@ public function testInitialize() 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 @@ public function testCreateFileNoPermissions() */ 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(); } /**