Skip to content

Commit 5b50b3f

Browse files
author
Anna Bukatar
committed
Merge branch 'ACP2E-49' of https://github.com/magento-l3/magento2ce into PR-2022-03-11
2 parents 8ab9c9f + e5f320c commit 5b50b3f

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

lib/internal/Magento/Framework/Setup/Patch/PatchApplier.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class PatchApplier
2222
/**
2323
* Flag means, that we need to read schema patches
2424
*/
25-
const SCHEMA_PATCH = 'schema';
25+
public const SCHEMA_PATCH = 'schema';
2626

2727
/**
2828
* Flag means, that we need to read data patches
2929
*/
30-
const DATA_PATCH = 'data';
30+
public const DATA_PATCH = 'data';
3131

3232
/**
3333
* @var PatchRegistryFactory
@@ -162,7 +162,9 @@ public function applyDataPatch($moduleName = null)
162162
$dataPatch->apply();
163163
$this->patchHistory->fixPatch(get_class($dataPatch));
164164
foreach ($dataPatch->getAliases() as $patchAlias) {
165-
$this->patchHistory->fixPatch($patchAlias);
165+
if (!$this->patchHistory->isApplied($patchAlias)) {
166+
$this->patchHistory->fixPatch($patchAlias);
167+
}
166168
}
167169
$this->moduleDataSetup->getConnection()->commit();
168170
} catch (\Exception $e) {
@@ -241,7 +243,9 @@ public function applySchemaPatch($moduleName = null)
241243
$schemaPatch->apply();
242244
$this->patchHistory->fixPatch(get_class($schemaPatch));
243245
foreach ($schemaPatch->getAliases() as $patchAlias) {
244-
$this->patchHistory->fixPatch($patchAlias);
246+
if (!$this->patchHistory->isApplied($patchAlias)) {
247+
$this->patchHistory->fixPatch($patchAlias);
248+
}
245249
}
246250
} catch (\Exception $e) {
247251
throw new SetupException(

lib/internal/Magento/Framework/Setup/Test/Unit/Patch/PatchApplierTest.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ public function testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatc
159159
]
160160
);
161161

162+
// phpstan:ignore
162163
$patches = [
163164
\SomeDataPatch::class,
165+
// phpstan:ignore
164166
\OtherDataPatch::class
165167
];
166168
$patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, ['registerPatch']);
@@ -170,16 +172,20 @@ public function testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatc
170172
$this->patchRegistryFactoryMock->expects($this->any())
171173
->method('create')
172174
->willReturn($patchRegistryMock);
173-
175+
// phpstan:ignore "Class SomeDataPatch not found."
174176
$patch1 = $this->createMock(\SomeDataPatch::class);
175177
$patch1->expects($this->once())->method('apply');
176178
$patch1->expects($this->once())->method('getAliases')->willReturn([]);
179+
// phpstan:ignore "Class OtherDataPatch not found."
177180
$patch2 = $this->createMock(\OtherDataPatch::class);
178181
$patch2->expects($this->once())->method('apply');
179182
$patch2->expects($this->once())->method('getAliases')->willReturn([]);
183+
180184
$this->objectManagerMock->expects($this->any())->method('create')->willReturnMap(
181185
[
186+
// phpstan:ignore
182187
['\\' . \SomeDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
188+
// phpstan:ignore
183189
['\\' . \OtherDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
184190
]
185191
);
@@ -203,8 +209,6 @@ public function testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatc
203209
*/
204210
public function testApplyDataPatchForAlias($moduleName, $dataPatches, $moduleVersionInDb)
205211
{
206-
$this->expectException('Exception');
207-
$this->expectExceptionMessageMatches('"Unable to apply data patch .+ cannot be applied twice"');
208212
$this->dataPatchReaderMock->expects($this->once())
209213
->method('read')
210214
->with($moduleName)
@@ -233,15 +237,6 @@ public function testApplyDataPatchForAlias($moduleName, $dataPatches, $moduleVer
233237
['\\' . $patchClass, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
234238
]
235239
);
236-
$this->connectionMock->expects($this->exactly(1))->method('beginTransaction');
237-
$this->connectionMock->expects($this->never())->method('commit');
238-
$this->patchHistoryMock->expects($this->any())->method('fixPatch')->willReturnCallback(
239-
function ($param1) {
240-
if ($param1 == 'PatchAlias') {
241-
throw new \LogicException(sprintf("Patch %s cannot be applied twice", $param1));
242-
}
243-
}
244-
);
245240
$this->patchApllier->applyDataPatch($moduleName);
246241
}
247242

@@ -254,7 +249,9 @@ public function applyDataPatchDataNewModuleProvider()
254249
'newly installed module' => [
255250
'moduleName' => 'Module1',
256251
'dataPatches' => [
252+
// phpstan:ignore
257253
\SomeDataPatch::class,
254+
// phpstan:ignore
258255
\OtherDataPatch::class
259256
],
260257
'moduleVersionInDb' => null,
@@ -282,8 +279,10 @@ public function testApplyDataPatchForInstalledModule($moduleName, $dataPatches,
282279
]
283280
);
284281

282+
// phpstan:ignore
285283
$patches = [
286284
\SomeDataPatch::class,
285+
// phpstan:ignore
287286
\OtherDataPatch::class
288287
];
289288
$patchRegistryMock = $this->createAggregateIteratorMock(
@@ -298,15 +297,20 @@ public function testApplyDataPatchForInstalledModule($moduleName, $dataPatches,
298297
->method('create')
299298
->willReturn($patchRegistryMock);
300299

300+
// phpstan:ignore "Class SomeDataPatch not found."
301301
$patch1 = $this->createMock(\SomeDataPatch::class);
302302
$patch1->expects(self::never())->method('apply');
303303
$patch1->expects(self::any())->method('getAliases')->willReturn([]);
304+
// phpstan:ignore "Class OtherDataPatch not found."
304305
$patch2 = $this->createMock(\OtherDataPatch::class);
305306
$patch2->expects(self::once())->method('apply');
306307
$patch2->expects(self::any())->method('getAliases')->willReturn([]);
308+
307309
$this->objectManagerMock->expects(self::any())->method('create')->willReturnMap(
308310
[
311+
// phpstan:ignore
309312
['\\' . \SomeDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
313+
// phpstan:ignore
310314
['\\' . \OtherDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
311315
]
312316
);
@@ -325,7 +329,9 @@ public function applyDataPatchDataInstalledModuleProvider()
325329
'upgrade module iwth only OtherDataPatch' => [
326330
'moduleName' => 'Module1',
327331
'dataPatches' => [
332+
// phpstan:ignore
328333
\SomeDataPatch::class,
334+
// phpstan:ignore
329335
\OtherDataPatch::class
330336
],
331337
'moduleVersionInDb' => '2.0.0',
@@ -356,8 +362,10 @@ public function testApplyDataPatchRollback($moduleName, $dataPatches, $moduleVer
356362
]
357363
);
358364

365+
// phpstan:ignore
359366
$patches = [
360367
\SomeDataPatch::class,
368+
// phpstan:ignore
361369
\OtherDataPatch::class
362370
];
363371
$patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, ['registerPatch']);
@@ -368,14 +376,19 @@ public function testApplyDataPatchRollback($moduleName, $dataPatches, $moduleVer
368376
->method('create')
369377
->willReturn($patchRegistryMock);
370378

379+
// phpstan:ignore "Class SomeDataPatch not found."
371380
$patch1 = $this->createMock(\SomeDataPatch::class);
372381
$patch1->expects($this->never())->method('apply');
382+
// phpstan:ignore "Class OtherDataPatch not found."
373383
$patch2 = $this->createMock(\OtherDataPatch::class);
374384
$exception = new \Exception('Patch Apply Error');
375385
$patch2->expects($this->once())->method('apply')->willThrowException($exception);
386+
376387
$this->objectManagerMock->expects($this->any())->method('create')->willReturnMap(
377388
[
389+
// phpstan:ignore
378390
['\\' . \SomeDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
391+
// phpstan:ignore
379392
['\\' . \OtherDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
380393
]
381394
);
@@ -421,6 +434,7 @@ public function testNonDataPatchApply()
421434

422435
public function testNonTransactionablePatch()
423436
{
437+
// phpstan:ignore "Class NonTransactionableDataPatch not found."
424438
$patches = [\NonTransactionableDataPatch::class];
425439
$this->dataPatchReaderMock->expects($this->once())
426440
->method('read')
@@ -477,8 +491,10 @@ public function testSchemaPatchAplly($moduleName, $schemaPatches, $moduleVersion
477491
]
478492
);
479493

494+
// phpstan:ignore
480495
$patches = [
481496
\SomeSchemaPatch::class,
497+
// phpstan:ignore
482498
\OtherSchemaPatch::class
483499
];
484500
$patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, ['registerPatch']);
@@ -489,15 +505,19 @@ public function testSchemaPatchAplly($moduleName, $schemaPatches, $moduleVersion
489505
->method('create')
490506
->willReturn($patchRegistryMock);
491507

508+
// phpstan:ignore "Class SomeSchemaPatch not found."
492509
$patch1 = $this->createMock(\SomeSchemaPatch::class);
493510
$patch1->expects($this->never())->method('apply');
494511
$patch1->expects($this->any())->method('getAliases')->willReturn([]);
512+
// phpstan:ignore "Class OtherSchemaPatch not found."
495513
$patch2 = $this->createMock(\OtherSchemaPatch::class);
496514
$patch2->expects($this->once())->method('apply');
497515
$patch2->expects($this->any())->method('getAliases')->willReturn([]);
498516
$this->patchFactoryMock->expects($this->any())->method('create')->willReturnMap(
499517
[
518+
// phpstan:ignore
500519
[\SomeSchemaPatch::class, ['schemaSetup' => $this->schemaSetupMock], $patch1],
520+
// phpstan:ignore
501521
[\OtherSchemaPatch::class, ['schemaSetup' => $this->schemaSetupMock], $patch2],
502522
]
503523
);
@@ -516,8 +536,6 @@ public function testSchemaPatchAplly($moduleName, $schemaPatches, $moduleVersion
516536
*/
517537
public function testSchemaPatchApplyForPatchAlias($moduleName, $schemaPatches, $moduleVersionInDb)
518538
{
519-
$this->expectException('Exception');
520-
$this->expectExceptionMessageMatches('"Unable to apply patch .+ cannot be applied twice"');
521539
$this->schemaPatchReaderMock->expects($this->once())
522540
->method('read')
523541
->with($moduleName)
@@ -542,19 +560,13 @@ public function testSchemaPatchApplyForPatchAlias($moduleName, $schemaPatches, $
542560
->willReturn($patchRegistryMock);
543561

544562
$this->patchFactoryMock->expects($this->any())->method('create')->willReturn($patch1);
545-
$this->patchHistoryMock->expects($this->any())->method('fixPatch')->willReturnCallback(
546-
function ($param1) {
547-
if ($param1 == 'PatchAlias') {
548-
throw new \LogicException(sprintf("Patch %s cannot be applied twice", $param1));
549-
}
550-
}
551-
);
552563

553564
$this->patchApllier->applySchemaPatch($moduleName);
554565
}
555566

556567
public function testRevertDataPatches()
557568
{
569+
// phpstan:ignore "Class RevertableDataPatch not found."
558570
$patches = [\RevertableDataPatch::class];
559571
$this->dataPatchReaderMock->expects($this->once())
560572
->method('read')
@@ -602,7 +614,9 @@ public function schemaPatchDataProvider()
602614
'upgrade module iwth only OtherSchemaPatch' => [
603615
'moduleName' => 'Module1',
604616
'schemaPatches' => [
617+
// phpstan:ignore
605618
\SomeSchemaPatch::class,
619+
// phpstan:ignore
606620
\OtherSchemaPatch::class
607621
],
608622
'moduleVersionInDb' => '2.0.0',

0 commit comments

Comments
 (0)