Skip to content

Commit 8037056

Browse files
committed
Merge branch 'AC-2258-add-test-coverage-for-installer-fix' into AC-2425-update-elasticsearch-7.17
2 parents 5493217 + bc1427d commit 8037056

File tree

2 files changed

+96
-56
lines changed

2 files changed

+96
-56
lines changed

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Magento\Framework\Model\ResourceModel\Db\Context;
3131
use Magento\Framework\Module\ModuleList\Loader as ModuleLoader;
3232
use Magento\Framework\Module\ModuleListInterface;
33+
use Magento\Framework\Module\ModuleResource;
3334
use Magento\Framework\Mview\TriggerCleaner;
3435
use Magento\Framework\Setup\Declaration\Schema\DryRunLogger;
3536
use Magento\Framework\Setup\FilePermissions;
@@ -68,35 +69,32 @@
6869
*/
6970
class Installer
7071
{
71-
/**#@+
72+
/**
7273
* Parameters for enabling/disabling modules
7374
*/
74-
const ENABLE_MODULES = 'enable-modules';
75-
const DISABLE_MODULES = 'disable-modules';
76-
/**#@- */
75+
public const ENABLE_MODULES = 'enable-modules';
76+
public const DISABLE_MODULES = 'disable-modules';
7777

78-
/**#@+
78+
/**
7979
* Formatting for progress log
8080
*/
81-
const PROGRESS_LOG_RENDER = '[Progress: %d / %d]';
82-
const PROGRESS_LOG_REGEX = '/\[Progress: (\d+) \/ (\d+)\]/s';
83-
/**#@- */
81+
public const PROGRESS_LOG_RENDER = '[Progress: %d / %d]';
82+
public const PROGRESS_LOG_REGEX = '/\[Progress: (\d+) \/ (\d+)\]/s';
8483

85-
/**#@+
84+
/**
8685
* Instance types for schema and data handler
8786
*/
88-
const SCHEMA_INSTALL = \Magento\Framework\Setup\InstallSchemaInterface::class;
89-
const SCHEMA_UPGRADE = \Magento\Framework\Setup\UpgradeSchemaInterface::class;
90-
const DATA_INSTALL = \Magento\Framework\Setup\InstallDataInterface::class;
91-
const DATA_UPGRADE = \Magento\Framework\Setup\UpgradeDataInterface::class;
92-
/**#@- */
87+
public const SCHEMA_INSTALL = \Magento\Framework\Setup\InstallSchemaInterface::class;
88+
public const SCHEMA_UPGRADE = \Magento\Framework\Setup\UpgradeSchemaInterface::class;
89+
public const DATA_INSTALL = \Magento\Framework\Setup\InstallDataInterface::class;
90+
public const DATA_UPGRADE = \Magento\Framework\Setup\UpgradeDataInterface::class;
9391

94-
const INFO_MESSAGE = 'message';
92+
public const INFO_MESSAGE = 'message';
9593

9694
/**
9795
* The lowest supported MySQL verion
9896
*/
99-
const MYSQL_VERSION_REQUIRED = '5.6.0';
97+
public const MYSQL_VERSION_REQUIRED = '5.6.0';
10098

10199
/**
102100
* File permissions checker
@@ -106,22 +104,16 @@ class Installer
106104
private $filePermissions;
107105

108106
/**
109-
* Deployment configuration repository
110-
*
111107
* @var Writer
112108
*/
113109
private $deploymentConfigWriter;
114110

115111
/**
116-
* Deployment configuration reader
117-
*
118112
* @var Reader
119113
*/
120114
private $deploymentConfigReader;
121115

122116
/**
123-
* Module list
124-
*
125117
* @var ModuleListInterface
126118
*/
127119
private $moduleList;
@@ -134,8 +126,6 @@ class Installer
134126
private $moduleLoader;
135127

136128
/**
137-
* Admin account factory
138-
*
139129
* @var AdminAccountFactory
140130
*/
141131
private $adminAccountFactory;
@@ -232,8 +222,6 @@ class Installer
232222
protected $sampleDataState;
233223

234224
/**
235-
* Component Registrar
236-
*
237225
* @var ComponentRegistrar
238226
*/
239227
private $componentRegistrar;
@@ -1029,7 +1017,7 @@ private function handleDBSchemaData($setup, $type, array $request)
10291017
// phpcs:ignore Magento2.Exceptions.DirectThrow
10301018
throw new Exception("Unsupported operation type $type is requested");
10311019
}
1032-
$resource = new \Magento\Framework\Module\ModuleResource($this->context);
1020+
$resource = $this->getModuleResource();
10331021
$verType = $type . '-version';
10341022
$installType = $type . '-install';
10351023
$upgradeType = $type . '-upgrade';
@@ -1063,7 +1051,7 @@ private function handleDBSchemaData($setup, $type, array $request)
10631051
$configVer = $this->moduleList->getOne($moduleName)['setup_version'];
10641052
$currentVersion = $moduleContextList[$moduleName]->getVersion();
10651053
// Schema/Data is installed
1066-
if ($currentVersion !== '') {
1054+
if ($configVer !== null && $currentVersion !== '') {
10671055
$status = version_compare($configVer, $currentVersion);
10681056
if ($status == \Magento\Framework\Setup\ModuleDataSetupInterface::VERSION_COMPARE_GREATER) {
10691057
$upgrader = $this->getSchemaDataHandler($moduleName, $upgradeType);
@@ -1133,6 +1121,16 @@ private function handleDBSchemaData($setup, $type, array $request)
11331121
}
11341122
}
11351123

1124+
/**
1125+
* Get a module Resource object
1126+
*
1127+
* @return ModuleResource
1128+
*/
1129+
public function getModuleResource(): ModuleResource
1130+
{
1131+
return new ModuleResource($this->context);
1132+
}
1133+
11361134
/**
11371135
* Assert DbConfigExists
11381136
*
@@ -1663,7 +1661,7 @@ private function getSchemaDataHandler($moduleName, $type)
16631661
/**
16641662
* Generates list of ModuleContext
16651663
*
1666-
* @param \Magento\Framework\Module\ModuleResource $resource
1664+
* @param ModuleResource $resource
16671665
* @param string $type
16681666
* @return ModuleContext[]
16691667
* @throws Exception

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

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Magento\Framework\Model\ResourceModel\Db\Context;
3030
use Magento\Framework\Module\ModuleList\Loader;
3131
use Magento\Framework\Module\ModuleListInterface;
32+
use Magento\Framework\Module\ModuleResource;
3233
use Magento\Framework\ObjectManagerInterface;
3334
use Magento\Framework\Registry;
3435
use Magento\Framework\Setup\FilePermissions;
@@ -77,7 +78,7 @@ class InstallerTest extends TestCase
7778
];
7879

7980
/**
80-
* @var Installer
81+
* @var Installer|MockObject
8182
*/
8283
private $object;
8384

@@ -230,9 +231,6 @@ protected function setUp(): void
230231
$this->config = $this->createMock(DeploymentConfig::class);
231232

232233
$this->moduleList = $this->getMockForAbstractClass(ModuleListInterface::class);
233-
$this->moduleList->expects($this->any())->method('getOne')->willReturn(
234-
['setup_version' => '2.0.0']
235-
);
236234
$this->moduleList->expects($this->any())->method('getNames')->willReturn(
237235
['Foo_One', 'Bar_Two']
238236
);
@@ -282,29 +280,35 @@ private function createObject($connectionFactory = false, $objectManagerProvider
282280
$objectManagerProvider->expects($this->any())->method('get')->willReturn($this->objectManager);
283281
}
284282

285-
return new Installer(
286-
$this->filePermissions,
287-
$this->configWriter,
288-
$this->configReader,
289-
$this->config,
290-
$this->moduleList,
291-
$this->moduleLoader,
292-
$this->adminFactory,
293-
$this->logger,
294-
$connectionFactory,
295-
$this->maintenanceMode,
296-
$this->filesystem,
297-
$objectManagerProvider,
298-
$this->contextMock,
299-
$this->configModel,
300-
$this->cleanupFiles,
301-
$this->dbValidator,
302-
$this->setupFactory,
303-
$this->dataSetupFactory,
304-
$this->sampleDataState,
305-
$this->componentRegistrar,
306-
$this->phpReadinessCheck
307-
);
283+
$installer = $this->getMockBuilder(Installer::class)
284+
->enableOriginalConstructor()
285+
->onlyMethods(['getModuleResource'])
286+
->setConstructorArgs([
287+
'filePermissions' => $this->filePermissions,
288+
'deploymentConfigWriter' => $this->configWriter,
289+
'deploymentConfigReader' => $this->configReader,
290+
'deploymentConfig' => $this->config,
291+
'moduleList' => $this->moduleList,
292+
'moduleLoader' => $this->moduleLoader,
293+
'adminAccountFactory' => $this->adminFactory,
294+
'log' => $this->logger,
295+
'connectionFactory' => $connectionFactory,
296+
'maintenanceMode' => $this->maintenanceMode,
297+
'filesystem' => $this->filesystem,
298+
'objectManagerProvider' => $objectManagerProvider,
299+
'context' => $this->contextMock,
300+
'setupConfigModel' => $this->configModel,
301+
'cleanupFiles' => $this->cleanupFiles,
302+
'dbValidator' => $this->dbValidator,
303+
'setupFactory' => $this->setupFactory,
304+
'dataSetupFactory' => $this->dataSetupFactory,
305+
'sampleDataState' => $this->sampleDataState,
306+
'componentRegistrar' => $this->componentRegistrar,
307+
'phpReadinessCheck' => $this->phpReadinessCheck,
308+
])
309+
->getMock();
310+
311+
return $installer;
308312
}
309313

310314
/**
@@ -315,6 +319,14 @@ private function createObject($connectionFactory = false, $objectManagerProvider
315319
*/
316320
public function testInstall(array $request, array $logMessages)
317321
{
322+
$this->moduleList->method('getOne')
323+
->willReturnMap(
324+
[
325+
['Foo_One', ['setup_version' => '2.0.0']],
326+
['Bar_Two', ['setup_version' => null]]
327+
]
328+
);
329+
318330
$this->config->expects($this->atLeastOnce())
319331
->method('get')
320332
->willReturnMap(
@@ -344,6 +356,16 @@ public function testInstall(array $request, array $logMessages)
344356
$resource = $this->createMock(ResourceConnection::class);
345357
$this->contextMock->expects($this->any())->method('getResources')->willReturn($resource);
346358
$resource->expects($this->any())->method('getConnection')->willReturn($connection);
359+
360+
$moduleResource = $this->getMockBuilder(ModuleResource::class)
361+
->enableOriginalConstructor()
362+
->onlyMethods(['getDbVersion', 'getDataVersion'])
363+
->setConstructorArgs(['context' => $this->contextMock])
364+
->getMock();
365+
$moduleResource->method('getDbVersion')->willReturnOnConsecutiveCalls(false, '2.1.0');
366+
$moduleResource->method('getDataVersion')->willReturn(false);
367+
$this->object->method('getModuleResource')->willReturn($moduleResource);
368+
347369
$dataSetup = $this->createMock(DataSetup::class);
348370
$dataSetup->expects($this->any())->method('getConnection')->willReturn($connection);
349371
$cacheManager = $this->createMock(Manager::class);
@@ -581,6 +603,7 @@ public function testInstallWithInvalidRemoteStorageConfiguration(bool $isDeploym
581603
['modules/Magento_User', null, '1']
582604
]
583605
);
606+
$this->moduleList->method('getOne')->willReturn(['setup_version' => '2.0.0']);
584607
$allModules = ['Foo_One' => [], 'Bar_Two' => []];
585608

586609
$this->declarationInstallerMock->expects(static::once())->method('installSchema');
@@ -603,6 +626,7 @@ public function testInstallWithInvalidRemoteStorageConfiguration(bool $isDeploym
603626
$resource->expects(static::any())->method('getConnection')->willReturn($connection);
604627

605628
$this->contextMock->expects(static::exactly(2))->method('getResources')->willReturn($resource);
629+
$this->setModuleResource();
606630

607631
$dataSetup = $this->createMock(DataSetup::class);
608632
$dataSetup->expects(static::never())->method('getConnection');
@@ -752,6 +776,7 @@ public function testInstallWithUnresolvableRemoteStorageValidator()
752776
]
753777
);
754778
$allModules = ['Foo_One' => [], 'Bar_Two' => []];
779+
$this->moduleList->method('getOne')->willReturn(['setup_version' => '2.0.0']);
755780

756781
$this->declarationInstallerMock->expects(static::once())->method('installSchema');
757782
$this->moduleLoader->expects(static::any())->method('load')->willReturn($allModules);
@@ -771,6 +796,8 @@ public function testInstallWithUnresolvableRemoteStorageValidator()
771796
$resource = $this->createMock(ResourceConnection::class);
772797
$this->contextMock->expects(static::any())->method('getResources')->willReturn($resource);
773798
$resource->expects(static::any())->method('getConnection')->willReturn($connection);
799+
$this->setModuleResource();
800+
774801
$dataSetup = $this->createMock(DataSetup::class);
775802
$dataSetup->expects(static::any())->method('getConnection')->willReturn($connection);
776803
$cacheManager = $this->createMock(Manager::class);
@@ -939,6 +966,7 @@ public function testInstallWithInvalidRemoteStorageConfigurationWithEarlyExcepti
939966
]
940967
);
941968
$allModules = ['Foo_One' => [], 'Bar_Two' => []];
969+
$this->moduleList->method('getOne')->willReturn(['setup_version' => '2.0.0']);
942970

943971
$this->declarationInstallerMock
944972
->expects(static::once())
@@ -1054,6 +1082,8 @@ public function installWithInvalidRemoteStorageConfigurationWithEarlyExceptionDa
10541082
*/
10551083
public function testInstallDataFixtures(bool $keepCache, array $expectedToEnableCacheTypes): void
10561084
{
1085+
$this->moduleList->method('getOne')->willReturn(['setup_version' => '2.0.0']);
1086+
10571087
$cacheManagerMock = $this->createMock(Manager::class);
10581088
//simulate disabled layout cache type
10591089
$cacheManagerMock->expects($this->atLeastOnce())
@@ -1108,6 +1138,7 @@ public function testInstallDataFixtures(bool $keepCache, array $expectedToEnable
11081138
$this->contextMock->expects($this->once())
11091139
->method('getResources')
11101140
->willReturn($resource);
1141+
$this->setModuleResource();
11111142

11121143
$dataSetup = $this->createMock(DataSetup::class);
11131144
$dataSetup->expects($this->once())
@@ -1367,6 +1398,17 @@ private function prepareForUpdateModulesTests()
13671398

13681399
return $newObject;
13691400
}
1401+
1402+
/**
1403+
* Sets a new ModuleResource object to the installer
1404+
*
1405+
* @return void
1406+
*/
1407+
private function setModuleResource(): void
1408+
{
1409+
$moduleResource = new ModuleResource($this->contextMock);
1410+
$this->object->method('getModuleResource')->willReturn($moduleResource);
1411+
}
13701412
}
13711413
}
13721414

0 commit comments

Comments
 (0)