Skip to content

Commit c7ad941

Browse files
authored
Merge pull request #995 from magento-falcons/MAGETWO-66853
Fixed issues: - MAGETWO-66736 Asymmetric transaction rollback error during stores importing - MAGETWO-66672 Impossible generate fixture if store_groups>1 - MAGETWO-61431 [GitHub] CLI issue with bin/magento and exit codes for failure #7576
2 parents 58edd7f + c3852a7 commit c7ad941

File tree

6 files changed

+150
-47
lines changed

6 files changed

+150
-47
lines changed

app/code/Magento/Store/Model/Config/Importer.php

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

88
use Magento\Framework\App\CacheInterface;
99
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
10-
use Magento\Framework\App\ResourceConnection;
1110
use Magento\Framework\Exception\State\InvalidTransitionException;
1211
use Magento\Store\Model\Config\Importer\DataDifferenceCalculator;
1312
use Magento\Store\Model\Config\Importer\Processor\ProcessorFactory;
1413
use Magento\Store\Model\ScopeInterface;
1514
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\Store\Model\ResourceModel\Website;
1616

1717
/**
1818
* Imports stores, websites and groups from transmitted data.
@@ -43,7 +43,7 @@ class Importer implements ImporterInterface
4343
/**
4444
* The resource of transaction.
4545
*
46-
* @var ResourceConnection
46+
* @var Website
4747
*/
4848
private $resource;
4949

@@ -59,14 +59,14 @@ class Importer implements ImporterInterface
5959
* @param ProcessorFactory $processFactory The factory for processes
6060
* @param StoreManagerInterface $storeManager The manager for operations with store
6161
* @param CacheInterface $cacheManager The application cache manager
62-
* @param ResourceConnection $resource The resource of transaction
62+
* @param Website $resource The resource of transaction
6363
*/
6464
public function __construct(
6565
DataDifferenceCalculator $dataDifferenceCalculator,
6666
ProcessorFactory $processFactory,
6767
StoreManagerInterface $storeManager,
6868
CacheInterface $cacheManager,
69-
ResourceConnection $resource
69+
Website $resource
7070
) {
7171
$this->dataDifferenceCalculator = $dataDifferenceCalculator;
7272
$this->processFactory = $processFactory;
@@ -100,25 +100,35 @@ public function import(array $data)
100100
);
101101
}
102102

103-
$this->resource->getConnection()->beginTransaction();
103+
$this->resource->beginTransaction();
104104

105105
foreach ($actions as $action) {
106106
$this->processFactory->create($action)->run($data);
107107
}
108-
109-
$this->resource->getConnection()->commit();
110108
} catch (\Exception $exception) {
111-
$this->resource->getConnection()->rollBack();
109+
$this->resource->rollBack();
110+
$this->reinitStores();
112111

113112
throw new InvalidTransitionException(__('%1', $exception->getMessage()), $exception);
114-
} finally {
115-
$this->storeManager->reinitStores();
116-
$this->cacheManager->clean();
117113
}
118114

115+
$this->resource->commit();
116+
$this->reinitStores();
117+
119118
return $messages;
120119
}
121120

121+
/**
122+
* Reinitialize store list.
123+
*
124+
* @return void
125+
*/
126+
private function reinitStores()
127+
{
128+
$this->storeManager->reinitStores();
129+
$this->cacheManager->clean();
130+
}
131+
122132
/**
123133
* Retrieves message reminder about root category assigning.
124134
*

app/code/Magento/Store/Test/Unit/Model/Config/ImporterTest.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
namespace Magento\Store\Test\Unit\Model\Config;
77

88
use Magento\Framework\App\CacheInterface;
9-
use Magento\Framework\App\ResourceConnection;
10-
use Magento\Framework\DB\Adapter\AdapterInterface;
119
use Magento\Store\Model\Config\Importer;
10+
use Magento\Store\Model\ResourceModel\Website;
1211
use Magento\Store\Model\ScopeInterface;
1312
use Magento\Store\Model\StoreManager;
1413
use PHPUnit_Framework_MockObject_MockObject as Mock;
@@ -52,15 +51,10 @@ class ImporterTest extends \PHPUnit_Framework_TestCase
5251
private $cacheManagerMock;
5352

5453
/**
55-
* @var ResourceConnection|Mock
54+
* @var Website|Mock
5655
*/
5756
private $resourceMock;
5857

59-
/**
60-
* @var AdapterInterface|Mock
61-
*/
62-
private $connectionMock;
63-
6458
/**
6559
* @inheritdoc
6660
*/
@@ -79,15 +73,9 @@ protected function setUp()
7973
->getMock();
8074
$this->cacheManagerMock = $this->getMockBuilder(CacheInterface::class)
8175
->getMockForAbstractClass();
82-
$this->resourceMock = $this->getMockBuilder(ResourceConnection::class)
76+
$this->resourceMock = $this->getMockBuilder(Website::class)
8377
->disableOriginalConstructor()
8478
->getMock();
85-
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
86-
->getMockForAbstractClass();
87-
88-
$this->resourceMock->expects($this->any())
89-
->method('getConnection')
90-
->willReturn($this->connectionMock);
9179
$this->processorFactoryMock->expects($this->any())
9280
->method('create')
9381
->willReturn($this->processorMock);
@@ -109,12 +97,12 @@ public function testImport()
10997
ScopeInterface::SCOPE_WEBSITES => ['websites'],
11098
];
11199

112-
$this->connectionMock->expects($this->once())
100+
$this->resourceMock->expects($this->once())
113101
->method('beginTransaction');
114102
$this->processorMock->expects($this->exactly(3))
115103
->method('run')
116104
->with($data);
117-
$this->connectionMock->expects($this->once())
105+
$this->resourceMock->expects($this->once())
118106
->method('commit');
119107
$this->storeManagerMock->expects($this->once())
120108
->method('reinitStores');
@@ -145,12 +133,12 @@ public function testImport()
145133
*/
146134
public function testImportWithException()
147135
{
148-
$this->connectionMock->expects($this->once())
136+
$this->resourceMock->expects($this->once())
149137
->method('beginTransaction');
150138
$this->processorMock->expects($this->any())
151139
->method('run')
152140
->willThrowException(new \Exception('Some error'));
153-
$this->connectionMock->expects($this->never())
141+
$this->resourceMock->expects($this->never())
154142
->method('commit');
155143
$this->storeManagerMock->expects($this->once())
156144
->method('reinitStores');

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

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

88
use Composer\Package\Version\VersionParser;
99
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\Console\Cli;
1011
use Magento\Framework\Module\DbVersionInfo;
1112
use Magento\Setup\Model\ObjectManagerProvider;
1213
use Symfony\Component\Console\Input\InputInterface;
@@ -17,6 +18,11 @@
1718
*/
1819
class DbStatusCommand extends AbstractSetupCommand
1920
{
21+
/**
22+
* Code for error when application upgrade is required.
23+
*/
24+
const EXIT_CODE_UPGRADE_REQUIRED = 2;
25+
2026
/**
2127
* Object manager provider
2228
*
@@ -60,8 +66,10 @@ protected function configure()
6066
protected function execute(InputInterface $input, OutputInterface $output)
6167
{
6268
if (!$this->deploymentConfig->isAvailable()) {
63-
$output->writeln("<info>No information is available: the Magento application is not installed.</info>");
64-
return;
69+
$output->writeln(
70+
"<info>No information is available: the Magento application is not installed.</info>"
71+
);
72+
return Cli::RETURN_FAILURE;
6573
}
6674
/** @var DbVersionInfo $dbVersionInfo */
6775
$dbVersionInfo = $this->objectManagerProvider->get()
@@ -93,13 +101,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
93101
'<info>Some modules use code versions newer or older than the database. ' .
94102
"First update the module code, then run 'setup:upgrade'.</info>"
95103
);
96-
// we must have an exit code higher than zero to indicate something was wrong
97-
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
98-
} else {
99-
$output->writeln("<info>Run 'setup:upgrade' to update your DB schema and data.</info>");
104+
return Cli::RETURN_FAILURE;
100105
}
101-
} else {
102-
$output->writeln('<info>All modules are up to date.</info>');
106+
107+
$output->writeln(
108+
"<info>Run 'setup:upgrade' to update your DB schema and data.</info>"
109+
);
110+
return static::EXIT_CODE_UPGRADE_REQUIRED;
103111
}
112+
113+
$output->writeln(
114+
'<info>All modules are up to date.</info>'
115+
);
116+
return Cli::RETURN_SUCCESS;
104117
}
105118
}

setup/src/Magento/Setup/Fixtures/StoresFixture.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,15 @@ private function generateStoreGroups()
254254
while ($existedStoreGroupCount < $this->storeGroupsCount) {
255255
$websiteId = $this->websiteIds[$existedStoreGroupCount % $existedWebsitesCount];
256256
$storeGroupName = sprintf('Store Group %d - website_id_%d', ++$existedStoreGroupCount, $websiteId);
257+
$storeGroupCode = sprintf('store_group_%d', $existedStoreGroupCount);
257258

258259
$storeGroup = clone $this->defaultStoreGroup;
259260
$storeGroup->addData(
260261
[
261262
'group_id' => null,
262263
'website_id' => $websiteId,
263264
'name' => $storeGroupName,
265+
'code' => $storeGroupCode,
264266
'root_category_id' => $this->getStoreCategoryId($storeGroupName),
265267
]
266268
);

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
*/
66
namespace Magento\Setup\Test\Unit\Console\Command;
77

8+
use Magento\Framework\Console\Cli;
89
use Magento\Framework\Module\DbVersionInfo;
910
use Magento\Setup\Console\Command\DbStatusCommand;
11+
use Magento\Framework\App\DeploymentConfig;
12+
use Magento\Setup\Model\ObjectManagerProvider;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use PHPUnit_Framework_MockObject_MockObject as Mock;
1015
use Symfony\Component\Console\Tester\CommandTester;
1116

17+
/**
18+
* @inheritdoc
19+
*/
1220
class DbStatusCommandTest extends \PHPUnit_Framework_TestCase
1321
{
1422
/**
15-
* @var \Magento\Framework\Module\DbVersionInfo|\PHPUnit_Framework_MockObject_MockObject
23+
* @var DbVersionInfo|Mock
1624
*/
1725
private $dbVersionInfo;
1826

1927
/**
20-
* @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
28+
* @var DeploymentConfig|Mock
2129
*/
2230
private $deploymentConfig;
2331

@@ -26,48 +34,66 @@ class DbStatusCommandTest extends \PHPUnit_Framework_TestCase
2634
*/
2735
private $command;
2836

37+
/**
38+
* @inheritdoc
39+
*/
2940
protected function setUp()
3041
{
31-
$this->dbVersionInfo = $this->getMock(\Magento\Framework\Module\DbVersionInfo::class, [], [], '', false);
32-
$objectManagerProvider = $this->getMock(\Magento\Setup\Model\ObjectManagerProvider::class, [], [], '', false);
33-
$objectManager = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
42+
$this->dbVersionInfo = $this->getMockBuilder(DbVersionInfo::class)
43+
->disableOriginalConstructor()
44+
->getMock();
45+
/** @var ObjectManagerProvider|Mock $objectManagerProvider */
46+
$objectManagerProvider = $this->getMockBuilder(ObjectManagerProvider::class)
47+
->disableOriginalConstructor()
48+
->getMock();
49+
/** @var ObjectManagerInterface|Mock $objectManager */
50+
$objectManager = $this->getMockForAbstractClass(ObjectManagerInterface::class);
51+
$this->deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
52+
->disableOriginalConstructor()
53+
->getMock();
54+
3455
$objectManagerProvider->expects($this->any())
3556
->method('get')
3657
->will($this->returnValue($objectManager));
3758
$objectManager->expects($this->any())
3859
->method('get')
3960
->will($this->returnValue($this->dbVersionInfo));
40-
$this->deploymentConfig = $this->getMock(\Magento\Framework\App\DeploymentConfig::class, [], [], '', false);
61+
4162
$this->command = new DbStatusCommand($objectManagerProvider, $this->deploymentConfig);
4263
}
4364

4465
/**
4566
* @param array $outdatedInfo
4667
* @param string $expectedMessage
68+
* @param int $expectedCode
4769
*
4870
* @dataProvider executeDataProvider
4971
*/
50-
public function testExecute(array $outdatedInfo, $expectedMessage)
72+
public function testExecute(array $outdatedInfo, $expectedMessage, $expectedCode)
5173
{
5274
$this->deploymentConfig->expects($this->once())
5375
->method('isAvailable')
5476
->will($this->returnValue(true));
5577
$this->dbVersionInfo->expects($this->once())
5678
->method('getDbVersionErrors')
5779
->will($this->returnValue($outdatedInfo));
80+
5881
$tester = new CommandTester($this->command);
5982
$tester->execute([]);
83+
6084
$this->assertStringMatchesFormat($expectedMessage, $tester->getDisplay());
85+
$this->assertSame($expectedCode, $tester->getStatusCode());
6186
}
6287

6388
public function executeDataProvider()
6489
{
6590
return [
6691
'DB is up to date' => [
6792
[],
68-
'All modules are up to date%a'
93+
'All modules are up to date%a',
94+
Cli::RETURN_SUCCESS
6995
],
70-
'DB is outdated' => [
96+
'DB is outdated' => [
7197
[
7298
[
7399
DbVersionInfo::KEY_MODULE => 'module_a',
@@ -78,6 +104,7 @@ public function executeDataProvider()
78104
],
79105
'%amodule_a%aschema%a1%a->%a2'
80106
. "%aRun 'setup:upgrade' to update your DB schema and data%a",
107+
DbStatusCommand::EXIT_CODE_UPGRADE_REQUIRED,
81108
],
82109
'code is outdated' => [
83110
[
@@ -90,6 +117,7 @@ public function executeDataProvider()
90117
],
91118
'%amodule_a%adata%a2.0.0%a->%a1.0.0'
92119
. '%aSome modules use code versions newer or older than the database%a',
120+
Cli::RETURN_FAILURE,
93121
],
94122
'both DB and code is outdated' => [
95123
[
@@ -109,6 +137,7 @@ public function executeDataProvider()
109137
'%amodule_a%aschema%a1.0.0%a->%a2.0.0'
110138
. '%amodule_b%adata%a2.0.0%a->%a1.0.0'
111139
. '%aSome modules use code versions newer or older than the database%a',
140+
Cli::RETURN_FAILURE,
112141
],
113142
];
114143
}
@@ -120,11 +149,14 @@ public function testExecuteNotInstalled()
120149
->will($this->returnValue(false));
121150
$this->dbVersionInfo->expects($this->never())
122151
->method('getDbVersionErrors');
152+
123153
$tester = new CommandTester($this->command);
124154
$tester->execute([]);
155+
125156
$this->assertStringMatchesFormat(
126157
'No information is available: the Magento application is not installed.%w',
127158
$tester->getDisplay()
128159
);
160+
$this->assertSame(Cli::RETURN_FAILURE, $tester->getStatusCode());
129161
}
130162
}

0 commit comments

Comments
 (0)