Skip to content

Commit 25b13cd

Browse files
MAGETWO-61778: Implement config:set command
1 parent 0c89970 commit 25b13cd

File tree

6 files changed

+65
-33
lines changed

6 files changed

+65
-33
lines changed

app/code/Magento/Config/Console/Command/ConfigSet/DefaultProcessor.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
*/
66
namespace Magento\Config\Console\Command\ConfigSet;
77

8-
use Symfony\Component\Console\Input\InputInterface;
9-
use Magento\Framework\Exception\CouldNotSaveException;
10-
use Magento\Framework\Exception\StateException;
11-
use Magento\Framework\App\DeploymentConfig;
12-
use Magento\Framework\App\Config\ConfigPathResolver;
8+
use Magento\Config\App\Config\Type\System;
139
use Magento\Config\Console\Command\ConfigSetCommand;
1410
use Magento\Config\Model\ConfigFactory;
15-
use Magento\Config\App\Config\Type\System;
11+
use Magento\Framework\App\Config\ConfigPathResolver;
12+
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\Exception\CouldNotSaveException;
14+
use Magento\Framework\Exception\StateException;
1615
use Magento\Store\Model\ScopeInterface;
16+
use Symfony\Component\Console\Input\InputInterface;
1717

1818
/**
1919
* Processes default flow of config:set command.
@@ -70,7 +70,8 @@ public function process(InputInterface $input)
7070
if (!$this->deploymentConfig->isAvailable()) {
7171
throw new StateException(
7272
__(
73-
'Magento is not installed yet and this value can be only saved with --%1 option.',
73+
'We can\'t save this option because Magento is not installed. '
74+
. 'To lock this value, enter the command again using the --%1 option.',
7475
ConfigSetCommand::OPTION_LOCK
7576
)
7677
);
@@ -79,7 +80,7 @@ public function process(InputInterface $input)
7980
if ($this->isLocked($path, $scope, $scopeCode)) {
8081
throw new CouldNotSaveException(
8182
__(
82-
'Effective value already locked. It can be changed with --%1 option',
83+
'The value you set has already been locked. To change the value, use the --%1 option.',
8384
ConfigSetCommand::OPTION_LOCK
8485
)
8586
);

app/code/Magento/Config/Console/Command/ConfigSet/LockProcessor.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
*/
66
namespace Magento\Config\Console\Command\ConfigSet;
77

8-
use Symfony\Component\Console\Input\InputInterface;
98
use Magento\Config\App\Config\Type\System;
10-
use Magento\Config\Model\Config\Structure;
119
use Magento\Config\Console\Command\ConfigSetCommand;
10+
use Magento\Config\Model\Config\Structure;
1211
use Magento\Config\Model\Config\Structure\Element\Field;
12+
use Magento\Framework\App\Config\ConfigPathResolver;
13+
use Magento\Framework\App\Config\Value;
14+
use Magento\Framework\App\Config\ValueFactory;
1315
use Magento\Framework\App\DeploymentConfig\Writer;
1416
use Magento\Framework\Config\File\ConfigFilePool;
1517
use Magento\Framework\Exception\CouldNotSaveException;
1618
use Magento\Framework\Exception\FileSystemException;
1719
use Magento\Framework\Stdlib\ArrayManager;
18-
use Magento\Framework\App\Config\ConfigPathResolver;
19-
use Magento\Framework\App\Config\Value;
20-
use Magento\Framework\App\Config\ValueFactory;
20+
use Symfony\Component\Console\Input\InputInterface;
2121

2222
/**
2323
* Processes file lock flow of config:set command.
@@ -97,6 +97,7 @@ public function process(InputInterface $input)
9797
$backendModel->setPath($path);
9898
$backendModel->setScope($scope);
9999
$backendModel->setScopeId($scopeCode);
100+
$backendModel->setValue($value);
100101

101102
try {
102103
/**
@@ -107,8 +108,8 @@ public function process(InputInterface $input)
107108
$backendModel->beforeSave();
108109

109110
$this->deploymentConfigWriter->saveConfig(
110-
[ConfigFilePool::APP_CONFIG => $this->arrayManager->set($configPath, [], $value)],
111-
true
111+
[ConfigFilePool::APP_CONFIG => $this->arrayManager->set($configPath, [], $backendModel->getValue())],
112+
false
112113
);
113114

114115
$backendModel->afterSave();

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
namespace Magento\Config\Console\Command;
77

88
use Magento\Config\Console\Command\ConfigSet\ConfigSetProcessorFactory;
9+
use Magento\Framework\App\Area;
910
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\Scope\ValidatorInterface;
12+
use Magento\Framework\Config\ScopeInterface;
1013
use Magento\Framework\Console\Cli;
1114
use Magento\Framework\Exception\LocalizedException;
1215
use Symfony\Component\Console\Command\Command;
1316
use Symfony\Component\Console\Input\InputArgument;
1417
use Symfony\Component\Console\Input\InputInterface;
1518
use Symfony\Component\Console\Input\InputOption;
1619
use Symfony\Component\Console\Output\OutputInterface;
17-
use Magento\Framework\App\Scope\ValidatorInterface;
1820

1921
/**
2022
* Command provides possibility to change system configuration.
@@ -41,16 +43,24 @@ class ConfigSetCommand extends Command
4143
*/
4244
private $validator;
4345

46+
/**
47+
* @var ScopeInterface
48+
*/
49+
private $scope;
50+
4451
/**
4552
* @param ConfigSetProcessorFactory $configSetProcessorFactory
4653
* @param ValidatorInterface $validator
54+
* @param ScopeInterface $scope
4755
*/
4856
public function __construct(
4957
ConfigSetProcessorFactory $configSetProcessorFactory,
50-
ValidatorInterface $validator
58+
ValidatorInterface $validator,
59+
ScopeInterface $scope
5160
) {
5261
$this->configSetProcessorFactory = $configSetProcessorFactory;
5362
$this->validator = $validator;
63+
$this->scope = $scope;
5464

5565
parent::__construct();
5666
}
@@ -68,25 +78,25 @@ protected function configure()
6878
InputArgument::REQUIRED,
6979
'Configuration path in format group/section/field_name'
7080
),
71-
new InputArgument(static::ARG_VALUE, InputArgument::REQUIRED, 'Value of configuration'),
81+
new InputArgument(static::ARG_VALUE, InputArgument::REQUIRED, 'Configuration value'),
7282
new InputOption(
7383
static::OPTION_SCOPE,
7484
null,
7585
InputArgument::OPTIONAL,
76-
'Scope of configuration (default, website, store)',
86+
'Configuration scope (default, website, or store)',
7787
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
7888
),
7989
new InputOption(
8090
static::OPTION_SCOPE_CODE,
8191
null,
8292
InputArgument::OPTIONAL,
83-
'Scope code of configuration (website code or store view code)'
93+
'Scope code'
8494
),
8595
new InputOption(
8696
static::OPTION_LOCK,
8797
'l',
8898
InputOption::VALUE_NONE,
89-
'Lock value to prevent it modification via admin configuration'
99+
'Lock value which prevents modification in the Admin'
90100
),
91101
]);
92102

@@ -106,14 +116,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
106116
$input->getOption(static::OPTION_SCOPE_CODE)
107117
);
108118

119+
// Emulating adminhtml scope to be able to read configs.
120+
$this->scope->setCurrentScope(Area::AREA_ADMINHTML);
121+
109122
$processor = $input->getOption(static::OPTION_LOCK)
110123
? $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_LOCK)
111124
: $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_DEFAULT);
112125
$message = $input->getOption(static::OPTION_LOCK)
113126
? 'Value was locked.'
114127
: 'Value was saved.';
115128

116-
// The processing flow depends in --lock option.
129+
// The processing flow depends on --lock option.
117130
$processor->process($input);
118131

119132
$output->writeln('<info>' . $message . '</info>');

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSet/DefaultProcessorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
*/
66
namespace Magento\Config\Test\Unit\Console\Command\ConfigSet;
77

8-
use Symfony\Component\Console\Input\InputInterface;
98
use Magento\Config\Console\Command\ConfigSet\DefaultProcessor;
109
use Magento\Config\Console\Command\ConfigSetCommand;
1110
use Magento\Config\Model\Config;
1211
use Magento\Config\Model\ConfigFactory;
13-
use Magento\Framework\App\Config\ScopeConfigInterface;
1412
use Magento\Framework\App\Config\ConfigPathResolver;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
1514
use Magento\Framework\App\DeploymentConfig;
1615
use PHPUnit_Framework_MockObject_MockObject as Mock;
16+
use Symfony\Component\Console\Input\InputInterface;
1717

1818
/**
1919
* Test for DefaultProcessor.
@@ -121,7 +121,7 @@ public function testProcess()
121121

122122
/**
123123
* @expectedException \Magento\Framework\Exception\LocalizedException
124-
* @expectedExceptionMessage Magento is not installed yet and this value can be only saved with --lock option.
124+
* @expectedExceptionMessage We can't save this option because Magento is not installed.
125125
*/
126126
public function testProcessNotInstalled()
127127
{
@@ -149,7 +149,7 @@ public function testProcessNotInstalled()
149149

150150
/**
151151
* @expectedException \Magento\Framework\Exception\LocalizedException
152-
* @expectedExceptionMessage Effective value already locked.
152+
* @expectedExceptionMessage The value you set has already been locked. To change the value, use the --lock option.
153153
*/
154154
public function testProcessLockedValue()
155155
{

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSet/LockProcessorTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88
use Magento\Config\Console\Command\ConfigSet\LockProcessor;
99
use Magento\Config\Console\Command\ConfigSetCommand;
10-
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Config\Model\Config\Structure;
11+
use Magento\Config\Model\Config\Structure\Element\Field;
1112
use Magento\Framework\App\Config\ConfigPathResolver;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
1214
use Magento\Framework\App\Config\Value;
15+
use Magento\Framework\App\Config\ValueFactory;
1316
use Magento\Framework\App\DeploymentConfig;
1417
use Magento\Framework\Config\File\ConfigFilePool;
1518
use Magento\Framework\Exception\FileSystemException;
1619
use Magento\Framework\Stdlib\ArrayManager;
17-
use Symfony\Component\Console\Input\InputInterface;
18-
use Magento\Config\Model\Config\Structure;
19-
use Magento\Framework\App\Config\ValueFactory;
20-
use Magento\Config\Model\Config\Structure\Element\Field;
2120
use PHPUnit_Framework_MockObject_MockObject as Mock;
21+
use Symfony\Component\Console\Input\InputInterface;
2222

2323
/**
2424
* Test for LockProcessor.
@@ -99,6 +99,7 @@ protected function setUp()
9999
->disableOriginalConstructor()
100100
->getMock();
101101
$this->valueMock = $this->getMockBuilder(Value::class)
102+
->setMethods(['validateBeforeSave', 'beforeSave', 'setValue', 'getValue', 'afterSave'])
102103
->disableOriginalConstructor()
103104
->getMock();
104105

@@ -155,6 +156,9 @@ public function testProcess()
155156
]
156157
]
157158
]);
159+
$this->valueMock->expects($this->once())
160+
->method('getValue')
161+
->willReturn($value);
158162
$this->deploymentConfigWriterMock->expects($this->once())
159163
->method('saveConfig')
160164
->with(
@@ -171,7 +175,7 @@ public function testProcess()
171175
]
172176
]
173177
],
174-
true
178+
false
175179
);
176180

177181
$this->model->process($this->inputMock);
@@ -204,6 +208,9 @@ public function testProcessNotReadableFs()
204208
$this->fieldMock->expects($this->once())
205209
->method('getBackendModel')
206210
->willReturn($this->valueMock);
211+
$this->valueMock->expects($this->once())
212+
->method('getValue')
213+
->willReturn($value);
207214
$this->configPathResolver->expects($this->once())
208215
->method('resolve')
209216
->willReturn('system/default/test/test/test');

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSetCommandTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Config\Console\Command\ConfigSet\ConfigSetProcessorInterface;
1010
use Magento\Config\Console\Command\ConfigSetCommand;
1111
use Magento\Framework\App\Scope\ValidatorInterface;
12+
use Magento\Framework\Config\ScopeInterface;
1213
use Magento\Framework\Console\Cli;
1314
use Magento\Framework\Exception\LocalizedException;
1415
use PHPUnit_Framework_MockObject_MockObject as Mock;
@@ -18,6 +19,7 @@
1819
* Test for ConfigSetCommand.
1920
*
2021
* @see ConfigSetCommand
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2123
*/
2224
class ConfigSetCommandTest extends \PHPUnit_Framework_TestCase
2325
{
@@ -41,6 +43,11 @@ class ConfigSetCommandTest extends \PHPUnit_Framework_TestCase
4143
*/
4244
private $processorMock;
4345

46+
/**
47+
* @var ScopeInterface|Mock
48+
*/
49+
private $scopeMock;
50+
4451
/**
4552
* @inheritdoc
4653
*/
@@ -54,10 +61,13 @@ protected function setUp()
5461
->getMockForAbstractClass();
5562
$this->processorMock = $this->getMockBuilder(ConfigSetProcessorInterface::class)
5663
->getMockForAbstractClass();
64+
$this->scopeMock = $this->getMockBuilder(ScopeInterface::class)
65+
->getMockForAbstractClass();
5766

5867
$this->command = new ConfigSetCommand(
5968
$this->configSetProcessorFactoryMock,
60-
$this->validatorMock
69+
$this->validatorMock,
70+
$this->scopeMock
6171
);
6272
}
6373

0 commit comments

Comments
 (0)