Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Yii Framework 2 Change Log
- Enh #21060: Add PSR-3 logging interoperability with `yii\log\PsrLogger` and `yii\log\PsrTarget` (terabytesoftw)
- Enh #21061: Add precise PHPStan array shapes for logger messages, profiling timings, and target filtering; remove an unreachable dispatcher exception branch (terabytesoftw)
- Bug #21162: Fix standalone actions: run sub-module hooks, short `Action::$id`, opt-in `actionNamespace`, exact `actionMap` routes (terabytesoftw)
- Bug #20694: Fix `yii\console\Controller::getOptionValues()` to return the option values of the given `$actionID` instead of the current action (hrompik)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

2.0.56 under development
------------------------
Expand Down
6 changes: 3 additions & 3 deletions framework/console/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,14 @@ public function optionAliases()
* Returns properties corresponding to the options for the action id
* Child classes may override this method to specify possible properties.
*
* @param string $actionID the action id of the current request
* @param string $actionID the action id to return the option values for.
* If it is empty, the option values of [[defaultAction]] are returned.
* @return array properties corresponding to the options for the action
*/
public function getOptionValues($actionID)
{
// $actionId might be used in subclasses to provide properties specific to action id
$properties = [];
foreach ($this->options($this->action->id) as $property) {
foreach ($this->options($actionID === '' ? $this->defaultAction : $actionID) as $property) {
$properties[$property] = $this->$property;
}

Expand Down
65 changes: 65 additions & 0 deletions tests/framework/console/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,69 @@ public function testGetActionHelpSummaryOnNull(): void
$helpSummary = $controller->getActionHelpSummary(null);
$this->assertEquals($controller->ansiFormat('Action not found.', Console::FG_RED), $helpSummary);
}

/**
* @see https://github.com/yiisoft/yii2/issues/20694
*/
public function testGetOptionValuesUsesGivenActionId(): void
{
$controller = $this->createControllerWithActionSpecificOption();
$controller->action = new InlineAction('index', $controller, 'actionIndex');

self::assertSame(
['color', 'interactive', 'help', 'silentExitOnException'],
array_keys($controller->getOptionValues('index')),
'Options of the given action should be returned.',
);
self::assertSame(
['color', 'interactive', 'help', 'silentExitOnException', 'dryRun'],
array_keys($controller->getOptionValues('deploy')),
'Options of the given action should be returned, not those of the current action.',
);
}

/**
* @see https://github.com/yiisoft/yii2/issues/20694
*/
public function testGetOptionValuesWithoutCurrentAction(): void
{
$controller = $this->createControllerWithActionSpecificOption(dryRun: true);

self::assertNull($controller->action);
self::assertTrue(
$controller->getOptionValues('deploy')['dryRun'],
'Option values should be available before an action is run.',
);
}

/**
* @see https://github.com/yiisoft/yii2/issues/20694
*/
public function testGetOptionValuesForEmptyActionIdUsesDefaultAction(): void
{
$controller = $this->createControllerWithActionSpecificOption(dryRun: true);
$controller->defaultAction = 'deploy';

self::assertTrue(
$controller->getOptionValues('')['dryRun'],
'Options of the default action should be returned for an empty action ID.',
);
}

private function createControllerWithActionSpecificOption(bool $dryRun = false): Controller
{
$controller = new class('fake', Yii::$app) extends Controller {
public bool $dryRun = false;

public function options($actionID)
{
$options = parent::options($actionID);

return $actionID === 'deploy' ? [...$options, 'dryRun'] : $options;
}
};
$controller->dryRun = $dryRun;

return $controller;
}
}
Loading