Skip to content

Commit 6719966

Browse files
author
Ivan Gavryshko
committed
MAGETWO-35137: Add deployment configuration set command
- added $override parameter to Writer::saveConfig, false by default - changed messages (approved) - fixed naming in ConfigOptionsListCollector according to CR - fixed Cyclomatic Complexity in ConfigSetCommand::execute
1 parent 0261353 commit 6719966

File tree

8 files changed

+84
-27
lines changed

8 files changed

+84
-27
lines changed

dev/tests/integration/testsuite/Magento/Setup/Model/ConfigOptionsListCollectorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public function setUp()
2121
->willReturn(\Magento\TestFramework\Helper\Bootstrap::getObjectManager());
2222
}
2323

24-
public function testCollectOptionLists()
24+
public function testCollectOptionsLists()
2525
{
2626
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
2727
$fullModuleListMock = $this->getMock('Magento\Framework\Module\FullModuleList', [], [], '', false);
28-
$fullModuleListMock->expects($this->never())->method('getNames');
28+
$fullModuleListMock->expects($this->once())->method('getNames');
2929
/** @var \Magento\Setup\Model\ConfigOptionsListCollector $object */
3030
$object = $objectManager->create(
3131
'Magento\Setup\Model\ConfigOptionsListCollector',
@@ -34,7 +34,7 @@ public function testCollectOptionLists()
3434
'fullModuleList' => $fullModuleListMock,
3535
]
3636
);
37-
$result = $object->collectOptionLists();
37+
$result = $object->collectOptionsLists();
3838

3939
$setupOptions = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
4040
->get('Magento\Framework\Config\ConfigOptionsList');

lib/internal/Magento/Framework/App/DeploymentConfig/Writer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ public function checkIfWritable()
8888
* Saves config
8989
*
9090
* @param array $data
91+
* @param bool $override
9192
* @return void
9293
*/
93-
public function saveConfig(array $data)
94+
public function saveConfig(array $data, $override = false)
9495
{
9596
$paths = $this->configFilePool->getPaths();
9697

@@ -99,7 +100,11 @@ public function saveConfig(array $data)
99100

100101
if ($this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->isExist($paths[$fileKey])) {
101102
$currentData = $this->reader->load($paths[$fileKey]);
102-
$config = array_replace_recursive($currentData, $config);
103+
if ($override) {
104+
$config = array_merge($currentData, $config);
105+
} else {
106+
$config = array_replace_recursive($currentData, $config);
107+
}
103108
}
104109

105110
$contents = $this->formatter->format($config);

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/WriterTest.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public function testSaveConfig()
7979
'foo' => 'bar',
8080
'key' => 'value',
8181
'baz' => [
82-
'test' => 'value'
82+
'test' => 'value',
83+
'test1' => 'value1'
8384
]
8485
],
8586
];
@@ -97,7 +98,8 @@ public function testSaveConfig()
9798
'foo' => 'bar',
9899
'key' => 'value',
99100
'baz' => [
100-
'test' => 'value2'
101+
'test' => 'value2',
102+
'test1' => 'value1'
101103
]
102104
],
103105
];
@@ -115,4 +117,54 @@ public function testSaveConfig()
115117

116118
$this->object->saveConfig($testSetUpdate);
117119
}
120+
121+
public function testSaveConfigOverride()
122+
{
123+
$configFiles = [
124+
ConfigFilePool::APP_CONFIG => 'test_conf.php',
125+
'test_key' => 'test2_conf.php'
126+
];
127+
128+
$testSetExisting = [
129+
ConfigFilePool::APP_CONFIG => [
130+
'foo' => 'bar',
131+
'key' => 'value',
132+
'baz' => [
133+
'test' => 'value',
134+
'test1' => 'value1'
135+
]
136+
],
137+
];
138+
139+
$testSetUpdate = [
140+
ConfigFilePool::APP_CONFIG => [
141+
'baz' => [
142+
'test' => 'value2'
143+
]
144+
],
145+
];
146+
147+
$testSetExpected = [
148+
ConfigFilePool::APP_CONFIG => [
149+
'foo' => 'bar',
150+
'key' => 'value',
151+
'baz' => [
152+
'test' => 'value2',
153+
]
154+
],
155+
];
156+
157+
$this->deploymentConfig->expects($this->once())->method('resetData');
158+
$this->configFilePool->expects($this->once())->method('getPaths')->willReturn($configFiles);
159+
$this->dirWrite->expects($this->any())->method('isExist')->willReturn(true);
160+
$this->reader->expects($this->once())->method('load')->willReturn($testSetExisting[ConfigFilePool::APP_CONFIG]);
161+
$this->formatter
162+
->expects($this->once())
163+
->method('format')
164+
->with($testSetExpected[ConfigFilePool::APP_CONFIG])
165+
->willReturn([]);
166+
$this->dirWrite->expects($this->once())->method('writeFile')->with('test_conf.php', []);
167+
168+
$this->object->saveConfig($testSetUpdate, true);
169+
}
118170
}

setup/src/Magento/Setup/Console/Command/ConfigSetCommand.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
7373
{
7474
$inputOptions = $input->getOptions();
7575
$optionCollection = $this->configModel->getAvailableOptions();
76-
$optionsToChange = [];
76+
$commandOptions = [];
7777
$optionsWithDefaultValues = [];
7878

7979
foreach ($optionCollection as $option) {
80+
$commandOptions[$option->getName()] = false;
81+
8082
$currentValue = $this->deploymentConfig->get($option->getConfigPath());
8183
if (($currentValue !== null) && ($inputOptions[$option->getName()] !== null)) {
8284
$dialog = $this->getHelperSet()->get('dialog');
@@ -85,14 +87,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
8587
'<question>Overwrite the existing configuration for ' . $option->getName() . '?[Y|n]</question>'
8688
)) {
8789
$inputOptions[$option->getName()] = null;
88-
} else {
89-
$optionsToChange[$option->getName()] = $inputOptions[$option->getName()];
90-
}
91-
} else {
92-
if ($inputOptions[$option->getName()] !== null) {
93-
$optionsToChange[$option->getName()] = $inputOptions[$option->getName()];
9490
}
9591
}
92+
9693
if ($option->getDefault() === $inputOptions[$option->getName()]
9794
&& $inputOptions[$option->getName()] !== null
9895
) {
@@ -107,17 +104,20 @@ function ($value) {
107104
}
108105
);
109106

107+
$optionsToChange = array_diff($inputOptions, $commandOptions);
108+
110109
$this->configModel->process($inputOptions);
110+
111111
if (count($optionsWithDefaultValues) > 0) {
112112
$defaultValuesMessage = implode(', ', $optionsWithDefaultValues);
113113
$output->writeln(
114-
'<info>You saved default value(s) for the next option(s): ' . $defaultValuesMessage . '.</info>'
114+
'<info>We saved default values for these options: ' . $defaultValuesMessage . '.</info>'
115115
);
116116
} else {
117117
if (count($optionsToChange) > 0) {
118-
$output->writeln('<info>You saved the deployment config.</info>');
118+
$output->writeln('<info>You saved the new configuration.</info>');
119119
} else {
120-
$output->writeln('<info>You did not save the deployment config.</info>');
120+
$output->writeln('<info>You made no changes to the configuration.</info>');
121121
}
122122
}
123123
}

setup/src/Magento/Setup/Model/ConfigModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getAvailableOptions()
6464
{
6565
/** @var AbstractConfigOption[] $optionCollection */
6666
$optionCollection = [];
67-
$optionLists = $this->collector->collectOptionLists();
67+
$optionLists = $this->collector->collectOptionsLists();
6868

6969
foreach ($optionLists as $optionList) {
7070
$optionCollection = array_merge($optionCollection, $optionList->getOptions());
@@ -92,7 +92,7 @@ public function process($inputOptions)
9292
$this->checkInstallationFilePermissions();
9393

9494
$fileConfigStorage = [];
95-
$options = $this->collector->collectOptionLists();
95+
$options = $this->collector->collectOptionsLists();
9696

9797
foreach ($options as $moduleName => $option) {
9898

@@ -145,7 +145,7 @@ public function validate(array $inputOptions)
145145
}
146146

147147
// validate ConfigOptionsList
148-
$options = $this->collector->collectOptionLists();
148+
$options = $this->collector->collectOptionsLists();
149149

150150
foreach ($options as $option) {
151151
$errors = array_merge($errors, $option->validate($inputOptions));

setup/src/Magento/Setup/Model/ConfigOptionsListCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct(
6969
*
7070
* @return \Magento\Framework\Setup\ConfigOptionsListInterface[]
7171
*/
72-
public function collectOptionLists()
72+
public function collectOptionsLists()
7373
{
7474
$optionsList = [];
7575

setup/src/Magento/Setup/Test/Unit/Console/Command/ConfigSetCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testExecuteNoInteractive()
5959
$commandTester = new CommandTester($this->command);
6060
$commandTester->execute(['--db_host' => 'host']);
6161
$this->assertSame(
62-
'You saved the deployment config.' . PHP_EOL,
62+
'You saved the new configuration.' . PHP_EOL,
6363
$commandTester->getDisplay()
6464
);
6565
}
@@ -116,9 +116,9 @@ private function checkInteraction($interactionType)
116116
$commandTester = new CommandTester($this->command);
117117
$commandTester->execute(['--db_host' => 'host']);
118118
if ($interactionType) {
119-
$message = 'You saved the deployment config.' . PHP_EOL;
119+
$message = 'You saved the new configuration.' . PHP_EOL;
120120
} else {
121-
$message = 'You did not save the deployment config.'.PHP_EOL;
121+
$message = 'You made no changes to the configuration.'.PHP_EOL;
122122
}
123123
$this->assertSame(
124124
$message,

setup/src/Magento/Setup/Test/Unit/Model/ConfigModelTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testValidate()
8080

8181
$this->collector
8282
->expects($this->exactly(2))
83-
->method('collectOptionLists')
83+
->method('collectOptionsLists')
8484
->will($this->returnValue([$configOption]));
8585

8686
$this->configModel->validate(['Fake' => null]);
@@ -136,7 +136,7 @@ public function testProcess()
136136
'Fake_Module' => $configOption
137137
];
138138
$this->collector->expects($this->once())
139-
->method('collectOptionLists')
139+
->method('collectOptionsLists')
140140
->will($this->returnValue($configOptionsList));
141141

142142
$this->writer->expects($this->once())->method('saveConfig')->with($testSetExpected);
@@ -159,7 +159,7 @@ public function testProcessException()
159159
'Fake_Module' => $configOption
160160
];
161161

162-
$this->collector->expects($this->once())->method('collectOptionLists')->will($this->returnValue($wrongData));
162+
$this->collector->expects($this->once())->method('collectOptionsLists')->will($this->returnValue($wrongData));
163163

164164
$this->configModel->process([]);
165165
}

0 commit comments

Comments
 (0)