Skip to content

Commit bd2d552

Browse files
author
Bohdan Korablov
committed
MAGETWO-65208: Store checksum for every section of configuration file & change behavior on read-only FS & add sorting of importers
1 parent cfabd5e commit bd2d552

File tree

10 files changed

+42
-88
lines changed

10 files changed

+42
-88
lines changed

app/code/Magento/Deploy/Console/Command/App/ConfigImport/Importer.php

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

88
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
99
use Magento\Framework\App\DeploymentConfig;
10-
use Magento\Deploy\Model\DeploymentConfig\ImportFailedException;
10+
use Magento\Framework\Exception\RuntimeException;
1111
use Psr\Log\LoggerInterface as Logger;
1212
use Magento\Deploy\Model\DeploymentConfig\Validator;
1313
use Magento\Deploy\Model\DeploymentConfig\ImporterPool;
@@ -91,7 +91,7 @@ public function __construct(
9191
*
9292
* @param OutputInterface $output the CLI output
9393
* @return void
94-
* @throws ImportFailedException is thrown when import has failed
94+
* @throws RuntimeException is thrown when import has failed
9595
*/
9696
public function import(OutputInterface $output)
9797
{
@@ -106,20 +106,20 @@ public function import(OutputInterface $output)
106106

107107
/**
108108
* @var string $section
109-
* @var string $importer
109+
* @var string $importerClassName
110110
*/
111111
foreach ($importers as $section => $importerClassName) {
112112
if (!$this->configValidator->isValid($section)) {
113113
/** @var ImporterInterface $importer */
114114
$importer = $this->importerFactory->create($importerClassName);
115-
$messages = $importer->import($this->deploymentConfig->getConfigData($section));
115+
$messages = $importer->import((array) $this->deploymentConfig->getConfigData($section));
116116
$output->writeln($messages);
117117
$this->configHash->regenerate($section);
118118
}
119119
}
120120
} catch (\Exception $exception) {
121121
$this->logger->error($exception);
122-
throw new ImportFailedException(__('Import is failed. Please, see the log report.'), $exception);
122+
throw new RuntimeException(__('Import is failed. Please see the log report.'), $exception);
123123
}
124124
}
125125
}

app/code/Magento/Deploy/Console/Command/App/ConfigImportCommand.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
*/
66
namespace Magento\Deploy\Console\Command\App;
77

8-
use Magento\Deploy\Model\DeploymentConfig\ImportFailedException;
8+
use Magento\Framework\Exception\RuntimeException;
99
use Symfony\Component\Console\Command\Command;
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Magento\Framework\Console\Cli;
1313
use Magento\Deploy\Console\Command\App\ConfigImport\Importer;
14-
use Magento\Framework\App\DeploymentConfig\Writer;
1514

1615
/**
1716
* Runs the process of importing configuration data from shared source to appropriate application sources.
@@ -34,21 +33,12 @@ class ConfigImportCommand extends Command
3433
*/
3534
private $importer;
3635

37-
/**
38-
* Configuration file writer.
39-
*
40-
* @var Writer
41-
*/
42-
private $writer;
43-
4436
/**
4537
* @param Importer $importer the configuration importer
46-
* @param Writer $writer the configuration file writer
4738
*/
48-
public function __construct(Importer $importer, Writer $writer)
39+
public function __construct(Importer $importer)
4940
{
5041
$this->importer = $importer;
51-
$this->writer = $writer;
5242

5343
parent::__construct();
5444
}
@@ -70,14 +60,9 @@ protected function configure()
7060
*/
7161
protected function execute(InputInterface $input, OutputInterface $output)
7262
{
73-
if (!$this->writer->checkIfWritable()) {
74-
$output->writeln('<error>Deployment configuration file is not writable.</error>');
75-
return Cli::RETURN_FAILURE;
76-
}
77-
7863
try {
7964
$this->importer->import($output);
80-
} catch (ImportFailedException $e) {
65+
} catch (RuntimeException $e) {
8166
$output->writeln('<error>' . $e->getMessage() . '</error>');
8267

8368
return Cli::RETURN_FAILURE;

app/code/Magento/Deploy/Model/DeploymentConfig/DataCollector.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* <type name="Magento\Deploy\Model\DeploymentConfig\ImporterPool">
1616
* <arguments>
1717
* <argument name="importers" xsi:type="array">
18-
* <item name="scopes" xsi:type="string">Magento\Store\Model\StoreImporter</item>
18+
* <item name="scopes" xsi:type="string">Magento\SomeModule\Model\SomeImporter</item>
1919
* </argument>
2020
* </arguments>
2121
* </type>
@@ -30,7 +30,7 @@
3030
* ]
3131
* ```
3232
*
33-
* In here we define section "scopes" and its importer Magento\Store\Model\StoreImporter.
33+
* In here we define section "scopes" and its importer Magento\SomeModule\Model\SomeImporter.
3434
* The data of this section will be collected then will be used in importing process from the shared configuration
3535
* files to appropriate application sources.
3636
*
@@ -75,6 +75,13 @@ public function __construct(ImporterPool $configImporterPool, DeploymentConfig $
7575
* ...
7676
* ]
7777
* ```
78+
*
79+
* This method retrieves the same structure for the specific section with only its data.
80+
* ```php
81+
* [
82+
* 'scopes' => [...]
83+
* ]
84+
*
7885
* In this example key of the array is the section name, value of the array is configuration data of the section.
7986
*
8087
* @param string $sectionName the section name for retrieving its configuration data
@@ -91,10 +98,7 @@ public function getConfig($sectionName = null)
9198
}
9299

93100
foreach ($sections as $section) {
94-
$data = $this->deploymentConfig->getConfigData($section);
95-
if ($data) {
96-
$result[$section] = $data;
97-
}
101+
$result[$section] = $this->deploymentConfig->getConfigData($section);
98102
}
99103

100104
return $result;

app/code/Magento/Deploy/Model/DeploymentConfig/ImportFailedException.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/code/Magento/Deploy/Model/DeploymentConfig/ImporterPool.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ public function getImporters()
130130
{
131131
if (!$this->sortedImporters) {
132132
$sortedImporters = [];
133-
$importers = $this->sort($this->importers);
134133

135-
foreach ($importers as $section => $importer) {
134+
foreach ($this->sort($this->importers) as $section => $importer) {
136135
if (empty($importer['class'])) {
137136
throw new ConfigurationMismatchException(__('Parameter "class" must be present.'));
138137
}

app/code/Magento/Deploy/Model/DeploymentConfig/Validator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(
5959
* If config data is empty always returns true.
6060
* In the other cases returns true.
6161
*
62-
* @param string $sectionName is name section for check data of the specific section
62+
* @param string $sectionName is section name for check data of the specific section
6363
* @return bool
6464
*/
6565
public function isValid($sectionName = null)
@@ -68,8 +68,9 @@ public function isValid($sectionName = null)
6868
$hashes = $this->configHash->get();
6969

7070
foreach ($configs as $section => $config) {
71-
$hash = isset($hashes[$section]) ? $hashes[$section] : null;
72-
if ($config && $this->hashGenerator->generate($config) !== $hash) {
71+
$savedHash = isset($hashes[$section]) ? $hashes[$section] : null;
72+
$generatedHash = empty($config) && !$savedHash ? null : $this->hashGenerator->generate($config);
73+
if ($generatedHash !== $savedHash) {
7374
return false;
7475
}
7576
}

app/code/Magento/Deploy/Test/Unit/Console/Command/App/ConfigImport/ImporterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function testImport()
143143
/**
144144
* @return void
145145
* @expectedException \Magento\Framework\Exception\LocalizedException
146-
* @expectedExceptionMessage Import is failed
146+
* @expectedExceptionMessage Import is failed. Please see the log report.
147147
*/
148148
public function testImportWithException()
149149
{

app/code/Magento/Deploy/Test/Unit/Console/Command/App/ConfigImportCommandTest.php

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
use Magento\Deploy\Console\Command\App\ConfigImportCommand;
99
use Magento\Deploy\Console\Command\App\ConfigImport\Importer;
1010
use Magento\Framework\Console\Cli;
11-
use Magento\Deploy\Model\DeploymentConfig\ImportFailedException;
11+
use Magento\Framework\Exception\RuntimeException;
1212
use Symfony\Component\Console\Tester\CommandTester;
13-
use Magento\Framework\App\DeploymentConfig\Writer;
1413

1514
class ConfigImportCommandTest extends \PHPUnit_Framework_TestCase
1615
{
@@ -19,11 +18,6 @@ class ConfigImportCommandTest extends \PHPUnit_Framework_TestCase
1918
*/
2019
private $importerMock;
2120

22-
/**
23-
* @var Writer|\PHPUnit_Framework_MockObject_MockObject
24-
*/
25-
private $writerMock;
26-
2721
/**
2822
* @var CommandTester
2923
*/
@@ -37,14 +31,8 @@ protected function setUp()
3731
$this->importerMock = $this->getMockBuilder(Importer::class)
3832
->disableOriginalConstructor()
3933
->getMock();
40-
$this->writerMock = $this->getMockBuilder(Writer::class)
41-
->disableOriginalConstructor()
42-
->getMock();
4334

44-
$configImportCommand = new ConfigImportCommand(
45-
$this->importerMock,
46-
$this->writerMock
47-
);
35+
$configImportCommand = new ConfigImportCommand($this->importerMock);
4836

4937
$this->commandTester = new CommandTester($configImportCommand);
5038
}
@@ -54,41 +42,20 @@ protected function setUp()
5442
*/
5543
public function testExecute()
5644
{
57-
$this->writerMock->expects($this->once())
58-
->method('checkIfWritable')
59-
->willReturn(true);
6045
$this->importerMock->expects($this->once())
6146
->method('import');
6247

6348
$this->assertSame(Cli::RETURN_SUCCESS, $this->commandTester->execute([]));
6449
}
6550

66-
/**
67-
* @return void
68-
*/
69-
public function testExecuteWithoutWritePermissions()
70-
{
71-
$this->writerMock->expects($this->once())
72-
->method('checkIfWritable')
73-
->willReturn(false);
74-
$this->importerMock->expects($this->never())
75-
->method('import');
76-
77-
$this->assertSame(Cli::RETURN_FAILURE, $this->commandTester->execute([]));
78-
$this->assertContains('Deployment configuration file is not writable.', $this->commandTester->getDisplay());
79-
}
80-
8151
/**
8252
* @return void
8353
*/
8454
public function testExecuteWithException()
8555
{
86-
$this->writerMock->expects($this->once())
87-
->method('checkIfWritable')
88-
->willReturn(true);
8956
$this->importerMock->expects($this->once())
9057
->method('import')
91-
->willThrowException(new ImportFailedException(__('Some error')));
58+
->willThrowException(new RuntimeException(__('Some error')));
9259

9360
$this->assertSame(Cli::RETURN_FAILURE, $this->commandTester->execute([]));
9461
$this->assertContains('Some error', $this->commandTester->getDisplay());

app/code/Magento/Deploy/Test/Unit/Model/DeploymentConfig/DataCollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testGetConfig()
5454
->method('getConfigData')
5555
->willReturnMap([['first', 'some data']]);
5656

57-
$this->assertSame(['first' => 'some data'], $this->dataCollector->getConfig());
57+
$this->assertSame(['first' => 'some data', 'second' => null], $this->dataCollector->getConfig());
5858
}
5959

6060
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Exception;
7+
8+
/**
9+
* Exception thrown if an error which can only be found on runtime occurs.
10+
*/
11+
class RuntimeException extends LocalizedException
12+
{
13+
14+
}

0 commit comments

Comments
 (0)