Skip to content

Commit

Permalink
add new console command context check luyadev#1927
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar authored and boehsermoe committed Jul 14, 2019
1 parent d49dd9e commit 5f751a2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion core/console/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,8 +18,18 @@
* @author Basil Suter <basil@nadar.io>
* @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.
Expand Down
16 changes: 13 additions & 3 deletions tests/core/console/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions tests/data/commands/TestConsoleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace luyatests\data\commands;

use luya\console\Command;

class TestConsoleCommand extends Command
{
public function actionFoo()
{
return 'bar';
}
}

0 comments on commit 5f751a2

Please sign in to comment.