10
10
use Magento \Framework \DB \Adapter \AdapterInterface ;
11
11
use Magento \Framework \Module \ModuleResource ;
12
12
use Magento \Framework \ObjectManagerInterface ;
13
+ use Magento \Framework \Setup \Exception ;
13
14
use Magento \Framework \Setup \ModuleDataSetupInterface ;
15
+ use Magento \Framework \Setup \Patch \DataPatchInterface ;
14
16
use Magento \Framework \Setup \Patch \PatchBackwardCompatability ;
17
+ use Magento \Framework \Setup \Patch \PatchInterface ;
15
18
use Magento \Framework \Setup \SchemaSetupInterface ;
16
19
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17
20
use Magento \Framework \Setup \Patch \PatchApplier ;
@@ -169,8 +172,10 @@ public function testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatc
169
172
170
173
$ patch1 = $ this ->createMock (\SomeDataPatch::class);
171
174
$ patch1 ->expects ($ this ->once ())->method ('apply ' );
175
+ $ patch1 ->expects ($ this ->once ())->method ('getAliases ' )->willReturn ([]);
172
176
$ patch2 = $ this ->createMock (\OtherDataPatch::class);
173
177
$ patch2 ->expects ($ this ->once ())->method ('apply ' );
178
+ $ patch2 ->expects ($ this ->once ())->method ('getAliases ' )->willReturn ([]);
174
179
$ this ->objectManagerMock ->expects ($ this ->any ())->method ('create ' )->willReturnMap (
175
180
[
176
181
['\\' . \SomeDataPatch::class, ['moduleDataSetup ' => $ this ->moduleDataSetupMock ], $ patch1 ],
@@ -188,6 +193,60 @@ public function testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatc
188
193
$ this ->patchApllier ->applyDataPatch ($ moduleName );
189
194
}
190
195
196
+ /**
197
+ * @param $moduleName
198
+ * @param $dataPatches
199
+ * @param $moduleVersionInDb
200
+ *
201
+ * @dataProvider applyDataPatchDataNewModuleProvider()
202
+ *
203
+ * @expectedException Exception
204
+ * @expectedExceptionMessageRegExp "Unable to apply data patch .+ cannot be applied twice"
205
+ */
206
+ public function testApplyDataPatchForAlias ($ moduleName , $ dataPatches , $ moduleVersionInDb )
207
+ {
208
+ $ this ->dataPatchReaderMock ->expects ($ this ->once ())
209
+ ->method ('read ' )
210
+ ->with ($ moduleName )
211
+ ->willReturn ($ dataPatches );
212
+
213
+ $ this ->moduleResourceMock ->expects ($ this ->any ())->method ('getDataVersion ' )->willReturnMap (
214
+ [
215
+ [$ moduleName , $ moduleVersionInDb ]
216
+ ]
217
+ );
218
+
219
+ $ patch1 = $ this ->createMock (DataPatchInterface::class);
220
+ $ patch1 ->expects ($ this ->once ())->method ('getAliases ' )->willReturn (['PatchAlias ' ]);
221
+ $ patchClass = get_class ($ patch1 );
222
+
223
+ $ patchRegistryMock = $ this ->createAggregateIteratorMock (PatchRegistry::class, [$ patchClass ], ['registerPatch ' ]);
224
+ $ patchRegistryMock ->expects ($ this ->any ())
225
+ ->method ('registerPatch ' );
226
+
227
+ $ this ->patchRegistryFactoryMock ->expects ($ this ->any ())
228
+ ->method ('create ' )
229
+ ->willReturn ($ patchRegistryMock );
230
+
231
+ $ this ->objectManagerMock ->expects ($ this ->any ())->method ('create ' )->willReturnMap (
232
+ [
233
+ ['\\' . $ patchClass , ['moduleDataSetup ' => $ this ->moduleDataSetupMock ], $ patch1 ],
234
+ ]
235
+ );
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 ' )->will (
239
+ $ this ->returnCallback (
240
+ function ($ param1 ) {
241
+ if ($ param1 == 'PatchAlias ' ) {
242
+ throw new \LogicException (sprintf ("Patch %s cannot be applied twice " , $ param1 ));
243
+ }
244
+ }
245
+ )
246
+ );
247
+ $ this ->patchApllier ->applyDataPatch ($ moduleName );
248
+ }
249
+
191
250
/**
192
251
* @return array
193
252
*/
@@ -243,8 +302,10 @@ public function testApplyDataPatchForInstalledModule($moduleName, $dataPatches,
243
302
244
303
$ patch1 = $ this ->createMock (\SomeDataPatch::class);
245
304
$ patch1 ->expects (self ::never ())->method ('apply ' );
305
+ $ patch1 ->expects (self ::any ())->method ('getAliases ' )->willReturn ([]);
246
306
$ patch2 = $ this ->createMock (\OtherDataPatch::class);
247
307
$ patch2 ->expects (self ::once ())->method ('apply ' );
308
+ $ patch2 ->expects (self ::any ())->method ('getAliases ' )->willReturn ([]);
248
309
$ this ->objectManagerMock ->expects (self ::any ())->method ('create ' )->willReturnMap (
249
310
[
250
311
['\\' . \SomeDataPatch::class, ['moduleDataSetup ' => $ this ->moduleDataSetupMock ], $ patch1 ],
@@ -279,7 +340,7 @@ public function applyDataPatchDataInstalledModuleProvider()
279
340
* @param $dataPatches
280
341
* @param $moduleVersionInDb
281
342
*
282
- * @expectedException \Magento\Framework\Setup\ Exception
343
+ * @expectedException Exception
283
344
* @expectedExceptionMessage Patch Apply Error
284
345
*
285
346
* @dataProvider applyDataPatchDataInstalledModuleProvider()
@@ -328,7 +389,7 @@ public function testApplyDataPatchRollback($moduleName, $dataPatches, $moduleVer
328
389
}
329
390
330
391
/**
331
- * @expectedException \Magento\Framework\Setup\ Exception
392
+ * @expectedException Exception
332
393
* @expectedExceptionMessageRegExp "Patch [a-zA-Z0-9\_]+ should implement DataPatchInterface"
333
394
*/
334
395
public function testNonDataPatchApply ()
@@ -434,8 +495,10 @@ public function testSchemaPatchAplly($moduleName, $schemaPatches, $moduleVersion
434
495
435
496
$ patch1 = $ this ->createMock (\SomeSchemaPatch::class);
436
497
$ patch1 ->expects ($ this ->never ())->method ('apply ' );
498
+ $ patch1 ->expects ($ this ->any ())->method ('getAliases ' )->willReturn ([]);
437
499
$ patch2 = $ this ->createMock (\OtherSchemaPatch::class);
438
500
$ patch2 ->expects ($ this ->once ())->method ('apply ' );
501
+ $ patch2 ->expects ($ this ->any ())->method ('getAliases ' )->willReturn ([]);
439
502
$ this ->patchFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturnMap (
440
503
[
441
504
[\SomeSchemaPatch::class, ['schemaSetup ' => $ this ->schemaSetupMock ], $ patch1 ],
@@ -448,6 +511,55 @@ public function testSchemaPatchAplly($moduleName, $schemaPatches, $moduleVersion
448
511
$ this ->patchApllier ->applySchemaPatch ($ moduleName );
449
512
}
450
513
514
+ /**
515
+ * @param $moduleName
516
+ * @param $schemaPatches
517
+ * @param $moduleVersionInDb
518
+ *
519
+ * @dataProvider schemaPatchDataProvider()
520
+ *
521
+ * @expectedException Exception
522
+ * @expectedExceptionMessageRegExp "Unable to apply patch .+ cannot be applied twice"
523
+ */
524
+ public function testSchemaPatchApplyForPatchAlias ($ moduleName , $ schemaPatches , $ moduleVersionInDb )
525
+ {
526
+ $ this ->schemaPatchReaderMock ->expects ($ this ->once ())
527
+ ->method ('read ' )
528
+ ->with ($ moduleName )
529
+ ->willReturn ($ schemaPatches );
530
+
531
+ $ this ->moduleResourceMock ->expects ($ this ->any ())->method ('getDbVersion ' )->willReturnMap (
532
+ [
533
+ [$ moduleName , $ moduleVersionInDb ]
534
+ ]
535
+ );
536
+
537
+ $ patch1 = $ this ->createMock (PatchInterface::class);
538
+ $ patch1 ->expects ($ this ->once ())->method ('getAliases ' )->willReturn (['PatchAlias ' ]);
539
+ $ patchClass = get_class ($ patch1 );
540
+
541
+ $ patchRegistryMock = $ this ->createAggregateIteratorMock (PatchRegistry::class, [$ patchClass ], ['registerPatch ' ]);
542
+ $ patchRegistryMock ->expects ($ this ->any ())
543
+ ->method ('registerPatch ' );
544
+
545
+ $ this ->patchRegistryFactoryMock ->expects ($ this ->any ())
546
+ ->method ('create ' )
547
+ ->willReturn ($ patchRegistryMock );
548
+
549
+ $ this ->patchFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ($ patch1 );
550
+ $ this ->patchHistoryMock ->expects ($ this ->any ())->method ('fixPatch ' )->will (
551
+ $ this ->returnCallback (
552
+ function ($ param1 ) {
553
+ if ($ param1 == 'PatchAlias ' ) {
554
+ throw new \LogicException (sprintf ("Patch %s cannot be applied twice " , $ param1 ));
555
+ }
556
+ }
557
+ )
558
+ );
559
+
560
+ $ this ->patchApllier ->applySchemaPatch ($ moduleName );
561
+ }
562
+
451
563
public function testRevertDataPatches ()
452
564
{
453
565
$ patches = [\RevertableDataPatch::class];
@@ -534,33 +646,43 @@ private function createAggregateIteratorMock($className, array $items = [], arra
534
646
535
647
$ someIterator ->expects ($ this ->any ())
536
648
->method ('rewind ' )
537
- ->willReturnCallback (function () use ($ iterator ) {
538
- $ iterator ->rewind ();
539
- });
649
+ ->willReturnCallback (
650
+ function () use ($ iterator ) {
651
+ $ iterator ->rewind ();
652
+ }
653
+ );
540
654
541
655
$ someIterator ->expects ($ this ->any ())
542
656
->method ('current ' )
543
- ->willReturnCallback (function () use ($ iterator ) {
544
- return $ iterator ->current ();
545
- });
657
+ ->willReturnCallback (
658
+ function () use ($ iterator ) {
659
+ return $ iterator ->current ();
660
+ }
661
+ );
546
662
547
663
$ someIterator ->expects ($ this ->any ())
548
664
->method ('key ' )
549
- ->willReturnCallback (function () use ($ iterator ) {
550
- return $ iterator ->key ();
551
- });
665
+ ->willReturnCallback (
666
+ function () use ($ iterator ) {
667
+ return $ iterator ->key ();
668
+ }
669
+ );
552
670
553
671
$ someIterator ->expects ($ this ->any ())
554
672
->method ('next ' )
555
- ->willReturnCallback (function () use ($ iterator ) {
556
- $ iterator ->next ();
557
- });
673
+ ->willReturnCallback (
674
+ function () use ($ iterator ) {
675
+ $ iterator ->next ();
676
+ }
677
+ );
558
678
559
679
$ someIterator ->expects ($ this ->any ())
560
680
->method ('valid ' )
561
- ->willReturnCallback (function () use ($ iterator ) {
562
- return $ iterator ->valid ();
563
- });
681
+ ->willReturnCallback (
682
+ function () use ($ iterator ) {
683
+ return $ iterator ->valid ();
684
+ }
685
+ );
564
686
565
687
return $ mockIteratorAggregate ;
566
688
}
0 commit comments