Browse Source

WIP #10840

batch-query-test
Daniel Gomez Pan 9 years ago
parent
commit
a0a44c382a
  1. 16
      framework/console/Controller.php
  2. 5
      framework/console/Request.php
  3. 10
      tests/framework/console/ControllerTest.php
  4. 30
      tests/framework/console/FakeController.php

16
framework/console/Controller.php

@ -87,6 +87,17 @@ class Controller extends \yii\base\Controller
if (!empty($params)) {
// populate options here so that they are available in beforeAction().
$options = $this->options($id === '' ? $this->defaultAction : $id);
if (isset($params['alias'])) {
$shortCuts = $this->shortCuts();
foreach ($params['alias'] as $name => $value) {
if (key_exists($name, $shortCuts)) {
$params[$shortCuts[$name]] = $value;
} else {
throw new Exception(Yii::t('yii', 'Unknown shorcut: -{name}', ['name' => $name]));
}
}
unset($params['alias']);
}
foreach ($params as $name => $value) {
if (in_array($name, $options, true)) {
$default = $this->$name;
@ -295,6 +306,11 @@ class Controller extends \yii\base\Controller
return ['color', 'interactive'];
}
public function shortCuts()
{
return [];
}
/**
* Returns properties corresponding to the options for the action id
* Child classes may override this method to specify possible properties.

5
framework/console/Request.php

@ -71,6 +71,11 @@ class Request extends \yii\base\Request
if ($name !== Application::OPTION_APPCONFIG) {
$params[$name] = isset($matches[3]) ? $matches[3] : true;
}
} elseif (preg_match('/^-(\w+)(=(.*))?$/', $param, $matches)) {
$name = $matches[1];
if ($name !== Application::OPTION_APPCONFIG) {
$params['alias'][$name] = isset($matches[3]) ? $matches[3] : true;
}
} else {
$params[] = $param;
}

10
tests/framework/console/ControllerTest.php

@ -9,7 +9,6 @@ namespace yiiunit\framework\console;
use Yii;
use yiiunit\TestCase;
use yiiunit\framework\di\stubs\Qux;
use yiiunit\framework\web\stubs\Bar;
use yiiunit\framework\web\stubs\OtherQux;
@ -39,6 +38,15 @@ class ControllerTest extends TestCase
$result = $controller->runAction('aksi2', $params);
$this->assertEquals([['d426', 'mdmunir'], 'single'], $result);
$params = ['alias' => ['t' => 'test']];
$result = $controller->runAction('aksi4', $params);
$this->assertEquals('test', $result);
$params = ['alias' => ['ta' => 'from params,notdefault']];
list($fromParam, $other) = $controller->runAction('aksi5', $params);
$this->assertEquals('from params', $fromParam);
$this->assertEquals('notdefault', $other);
$params = ['avaliable'];
$message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]);
$this->setExpectedException('yii\console\Exception', $message);

30
tests/framework/console/FakeController.php

@ -8,9 +8,6 @@
namespace yiiunit\framework\console;
use yii\console\Controller;
use yiiunit\framework\di\stubs\QuxInterface;
use yiiunit\framework\web\stubs\Bar;
use yii\validators\EmailValidator;
/**
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
@ -18,6 +15,23 @@ use yii\validators\EmailValidator;
*/
class FakeController extends Controller
{
public $test;
public $testArray = [];
public function options($actionID)
{
return array_merge(parent::options($actionID), [
'test',
'testArray'
]);
}
public function shortCuts()
{
return ['t' => 'test', 'ta' => 'testArray'];
}
public function actionAksi1($fromParam, $other = 'default')
{
@ -32,4 +46,14 @@ class FakeController extends Controller
public function actionAksi3($available, $missing)
{
}
public function actionAksi4()
{
return $this->test;
}
public function actionAksi5()
{
return $this->testArray;
}
}

Loading…
Cancel
Save