From 0dc0c10f0e45ac905af29bd1d9d10ad510b95956 Mon Sep 17 00:00:00 2001 From: Basil Date: Sat, 8 Jun 2019 12:15:17 +0200 Subject: [PATCH] add new console command context check #1927 --- core/CHANGELOG.md | 1 + core/console/Controller.php | 16 +++++++++++++++- tests/core/console/ApplicationTest.php | 16 +++++++++++++--- tests/data/commands/TestConsoleCommand.php | 13 +++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 tests/data/commands/TestConsoleCommand.php diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index a12c751c8..78899b971 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. This projec ## 1.0.19 (in progress) ++ [#1927](https://github.com/luyadev/luya/issues/1927) New check whether console command is running in console application context. + [#1925](https://github.com/luyadev/luya/issues/1925) Add new actions and controller retrieve options in object helper. ## 1.0.18 (27. May 2019) diff --git a/core/console/Controller.php b/core/console/Controller.php index 8fe9665d6..30b9379d1 100644 --- a/core/console/Controller.php +++ b/core/console/Controller.php @@ -4,6 +4,10 @@ use Yii; use yii\helpers\Console; +use yii\console\Controller as BaseController; +use luya\helpers\ObjectHelper; +use yii\base\InvalidCallException; +use yii\console\Application; /** * Console Controller base class. @@ -14,8 +18,18 @@ * @author Basil Suter * @since 1.0.0 */ -abstract class Controller extends \yii\console\Controller +abstract class Controller extends BaseController { + public function init() + { + parent::init(); + + // Ensure the console command is running under web application object. + if (!ObjectHelper::isInstanceOf(Yii::$app, Application::class, false)) { + throw new InvalidCallException("The console controller can only run within a console Application context."); + } + } + /** * Helper method to see if the current Application is muted or not. If the Application is muted, no output * will displayed. diff --git a/tests/core/console/ApplicationTest.php b/tests/core/console/ApplicationTest.php index 8c200fd3c..a5421e219 100644 --- a/tests/core/console/ApplicationTest.php +++ b/tests/core/console/ApplicationTest.php @@ -6,19 +6,29 @@ use Yii; use yii\console\Controller; +use luyatests\data\commands\TestConsoleCommand; +use luya\web\Application; class ApplicationTest extends LuyaConsoleTestCase { public function testInvalidCommandException() { $this->expectException('yii\console\Exception'); - Yii::$app->runAction('luya/luya/luya'); + $this->app->runAction('luya/luya/luya'); } public function testInvalidRouteCommand() { - $this->expectException('yii\console\Exception'); - Yii::$app->runAction('consolemodule/test-command/notavailable'); + $this->expectException('yii\console\UnknownCommandException'); + $this->app->runAction('consolemodule/test-command/notavailable'); + } + + public function testInvalidApplicationContext() + { + $module = new Application(['basePath' => dirname(__DIR__), 'id' => 'barfoo']); + $this->expectException("yii\base\InvalidCallException"); + $command = new TestConsoleCommand('console', $module); + $command->actionFoo(); } public function testRouting() diff --git a/tests/data/commands/TestConsoleCommand.php b/tests/data/commands/TestConsoleCommand.php new file mode 100644 index 000000000..37a323816 --- /dev/null +++ b/tests/data/commands/TestConsoleCommand.php @@ -0,0 +1,13 @@ +