Skip to content

Commit ce6ec71

Browse files
author
Bohdan Korablov
committed
MAGETWO-63382: CLI Improvements: Configuration management - Command config:show
1 parent 3a11eda commit ce6ec71

File tree

9 files changed

+429
-96
lines changed

9 files changed

+429
-96
lines changed

app/code/Magento/Config/App/Config/Source/EnvironmentConfigSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Framework\Stdlib\ArrayManager;
1313

1414
/**
15-
* Class for retrieving configuration from environment variables.
15+
* Class for retrieving configurations from environment variables.
1616
*/
1717
class EnvironmentConfigSource implements ConfigSourceInterface
1818
{

app/code/Magento/Config/Console/Command/ConfigShow/ValueProcessor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Magento\Framework\App\Area;
1515

1616
/**
17-
* Class processes value using backend model.
17+
* Class processes values using backend model.
18+
*
19+
* Backend model is gotten from field via its path (e.g. scope_id/group_id/field_id).
1820
*/
1921
class ValueProcessor
2022
{
@@ -57,11 +59,11 @@ public function __construct(
5759
/**
5860
* Processes value using backend model.
5961
*
60-
* @param string $scope The scope of configuration
62+
* @param string $scope The scope of configuration. E.g. 'default', 'website' or 'store'
6163
* @param string $scopeCode The scope code of configuration
6264
* @param string $value The value to process
63-
* @param string $path The configuration path for getting backend model
64-
* @return string
65+
* @param string $path The configuration path for getting backend model. E.g. scope_id/group_id/field_id
66+
* @return string processed value result
6567
*/
6668
public function process($scope, $scopeCode, $value, $path)
6769
{

app/code/Magento/Config/Console/Command/ConfigShowCommand.php

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
2020

2121
/**
22-
* Command provides possibility to show system configuration.
22+
* Command provides possibility to show saved system configuration.
2323
*/
2424
class ConfigShowCommand extends Command
2525
{
@@ -74,7 +74,7 @@ class ConfigShowCommand extends Command
7474
private $scopeCode;
7575

7676
/**
77-
* The path of configuration.
77+
* The configuration path.
7878
*
7979
* @var string
8080
*/
@@ -108,67 +108,86 @@ protected function configure()
108108
$this->addArgument(
109109
self::INPUT_ARGUMENT_PATH,
110110
InputArgument::OPTIONAL,
111-
'Configuration path for example group/section/field_name'
111+
'Configuration path, for example section_id/group_id/field_id'
112112
);
113113
$this->addOption(
114114
self::INPUT_OPTION_SCOPE,
115115
null,
116116
InputOption::VALUE_OPTIONAL,
117-
'Scope for configuration, if not set use \'default\'',
117+
'Scope for configuration, if not specified, then \'default\' scope will be used',
118118
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
119119
);
120120
$this->addOption(
121121
self::INPUT_OPTION_SCOPE_CODE,
122122
null,
123123
InputOption::VALUE_OPTIONAL,
124-
'Scope code for configuration, empty string by default',
124+
'Scope code (required only if scope is not `default`)',
125125
''
126126
);
127127
$this->setName('config:show')
128-
->setDescription('Shows configuration value for given path');
128+
->setDescription(
129+
'Shows configuration value for given path. If path is not specified, all saved values will be shown'
130+
);
129131
parent::configure();
130132
}
131133

132134
/**
133135
* Displays configuration value for given configuration path.
134136
*
135-
* Shows error message if configuration for given path doesn't exists
137+
* Shows error message if configuration for given path doesn't exist
136138
* or scope/scope-code doesn't pass validation.
137139
*
138140
* {@inheritdoc}
139141
*/
140142
protected function execute(InputInterface $input, OutputInterface $output)
141143
{
142-
$this->scope = $input->getOption(self::INPUT_OPTION_SCOPE);
143-
$this->scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
144-
$this->inputPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
145-
146144
try {
145+
$this->scope = $input->getOption(self::INPUT_OPTION_SCOPE);
146+
$this->scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
147+
$this->inputPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
148+
147149
$this->scopeValidator->isValid($this->scope, $this->scopeCode);
148150
$configPath = $this->pathResolver->resolve($this->inputPath, $this->scope, $this->scopeCode);
149151
$configValue = $this->configSource->get($configPath);
150-
} catch (LocalizedException $e) {
151-
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
152-
return Cli::RETURN_FAILURE;
153-
}
154152

155-
if (empty($configValue)) {
156-
$output->writeln(sprintf(
157-
'<error>%s</error>',
158-
__('Configuration for path: "%1" doesn\'t exist', $this->inputPath)->render()
159-
));
153+
if (empty($configValue)) {
154+
$output->writeln(sprintf(
155+
'<error>%s</error>',
156+
__('Configuration for path: "%1" doesn\'t exist', $this->inputPath)->render()
157+
));
158+
return Cli::RETURN_FAILURE;
159+
}
160+
161+
$this->outputResult($output, $configValue, $this->inputPath);
162+
return Cli::RETURN_SUCCESS;
163+
} catch (\Exception $e) {
164+
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
160165
return Cli::RETURN_FAILURE;
161166
}
162-
163-
$this->outputResult($output, $configValue, $this->inputPath);
164-
return Cli::RETURN_SUCCESS;
165167
}
166168

167169
/**
168170
* Outputs single configuration value or list of values if array given.
169171
*
170172
* @param OutputInterface $output an OutputInterface instance
171-
* @param mixed $configValue single value or array of values
173+
* @param mixed $configValue can be string when $configPath is a path for concreate field.
174+
* In other cases $configValue should be an array
175+
* ```php
176+
* [
177+
* 'section' =>
178+
* [
179+
* 'group1' =>
180+
* [
181+
* 'field1' => 'value1',
182+
* 'field2' => 'value2'
183+
* ],
184+
* 'group2' =>
185+
* [
186+
* 'field1' => 'value3'
187+
* ]
188+
* ]
189+
* ]
190+
* ```
172191
* @param string $configPath base configuration path
173192
* @return void
174193
*/

app/code/Magento/Config/Test/Unit/App/Config/Source/EnvironmentConfigSourceTest.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class EnvironmentConfigSourceTest extends \PHPUnit_Framework_TestCase
2929

3030
protected function setUp()
3131
{
32-
$_ENV['CONFIG__UNIT__TEST__VALUE'] = 'test_value';
33-
3432
$this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
3533
->disableOriginalConstructor()
3634
->getMock();
@@ -49,12 +47,18 @@ protected function setUp()
4947
$this->source = new EnvironmentConfigSource($this->arrayManagerMock, $placeholderFactoryMock);
5048
}
5149

52-
public function testGet()
50+
/**
51+
* @param string $path
52+
* @param array|string $expectedResult
53+
* @dataProvider getDataProvider
54+
*/
55+
public function testGet($path, $expectedResult)
5356
{
5457
$placeholder = 'CONFIG__UNIT__TEST__VALUE';
55-
$value = 'test_value';
56-
$path = 'unit/test/value';
57-
$expectedArray = ['unit' => ['test' => ['value' => $value]]];
58+
$configValue = 'test_value';
59+
$configPath = 'unit/test/value';
60+
$expectedArray = ['unit' => ['test' => ['value' => $configValue]]];
61+
$_ENV[$placeholder] = $configValue;
5862

5963
$this->placeholderMock->expects($this->any())
6064
->method('isApplicable')
@@ -64,12 +68,41 @@ public function testGet()
6468
$this->placeholderMock->expects($this->once())
6569
->method('restore')
6670
->with($placeholder)
67-
->willReturn($path);
71+
->willReturn($configPath);
6872
$this->arrayManagerMock->expects($this->once())
6973
->method('set')
70-
->with($path, [], $value)
74+
->with($configPath, [], $configValue)
7175
->willReturn($expectedArray);
7276

77+
$this->assertEquals($expectedResult, $this->source->get($path));
78+
}
79+
80+
/**
81+
* @return array
82+
*/
83+
public function getDataProvider()
84+
{
85+
return [
86+
['', ['unit' => ['test' => ['value' => 'test_value']]]],
87+
['unit', ['test' => ['value' => 'test_value']]],
88+
['unit/test', ['value' => 'test_value']],
89+
['unit/test/value', 'test_value'],
90+
['wrong/path', []],
91+
];
92+
}
93+
94+
public function testGetWithoutEnvCongigurationVariables()
95+
{
96+
$expectedArray = [];
97+
98+
$this->placeholderMock->expects($this->any())
99+
->method('isApplicable')
100+
->willReturn(false);
101+
$this->placeholderMock->expects($this->never())
102+
->method('restore');
103+
$this->arrayManagerMock->expects($this->never())
104+
->method('set');
105+
73106
$this->assertSame($expectedArray, $this->source->get());
74107
}
75108

0 commit comments

Comments
 (0)