19
19
use Magento \Catalog \Api \Data \ProductCustomOptionInterfaceFactory ;
20
20
use Magento \Catalog \Api \Data \ProductCustomOptionInterface ;
21
21
use Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks ;
22
- use Magento \Catalog \Model \Product \LinkTypeProvider ;
23
- use Magento \Catalog \Api \Data \ProductLinkTypeInterface ;
24
- use Magento \Catalog \Model \ProductLink \Link as ProductLink ;
25
22
26
23
/**
27
24
* Class HelperTest
@@ -107,11 +104,6 @@ class HelperTest extends \PHPUnit_Framework_TestCase
107
104
*/
108
105
protected $ linkResolverMock ;
109
106
110
- /**
111
- * @var \Magento\Catalog\Model\Product\LinkTypeProvider|\PHPUnit_Framework_MockObject_MockObject
112
- */
113
- protected $ linkTypeProviderMock ;
114
-
115
107
/**
116
108
* @var ProductLinks|\PHPUnit_Framework_MockObject_MockObject
117
109
*/
@@ -121,7 +113,6 @@ protected function setUp()
121
113
{
122
114
$ this ->objectManager = new ObjectManager ($ this );
123
115
$ this ->productLinkFactoryMock = $ this ->getMockBuilder (ProductLinkInterfaceFactory::class)
124
- ->setMethods (['create ' ])
125
116
->disableOriginalConstructor ()
126
117
->getMock ();
127
118
$ this ->productRepositoryMock = $ this ->getMockBuilder (ProductRepository::class)
@@ -159,6 +150,7 @@ protected function setUp()
159
150
'__sleep ' ,
160
151
'__wakeup ' ,
161
152
'getSku ' ,
153
+ 'getProductLinks ' ,
162
154
'getWebsiteIds '
163
155
])
164
156
->disableOriginalConstructor ()
@@ -176,9 +168,6 @@ protected function setUp()
176
168
$ this ->productLinksMock ->expects ($ this ->any ())
177
169
->method ('initializeLinks ' )
178
170
->willReturn ($ this ->productMock );
179
- $ this ->linkTypeProviderMock = $ this ->getMockBuilder (LinkTypeProvider::class)
180
- ->disableOriginalConstructor ()
181
- ->getMock ();
182
171
183
172
$ this ->helper = $ this ->objectManager ->getObject (Helper::class, [
184
173
'request ' => $ this ->requestMock ,
@@ -189,7 +178,6 @@ protected function setUp()
189
178
'customOptionFactory ' => $ this ->customOptionFactoryMock ,
190
179
'productLinkFactory ' => $ this ->productLinkFactoryMock ,
191
180
'productRepository ' => $ this ->productRepositoryMock ,
192
- 'linkTypeProvider ' => $ this ->linkTypeProviderMock ,
193
181
]);
194
182
195
183
$ this ->linkResolverMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Link \Resolver::class)
@@ -202,10 +190,10 @@ protected function setUp()
202
190
}
203
191
204
192
/**
193
+ * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize
205
194
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
206
- * @param array $links
207
195
*/
208
- private function assembleProductMock ( $ links = [] )
196
+ public function testInitialize ( )
209
197
{
210
198
$ this ->customOptionMock ->expects ($ this ->once ())
211
199
->method ('setProductSku ' );
@@ -241,6 +229,9 @@ private function assembleProductMock($links = [])
241
229
$ attributeDate ->expects ($ this ->any ())
242
230
->method ('getBackend ' )
243
231
->willReturn ($ attributeDateBackEnd );
232
+ $ this ->productMock ->expects ($ this ->any ())
233
+ ->method ('getProductLinks ' )
234
+ ->willReturn ([]);
244
235
$ attributeNonDateBackEnd ->expects ($ this ->any ())
245
236
->method ('getType ' )
246
237
->willReturn ('non-datetime ' );
@@ -263,7 +254,7 @@ private function assembleProductMock($links = [])
263
254
->method ('getPost ' )
264
255
->with ('use_default ' )
265
256
->willReturn ($ useDefaults );
266
- $ this ->linkResolverMock ->expects ($ this ->once ())->method ('getLinks ' )->willReturn ($ links );
257
+ $ this ->linkResolverMock ->expects ($ this ->once ())->method ('getLinks ' )->willReturn ([] );
267
258
$ this ->stockFilterMock ->expects ($ this ->once ())
268
259
->method ('filter ' )
269
260
->with (['stock_data ' ])
@@ -275,6 +266,9 @@ private function assembleProductMock($links = [])
275
266
$ this ->productMock ->expects ($ this ->once ())
276
267
->method ('unlockAttribute ' )
277
268
->with ('media ' );
269
+ $ this ->productMock ->expects ($ this ->any ())
270
+ ->method ('getProductLinks ' )
271
+ ->willReturn ([]);
278
272
$ this ->productMock ->expects ($ this ->once ())
279
273
->method ('lockAttribute ' )
280
274
->with ('media ' );
@@ -289,7 +283,7 @@ private function assembleProductMock($links = [])
289
283
$ this ->productMock ->expects ($ this ->once ())
290
284
->method ('addData ' )
291
285
->with ($ productData );
292
- $ this ->productMock ->expects ($ this ->any ())
286
+ $ this ->productMock ->expects ($ this ->once ())
293
287
->method ('getSku ' )
294
288
->willReturn ('sku ' );
295
289
$ this ->productMock ->expects ($ this ->any ())
@@ -303,193 +297,10 @@ private function assembleProductMock($links = [])
303
297
$ this ->productMock ->expects ($ this ->once ())
304
298
->method ('setOptions ' )
305
299
->with ([$ this ->customOptionMock ]);
306
- }
307
-
308
- /**
309
- * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize
310
- */
311
- public function testInitialize ()
312
- {
313
- $ this ->assembleProductMock ();
314
- $ this ->linkTypeProviderMock ->expects ($ this ->once ())
315
- ->method ('getItems ' )
316
- ->willReturn ($ this ->assembleLinkTypes (['related ' , 'upsell ' , 'crosssell ' ]));
317
300
318
301
$ this ->assertEquals ($ this ->productMock , $ this ->helper ->initialize ($ this ->productMock ));
319
302
}
320
303
321
- /**
322
- * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize
323
- * @dataProvider initializeWithLinksDataProvider
324
- */
325
- public function testInitializeWithLinks ($ links , $ linkTypes , $ expectedLinks )
326
- {
327
- $ this ->productLinkFactoryMock ->expects ($ this ->any ())
328
- ->method ('create ' )
329
- ->willReturnCallback (function () {
330
- return $ this ->getMockBuilder (ProductLink::class)
331
- ->setMethods (null )
332
- ->disableOriginalConstructor ()
333
- ->getMock ();
334
- });
335
-
336
- $ this ->linkTypeProviderMock ->expects ($ this ->once ())
337
- ->method ('getItems ' )
338
- ->willReturn ($ this ->assembleLinkTypes ($ linkTypes ));
339
-
340
- $ this ->assembleProductRepositoryMock ($ links );
341
- $ this ->assembleProductMock ($ links );
342
-
343
- $ this ->assertEquals ($ this ->productMock , $ this ->helper ->initialize ($ this ->productMock ));
344
-
345
- $ productLinks = $ this ->productMock ->getProductLinks ();
346
- $ this ->assertCount (count ($ expectedLinks ), $ productLinks );
347
- $ resultLinks = [];
348
-
349
- foreach ($ productLinks as $ link ) {
350
- $ this ->assertInstanceOf (ProductLink::class, $ link );
351
- $ this ->assertEquals ('sku ' , $ link ->getSku ());
352
- $ resultLinks [] = ['type ' => $ link ->getLinkType (), 'linked_product_sku ' => $ link ->getLinkedProductSku ()];
353
- }
354
-
355
- $ this ->assertEquals ($ expectedLinks , $ resultLinks );
356
- }
357
-
358
- /**
359
- * @return array
360
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
361
- */
362
- public function initializeWithLinksDataProvider ()
363
- {
364
- return [
365
- // No links
366
- [
367
- 'links ' => [],
368
- 'linkTypes ' => ['related ' , 'upsell ' , 'crosssell ' ],
369
- 'expected_links ' => [],
370
- ],
371
-
372
- // Related links
373
- [
374
- 'links ' => [
375
- 'related ' => [
376
- 0 => [
377
- 'id ' => 1 ,
378
- 'thumbnail ' => 'http://magento.dev/media/no-image.jpg ' ,
379
- 'name ' => 'Test ' ,
380
- 'status ' => 'Enabled ' ,
381
- 'attribute_set ' => 'Default ' ,
382
- 'sku ' => 'Test ' ,
383
- 'price ' => 1.00 ,
384
- 'position ' => 1 ,
385
- 'record_id ' => 1 ,
386
- ]
387
- ]
388
- ],
389
- 'linkTypes ' => ['related ' , 'upsell ' , 'crosssell ' ],
390
- 'expected_links ' => [
391
- ['type ' => 'related ' , 'linked_product_sku ' => 'Test ' ],
392
- ],
393
- ],
394
-
395
- // Custom link
396
- [
397
- 'links ' => [
398
- 'customlink ' => [
399
- 0 => [
400
- 'id ' => 4 ,
401
- 'thumbnail ' => 'http://magento.dev/media/no-image.jpg ' ,
402
- 'name ' => 'Test Custom ' ,
403
- 'status ' => 'Enabled ' ,
404
- 'attribute_set ' => 'Default ' ,
405
- 'sku ' => 'Testcustom ' ,
406
- 'price ' => 1.00 ,
407
- 'position ' => 1 ,
408
- 'record_id ' => 1 ,
409
- ],
410
- ],
411
- ],
412
- 'linkTypes ' => ['related ' , 'upsell ' , 'crosssell ' , 'customlink ' ],
413
- 'expected_links ' => [
414
- ['type ' => 'customlink ' , 'linked_product_sku ' => 'Testcustom ' ],
415
- ],
416
- ],
417
-
418
- // Both links
419
- [
420
- 'links ' => [
421
- 'related ' => [
422
- 0 => [
423
- 'id ' => 1 ,
424
- 'thumbnail ' => 'http://magento.dev/media/no-image.jpg ' ,
425
- 'name ' => 'Test ' ,
426
- 'status ' => 'Enabled ' ,
427
- 'attribute_set ' => 'Default ' ,
428
- 'sku ' => 'Test ' ,
429
- 'price ' => 1.00 ,
430
- 'position ' => 1 ,
431
- 'record_id ' => 1 ,
432
- ],
433
- ],
434
- 'customlink ' => [
435
- 0 => [
436
- 'id ' => 4 ,
437
- 'thumbnail ' => 'http://magento.dev/media/no-image.jpg ' ,
438
- 'name ' => 'Test Custom ' ,
439
- 'status ' => 'Enabled ' ,
440
- 'attribute_set ' => 'Default ' ,
441
- 'sku ' => 'Testcustom ' ,
442
- 'price ' => 2.00 ,
443
- 'position ' => 2 ,
444
- 'record_id ' => 1 ,
445
- ],
446
- ],
447
- ],
448
- 'linkTypes ' => ['related ' , 'upsell ' , 'crosssell ' , 'customlink ' ],
449
- 'expected_links ' => [
450
- ['type ' => 'related ' , 'linked_product_sku ' => 'Test ' ],
451
- ['type ' => 'customlink ' , 'linked_product_sku ' => 'Testcustom ' ],
452
- ],
453
- ],
454
-
455
- // Undefined link type
456
- [
457
- 'links ' => [
458
- 'related ' => [
459
- 0 => [
460
- 'id ' => 1 ,
461
- 'thumbnail ' => 'http://magento.dev/media/no-image.jpg ' ,
462
- 'name ' => 'Test ' ,
463
- 'status ' => 'Enabled ' ,
464
- 'attribute_set ' => 'Default ' ,
465
- 'sku ' => 'Test ' ,
466
- 'price ' => 1.00 ,
467
- 'position ' => 1 ,
468
- 'record_id ' => 1 ,
469
- ],
470
- ],
471
- 'customlink ' => [
472
- 0 => [
473
- 'id ' => 4 ,
474
- 'thumbnail ' => 'http://magento.dev/media/no-image.jpg ' ,
475
- 'name ' => 'Test Custom ' ,
476
- 'status ' => 'Enabled ' ,
477
- 'attribute_set ' => 'Default ' ,
478
- 'sku ' => 'Testcustom ' ,
479
- 'price ' => 2.00 ,
480
- 'position ' => 2 ,
481
- 'record_id ' => 1 ,
482
- ],
483
- ],
484
- ],
485
- 'linkTypes ' => ['related ' , 'upsell ' , 'crosssell ' ],
486
- 'expected_links ' => [
487
- ['type ' => 'related ' , 'linked_product_sku ' => 'Test ' ],
488
- ],
489
- ],
490
- ];
491
- }
492
-
493
304
/**
494
305
* Data provider for testMergeProductOptions
495
306
*
@@ -579,55 +390,4 @@ public function testMergeProductOptions($productOptions, $defaultOptions, $expec
579
390
$ result = $ this ->helper ->mergeProductOptions ($ productOptions , $ defaultOptions );
580
391
$ this ->assertEquals ($ expectedResults , $ result );
581
392
}
582
-
583
- /**
584
- * @param array $types
585
- * @return array
586
- */
587
- private function assembleLinkTypes ($ types )
588
- {
589
- $ linkTypes = [];
590
- $ linkTypeCode = 1 ;
591
-
592
- foreach ($ types as $ typeName ) {
593
- $ linkType = $ this ->getMock (ProductLinkTypeInterface::class);
594
- $ linkType ->method ('getCode ' )->willReturn ($ linkTypeCode ++);
595
- $ linkType ->method ('getName ' )->willReturn ($ typeName );
596
-
597
- $ linkTypes [] = $ linkType ;
598
- }
599
-
600
- return $ linkTypes ;
601
- }
602
-
603
- /**
604
- * @param array $links
605
- */
606
- private function assembleProductRepositoryMock ($ links )
607
- {
608
- $ repositoryReturnMap = [];
609
-
610
- foreach ($ links as $ linkType ) {
611
- foreach ($ linkType as $ link ) {
612
- $ mockLinkedProduct = $ this ->getMockBuilder (Product::class)
613
- ->disableOriginalConstructor ()
614
- ->getMock ();
615
-
616
- $ mockLinkedProduct ->expects ($ this ->any ())
617
- ->method ('getId ' )
618
- ->willReturn ($ link ['id ' ]);
619
-
620
- $ mockLinkedProduct ->expects ($ this ->any ())
621
- ->method ('getSku ' )
622
- ->willReturn ($ link ['sku ' ]);
623
-
624
- // Even optional arguments need to be provided for returnMapValue
625
- $ repositoryReturnMap [] = [$ link ['id ' ], false , null , false , $ mockLinkedProduct ];
626
- }
627
- }
628
-
629
- $ this ->productRepositoryMock ->expects ($ this ->any ())
630
- ->method ('getById ' )
631
- ->will ($ this ->returnValueMap ($ repositoryReturnMap ));
632
- }
633
393
}
0 commit comments