Skip to content

Commit 420cfb6

Browse files
author
Bohdan Korablov
committed
MAGETWO-64602: Sync config file with DB: Validation
1 parent 9f46b4e commit 420cfb6

File tree

7 files changed

+344
-19
lines changed

7 files changed

+344
-19
lines changed

app/code/Magento/Deploy/Model/DeploymentConfig/Importer.php renamed to app/code/Magento/Deploy/Console/Command/App/ConfigImport/Importer.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Deploy\Model\DeploymentConfig;
6+
namespace Magento\Deploy\Console\Command\App\ConfigImport;
77

88
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
99
use Magento\Framework\App\DeploymentConfig;
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Psr\Log\LoggerInterface as Logger;
12+
use Magento\Deploy\Model\DeploymentConfig\Validator;
13+
use Magento\Deploy\Model\DeploymentConfig\ImporterPool;
14+
use Magento\Deploy\Model\DeploymentConfig\Hash;
15+
use Symfony\Component\Console\Output\OutputInterface;
1216

1317
/**
1418
* Runs importing of config data from deployment configuration files.
@@ -74,28 +78,27 @@ public function __construct(
7478
/**
7579
* Runs importing of config data from deployment configuration files.
7680
*
77-
* @return array
81+
* @param OutputInterface $output the CLI output
82+
* @return void
7883
* @throws LocalizedException
7984
*/
80-
public function import()
85+
public function import(OutputInterface $output)
8186
{
82-
$messages = ['Start import:'];
87+
$output->writeln('<info>Start import:</info>');
8388

8489
try {
8590
$importers = $this->configImporterPool->getImporters();
8691

8792
if (!$importers || $this->configValidator->isValid()) {
88-
$messages[] = 'Nothing to import';
93+
$output->writeln('<info>Nothing to import</info>');
8994
} else {
9095
/**
9196
* @var string $namespace
9297
* @var ImporterInterface $importer
9398
*/
9499
foreach ($importers as $namespace => $importer) {
95-
$messages = array_merge(
96-
$messages,
97-
$importer->import($this->deploymentConfig->getConfigData($namespace))
98-
);
100+
$messages = $importer->import($this->deploymentConfig->getConfigData($namespace));
101+
$output->writeln($messages);
99102
}
100103

101104
$this->configHash->regenerate();
@@ -104,7 +107,5 @@ public function import()
104107
$this->logger->error($exception);
105108
throw new LocalizedException(__('Import is failed'), $exception);
106109
}
107-
108-
return $messages;
109110
}
110111
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Magento\Framework\Console\Cli;
13-
use Magento\Deploy\Model\DeploymentConfig\Importer;
13+
use Magento\Deploy\Console\Command\App\ConfigImport\Importer;
1414

1515
/**
1616
* Imports data from deployment configuration files to the DB.
@@ -55,8 +55,7 @@ protected function configure()
5555
protected function execute(InputInterface $input, OutputInterface $output)
5656
{
5757
try {
58-
$messages = $this->importer->import();
59-
$output->writeln($messages);
58+
$this->importer->import($output);
6059
} catch (LocalizedException $e) {
6160
$output->writeln('<error>' . $e->getMessage() . '</error>');
6261

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ImporterPool
2121
* Sections are defined with importers in di.xml
2222
* E.g.
2323
* ```xml
24-
* <type name="Magento\Framework\App\DeploymentConfig\ConfigImporterPool">
24+
* <type name="Magento\Deploy\Model\DeploymentConfig\ImporterPool">
2525
* <arguments>
2626
* <argument name="importers" xsi:type="array">
2727
* <item name="scopes" xsi:type="string">Magento\Store\Model\StoreImporter</item>
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Deploy\Test\Unit\Model\DeploymentConfig;
7+
8+
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Psr\Log\LoggerInterface as Logger;
12+
use Magento\Deploy\Console\Command\App\ConfigImport\Importer;
13+
use Magento\Deploy\Model\DeploymentConfig\Validator;
14+
use Magento\Deploy\Model\DeploymentConfig\Hash;
15+
use Magento\Deploy\Model\DeploymentConfig\ImporterPool;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
18+
class ImporterTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var Validator|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $configValidatorMock;
24+
25+
/**
26+
* @var ImporterPool|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $configImporterPoolMock;
29+
30+
/**
31+
* @var DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $deploymentConfigMock;
34+
35+
/**
36+
* @var Hash|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $configHashMock;
39+
40+
/**
41+
* @var Logger|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $loggerMock;
44+
45+
/**
46+
* @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $outputMock;
49+
50+
/**
51+
* @var Importer
52+
*/
53+
private $importer;
54+
55+
/**
56+
* @return void
57+
*/
58+
protected function setUp()
59+
{
60+
$this->configValidatorMock = $this->getMockBuilder(Validator::class)
61+
->disableOriginalConstructor()
62+
->getMock();
63+
$this->configImporterPoolMock = $this->getMockBuilder(ImporterPool::class)
64+
->disableOriginalConstructor()
65+
->getMock();
66+
$this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
67+
->disableOriginalConstructor()
68+
->getMock();
69+
$this->configHashMock = $this->getMockBuilder(Hash::class)
70+
->disableOriginalConstructor()
71+
->getMock();
72+
$this->loggerMock = $this->getMockBuilder(Logger::class)
73+
->disableOriginalConstructor()
74+
->getMock();
75+
$this->outputMock = $this->getMockBuilder(OutputInterface::class)
76+
->getMockForAbstractClass();
77+
78+
$this->importer = new Importer(
79+
$this->configValidatorMock,
80+
$this->configImporterPoolMock,
81+
$this->deploymentConfigMock,
82+
$this->configHashMock,
83+
$this->loggerMock
84+
);
85+
}
86+
87+
/**
88+
* @return void
89+
*/
90+
public function testImport()
91+
{
92+
$configData = ['some data'];
93+
$messages = ['Import has done'];
94+
$expectsMessages = ['Import has done'];
95+
$importerMock = $this->getMockBuilder(ImporterInterface::class)
96+
->getMockForAbstractClass();
97+
$importers = ['someSection' => $importerMock];
98+
99+
$this->configImporterPoolMock->expects($this->once())
100+
->method('getImporters')
101+
->willReturn($importers);
102+
$this->configValidatorMock->expects($this->any())
103+
->method('isValid')
104+
->willReturn(false);
105+
$this->deploymentConfigMock->expects($this->once())
106+
->method('getConfigData')
107+
->with('someSection')
108+
->willReturn($configData);
109+
$importerMock->expects($this->once())
110+
->method('import')
111+
->with($configData)
112+
->willReturn($messages);
113+
$this->configHashMock->expects($this->once())
114+
->method('regenerate');
115+
$this->loggerMock->expects($this->never())
116+
->method('error');
117+
118+
$this->outputMock->expects($this->at(0))
119+
->method('writeln')
120+
->with('<info>Start import:</info>');
121+
$this->outputMock->expects($this->at(1))
122+
->method('writeln')
123+
->with($expectsMessages);
124+
125+
$this->importer->import($this->outputMock);
126+
}
127+
128+
/**
129+
* @return void
130+
* @expectedException \Magento\Framework\Exception\LocalizedException
131+
* @expectedExceptionMessage Import is failed
132+
*/
133+
public function testImportWithException()
134+
{
135+
$exception = new LocalizedException(__('Some error'));
136+
$this->outputMock->expects($this->at(0))
137+
->method('writeln')
138+
->with('<info>Start import:</info>');
139+
$this->configImporterPoolMock->expects($this->once())
140+
->method('getImporters')
141+
->willThrowException($exception);
142+
$this->loggerMock->expects($this->once())
143+
->method('error')
144+
->with($exception);
145+
146+
$this->importer->import($this->outputMock);
147+
}
148+
149+
/**
150+
* @param array $importers
151+
* @param bool $isValid
152+
* @return void
153+
* @dataProvider importNothingToImportDataProvider
154+
*/
155+
public function testImportNothingToImport(array $importers, $isValid)
156+
{
157+
$this->configImporterPoolMock->expects($this->once())
158+
->method('getImporters')
159+
->willReturn($importers);
160+
$this->configValidatorMock->expects($this->any())
161+
->method('isValid')
162+
->willReturn($isValid);
163+
$this->deploymentConfigMock->expects($this->never())
164+
->method('getConfigData');
165+
$this->configHashMock->expects($this->never())
166+
->method('regenerate');
167+
$this->loggerMock->expects($this->never())
168+
->method('error');
169+
170+
$this->outputMock->expects($this->at(0))
171+
->method('writeln')
172+
->with('<info>Start import:</info>');
173+
$this->outputMock->expects($this->at(1))
174+
->method('writeln')
175+
->with('<info>Nothing to import</info>');
176+
177+
$this->importer->import($this->outputMock);
178+
}
179+
180+
/**
181+
* @return array
182+
*/
183+
public function importNothingToImportDataProvider()
184+
{
185+
return [
186+
['importers' => [], 'isValid' => true],
187+
['importers' => [], 'isValid' => false],
188+
['importers' => ['someImporter'], 'isValid' => true],
189+
];
190+
}
191+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Deploy\Test\Unit\Console\Command\App;
77

88
use Magento\Deploy\Console\Command\App\ConfigImportCommand;
9-
use Magento\Deploy\Model\DeploymentConfig\Importer;
9+
use Magento\Deploy\Console\Command\App\ConfigImport\Importer;
1010
use Magento\Framework\Console\Cli;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Symfony\Component\Console\Tester\CommandTester;
@@ -45,11 +45,9 @@ protected function setUp()
4545
public function testExecute()
4646
{
4747
$this->importerMock->expects($this->once())
48-
->method('import')
49-
->willReturn(['Import in progress']);
48+
->method('import');
5049

5150
$this->assertSame(Cli::RETURN_SUCCESS, $this->commandTester->execute([]));
52-
$this->assertContains('Import in progress', $this->commandTester->getDisplay());
5351
}
5452

5553
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ protected function setUp()
4141
$this->dataCollector = new DataCollector($this->configImporterPoolMock, $this->deploymentConfigMock);
4242
}
4343

44+
/**
45+
* @return void
46+
*/
4447
public function testGetConfig()
4548
{
4649
$sections = ['first', 'second'];

0 commit comments

Comments
 (0)