Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkManagementTest.php
Go to the documentation of this file.
1 <?php
9 
12 
19 class LinkManagementTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $model;
25 
29  protected $productRepository;
30 
34  protected $product;
35 
39  protected $linkFactory;
40 
44  protected $productType;
45 
49  protected $optionCollection;
50 
55 
59  protected $option;
60 
65 
69  protected $bundleFactoryMock;
70 
75 
79  protected $storeManagerMock;
80 
84  protected $link;
85 
89  protected $storeId = 2;
90 
94  protected $optionIds = [1, 2, 3];
95 
100 
104  protected $metadataPoolMock;
105 
109  protected $metadataMock;
110 
114  protected $linkField = 'product_id';
115 
116  protected function setUp()
117  {
118  $helper = new ObjectManager($this);
119 
120  $this->productRepository = $this->getMockBuilder(\Magento\Catalog\Model\ProductRepository::class)
121  ->setMethods(['get'])
122  ->disableOriginalConstructor()
123  ->getMock();
124  $this->productType = $this->getMockBuilder(\Magento\Bundle\Model\Product\Type\Interceptor::class)
125  ->setMethods(['getOptionsCollection', 'setStoreFilter', 'getSelectionsCollection', 'getOptionsIds'])
126  ->disableOriginalConstructor()
127  ->getMock();
128  $this->option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)
129  ->setMethods(['getSelections', 'getOptionId', '__wakeup'])
130  ->disableOriginalConstructor()
131  ->getMock();
132  $this->optionCollection = $this->getMockBuilder(\Magento\Bundle\Model\ResourceModel\Option\Collection::class)
133  ->setMethods(['appendSelections'])
134  ->disableOriginalConstructor()
135  ->getMock();
136  $this->selectionCollection = $this->getMockBuilder(
137  \Magento\Bundle\Model\ResourceModel\Selection\Collection::class
138  )->disableOriginalConstructor()->getMock();
139  $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
140  ->setMethods(['getTypeInstance', 'getStoreId', 'getTypeId', '__wakeup', 'getId', 'getData'])
141  ->disableOriginalConstructor()
142  ->getMock();
143  $this->link = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
144  ->disableOriginalConstructor()
145  ->getMock();
146  $this->linkFactory = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
147  ->setMethods(['create'])
148  ->disableOriginalConstructor()
149  ->getMock();
150  $this->bundleSelectionMock = $this->createPartialMock(
151  \Magento\Bundle\Model\SelectionFactory::class,
152  ['create']
153  );
154  $this->bundleFactoryMock = $this->createPartialMock(
155  \Magento\Bundle\Model\ResourceModel\BundleFactory::class,
156  ['create']
157  );
158  $this->optionCollectionFactoryMock = $this->createPartialMock(
159  \Magento\Bundle\Model\ResourceModel\Option\CollectionFactory::class,
160  ['create']
161  );
162  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
163  $this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
164  ->disableOriginalConstructor()
165  ->getMock();
166  $this->metadataMock = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadata::class)
167  ->disableOriginalConstructor()
168  ->getMock();
169  $this->metadataPoolMock->expects($this->any())->method('getMetadata')
170  ->with(\Magento\Catalog\Api\Data\ProductInterface::class)
171  ->willReturn($this->metadataMock);
172 
173  $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
174  ->disableOriginalConstructor()
175  ->getMock();
176  $this->model = $helper->getObject(
177 
178  LinkManagement::class,
179  [
180  'productRepository' => $this->productRepository,
181  'linkFactory' => $this->linkFactory,
182  'bundleFactory' => $this->bundleFactoryMock,
183  'bundleSelection' => $this->bundleSelectionMock,
184  'optionCollection' => $this->optionCollectionFactoryMock,
185  'storeManager' => $this->storeManagerMock,
186  'dataObjectHelper' => $this->dataObjectHelperMock,
187  ]
188  );
189  $refClass = new \ReflectionClass(LinkManagement::class);
190  $refProperty = $refClass->getProperty('metadataPool');
191  $refProperty->setAccessible(true);
192  $refProperty->setValue($this->model, $this->metadataPoolMock);
193  }
194 
195  public function testGetChildren()
196  {
197  $productSku = 'productSku';
198 
199  $this->getOptions();
200 
201  $this->productRepository->expects($this->any())->method('get')->with($this->equalTo($productSku))
202  ->will($this->returnValue($this->product));
203 
204  $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle'));
205 
206  $this->productType->expects($this->once())->method('setStoreFilter')->with(
207  $this->equalTo($this->storeId),
208  $this->product
209  );
210  $this->productType->expects($this->once())->method('getSelectionsCollection')
211  ->with($this->equalTo($this->optionIds), $this->equalTo($this->product))
212  ->will($this->returnValue($this->selectionCollection));
213  $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
214  ->will($this->returnValue($this->optionIds));
215 
216  $this->optionCollection->expects($this->once())->method('appendSelections')
217  ->with($this->equalTo($this->selectionCollection))
218  ->will($this->returnValue([$this->option]));
219 
220  $this->option->expects($this->any())->method('getSelections')->willReturn([$this->product]);
221  $this->product->expects($this->any())->method('getData')->willReturn([]);
222 
223  $this->dataObjectHelperMock->expects($this->once())
224  ->method('populateWithArray')
225  ->with($this->link, $this->anything(), \Magento\Bundle\Api\Data\LinkInterface::class)
226  ->willReturnSelf();
227  $this->link->expects($this->once())->method('setIsDefault')->willReturnSelf();
228  $this->link->expects($this->once())->method('setQty')->willReturnSelf();
229  $this->link->expects($this->once())->method('setCanChangeQuantity')->willReturnSelf();
230  $this->link->expects($this->once())->method('setPrice')->willReturnSelf();
231  $this->link->expects($this->once())->method('setPriceType')->willReturnSelf();
232  $this->link->expects($this->once())->method('setId')->willReturnSelf();
233  $this->linkFactory->expects($this->once())->method('create')->willReturn($this->link);
234 
235  $this->assertEquals([$this->link], $this->model->getChildren($productSku));
236  }
237 
238  public function testGetChildrenWithOptionId()
239  {
240  $productSku = 'productSku';
241 
242  $this->getOptions();
243 
244  $this->productRepository->expects($this->any())->method('get')->with($this->equalTo($productSku))
245  ->will($this->returnValue($this->product));
246 
247  $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle'));
248 
249  $this->productType->expects($this->once())->method('setStoreFilter')->with(
250  $this->equalTo($this->storeId),
251  $this->product
252  );
253  $this->productType->expects($this->once())->method('getSelectionsCollection')
254  ->with($this->equalTo($this->optionIds), $this->equalTo($this->product))
255  ->will($this->returnValue($this->selectionCollection));
256  $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
257  ->will($this->returnValue($this->optionIds));
258 
259  $this->optionCollection->expects($this->once())->method('appendSelections')
260  ->with($this->equalTo($this->selectionCollection))
261  ->will($this->returnValue([$this->option]));
262 
263  $this->option->expects($this->any())->method('getOptionId')->will($this->returnValue(10));
264  $this->option->expects($this->once())->method('getSelections')->willReturn([1, 2]);
265 
266  $this->dataObjectHelperMock->expects($this->never())->method('populateWithArray');
267 
268  $this->assertEquals([], $this->model->getChildren($productSku, 1));
269  }
270 
274  public function testGetChildrenException()
275  {
276  $productSku = 'productSku';
277 
278  $this->productRepository->expects($this->once())->method('get')->with($this->equalTo($productSku))
279  ->will($this->returnValue($this->product));
280 
281  $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('simple'));
282 
283  $this->assertEquals([$this->link], $this->model->getChildren($productSku));
284  }
285 
290  {
291  $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
292  $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
293 
294  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
295  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
296  \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
297  ));
298  $this->model->addChild($productMock, 1, $productLink);
299  }
300 
305  {
306  $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
307  $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
308 
309  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
310  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
311  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
312  ));
313 
314  $store = $this->createMock(\Magento\Store\Model\Store::class);
315  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
316  $store->expects($this->any())->method('getId')->will($this->returnValue(0));
317 
318  $emptyOption = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
319  ->setMethods(['getId', '__wakeup'])
320  ->getMock();
321  $emptyOption->expects($this->once())
322  ->method('getId')
323  ->will($this->returnValue(null));
324 
325  $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
326  $optionsCollectionMock->expects($this->once())
327  ->method('setIdFilter')
328  ->with($this->equalTo(1))
329  ->will($this->returnSelf());
330  $optionsCollectionMock->expects($this->once())
331  ->method('getFirstItem')
332  ->will($this->returnValue($emptyOption));
333 
334  $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
335  $this->returnValue($optionsCollectionMock)
336  );
337  $this->model->addChild($productMock, 1, $productLink);
338  }
339 
345  {
346  $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
347  $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
348  $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
349 
350  $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
351  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
352  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
353  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
354  ));
355  $productMock->expects($this->any())
356  ->method('getData')
357  ->with($this->linkField)
358  ->willReturn($this->linkField);
359 
360  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
361  $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
362  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(true));
363  $this->productRepository
364  ->expects($this->once())
365  ->method('get')
366  ->with('linked_product_sku')
367  ->will($this->returnValue($linkedProductMock));
368 
369  $store = $this->createMock(\Magento\Store\Model\Store::class);
370  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
371  $store->expects($this->any())->method('getId')->will($this->returnValue(0));
372 
373  $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
374  ->setMethods(['getId', '__wakeup'])
375  ->getMock();
376  $option->expects($this->once())->method('getId')->will($this->returnValue(1));
377 
378  $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
379  $optionsCollectionMock->expects($this->once())
380  ->method('setIdFilter')
381  ->with($this->equalTo('1'))
382  ->will($this->returnSelf());
383  $optionsCollectionMock->expects($this->once())
384  ->method('getFirstItem')
385  ->will($this->returnValue($option));
386  $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
387  $this->returnValue($optionsCollectionMock)
388  );
389 
390  $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
391  $bundle->expects($this->once())->method('getSelectionsData')->with($this->linkField)->willReturn([]);
392  $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
393  $this->model->addChild($productMock, 1, $productLink);
394  }
395 
400  {
401  $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
402  ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
403  ->disableOriginalConstructor()
404  ->getMockForAbstractClass();
405  $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
406  $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
407  $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
408 
409  $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
410  $productMock = $this->createPartialMock(
411  \Magento\Catalog\Model\Product::class,
412  ['getTypeId', 'getCopyFromView', 'getData', 'getTypeInstance', 'getSku']
413  );
414  $productMock->expects($this->once())->method('getTypeId')->willReturn(
415  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
416  );
417 
418  $productMock->expects($this->any())
419  ->method('getData')
420  ->with($this->linkField)
421  ->willReturn($this->linkField);
422  $productMock->expects($this->any())->method('getCopyFromView')->will($this->returnValue(false));
423 
424  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
425  $linkedProductMock->expects($this->any())->method('getEntityId')->will($this->returnValue(13));
426  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
427  $this->productRepository
428  ->expects($this->once())
429  ->method('get')
430  ->with('linked_product_sku')
431  ->will($this->returnValue($linkedProductMock));
432 
433  $store = $this->createMock(\Magento\Store\Model\Store::class);
434  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
435  $store->expects($this->any())->method('getId')->will($this->returnValue(0));
436 
437  $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
438  ->setMethods(['getId', '__wakeup'])
439  ->getMock();
440  $option->expects($this->once())->method('getId')->will($this->returnValue(1));
441 
442  $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
443  $optionsCollectionMock->expects($this->once())
444  ->method('setIdFilter')
445  ->with($this->equalTo(1))
446  ->will($this->returnSelf());
447  $optionsCollectionMock->expects($this->once())
448  ->method('getFirstItem')
449  ->will($this->returnValue($option));
450  $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
451  $this->returnValue($optionsCollectionMock)
452  );
453 
454  $selections = [
455  ['option_id' => 1, 'product_id' => 12, 'parent_product_id' => 'product_id'],
456  ['option_id' => 1, 'product_id' => 13, 'parent_product_id' => 'product_id'],
457  ];
458  $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
459  $bundle->expects($this->once())->method('getSelectionsData')
460  ->with($this->linkField)
461  ->will($this->returnValue($selections));
462  $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
463  $this->model->addChild($productMock, 1, $productLink);
464  }
465 
469  public function testAddChildCouldNotSave()
470  {
471  $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
472  ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
473  ->disableOriginalConstructor()
474  ->getMockForAbstractClass();
475  $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
476  $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
477  $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
478 
479  $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
480  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
481  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
482  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
483  ));
484  $productMock->expects($this->any())
485  ->method('getData')
486  ->with($this->linkField)
487  ->willReturn($this->linkField);
488 
489  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
490  $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
491  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
492  $this->productRepository
493  ->expects($this->once())
494  ->method('get')
495  ->with('linked_product_sku')
496  ->will($this->returnValue($linkedProductMock));
497 
498  $store = $this->createMock(\Magento\Store\Model\Store::class);
499  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
500  $store->expects($this->any())->method('getId')->will($this->returnValue(0));
501 
502  $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
503  ->setMethods(['getId', '__wakeup'])
504  ->getMock();
505  $option->expects($this->once())->method('getId')->will($this->returnValue(1));
506 
507  $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
508  $optionsCollectionMock->expects($this->once())
509  ->method('setIdFilter')
510  ->with($this->equalTo(1))
511  ->will($this->returnSelf());
512  $optionsCollectionMock->expects($this->once())
513  ->method('getFirstItem')
514  ->will($this->returnValue($option));
515  $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
516  $this->returnValue($optionsCollectionMock)
517  );
518 
519  $selections = [
520  ['option_id' => 1, 'product_id' => 11],
521  ['option_id' => 1, 'product_id' => 12],
522  ];
523  $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
524  $bundle->expects($this->once())->method('getSelectionsData')
525  ->with($this->linkField)
526  ->will($this->returnValue($selections));
527  $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
528 
529  $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, ['save']);
530  $selection->expects($this->once())->method('save')
531  ->will(
532  $this->returnCallback(
533  function () {
534  throw new \Exception('message');
535  }
536  )
537  );
538  $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
539  $this->model->addChild($productMock, 1, $productLink);
540  }
541 
542  public function testAddChild()
543  {
544  $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
545  ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
546  ->disableOriginalConstructor()
547  ->getMockForAbstractClass();
548  $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
549  $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue(1));
550  $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
551 
552  $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
553  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
554  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
555  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
556  ));
557  $productMock->expects($this->any())
558  ->method('getData')
559  ->with($this->linkField)
560  ->willReturn($this->linkField);
561 
562  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
563  $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue(13));
564  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
565  $this->productRepository
566  ->expects($this->once())
567  ->method('get')
568  ->with('linked_product_sku')
569  ->will($this->returnValue($linkedProductMock));
570 
571  $store = $this->createMock(\Magento\Store\Model\Store::class);
572  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
573  $store->expects($this->any())->method('getId')->will($this->returnValue(0));
574 
575  $option = $this->getMockBuilder(\Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
576  ->setMethods(['getId', '__wakeup'])
577  ->getMock();
578  $option->expects($this->once())->method('getId')->will($this->returnValue(1));
579 
580  $optionsCollectionMock = $this->createMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class);
581  $optionsCollectionMock->expects($this->once())
582  ->method('setIdFilter')
583  ->with($this->equalTo(1))
584  ->will($this->returnSelf());
585  $optionsCollectionMock->expects($this->once())
586  ->method('getFirstItem')
587  ->will($this->returnValue($option));
588  $this->optionCollectionFactoryMock->expects($this->any())->method('create')->will(
589  $this->returnValue($optionsCollectionMock)
590  );
591 
592  $selections = [
593  ['option_id' => 1, 'product_id' => 11],
594  ['option_id' => 1, 'product_id' => 12],
595  ];
596  $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
597  $bundle->expects($this->once())->method('getSelectionsData')
598  ->with($this->linkField)
599  ->will($this->returnValue($selections));
600  $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
601 
602  $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, ['save', 'getId']);
603  $selection->expects($this->once())->method('save');
604  $selection->expects($this->once())->method('getId')->will($this->returnValue(42));
605  $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
606  $result = $this->model->addChild($productMock, 1, $productLink);
607  $this->assertEquals(42, $result);
608  }
609 
610  public function testSaveChild()
611  {
612  $id = 12;
613  $optionId = 1;
614  $position = 3;
615  $qty = 2;
616  $priceType = 1;
617  $price = 10.5;
618  $canChangeQuantity = true;
619  $isDefault = true;
620  $linkProductId = 45;
621  $parentProductId = 32;
622  $bundleProductSku = 'bundleProductSku';
623 
624  $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
625  ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
626  ->disableOriginalConstructor()
627  ->getMockForAbstractClass();
628  $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
629  $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
630  $productLink->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
631  $productLink->expects($this->any())->method('getPosition')->will($this->returnValue($position));
632  $productLink->expects($this->any())->method('getQty')->will($this->returnValue($qty));
633  $productLink->expects($this->any())->method('getPriceType')->will($this->returnValue($priceType));
634  $productLink->expects($this->any())->method('getPrice')->will($this->returnValue($price));
635  $productLink->expects($this->any())->method('getCanChangeQuantity')
636  ->will($this->returnValue($canChangeQuantity));
637  $productLink->expects($this->any())->method('getIsDefault')->will($this->returnValue($isDefault));
638  $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue($optionId));
639 
640  $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn($this->linkField);
641  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
642  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
643  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
644  ));
645  $productMock->expects($this->any())
646  ->method('getData')
647  ->with($this->linkField)
648  ->willReturn($parentProductId);
649 
650  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
651  $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
652  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
653  $this->productRepository
654  ->expects($this->at(0))
655  ->method('get')
656  ->with($bundleProductSku)
657  ->will($this->returnValue($productMock));
658  $this->productRepository
659  ->expects($this->at(1))
660  ->method('get')
661  ->with('linked_product_sku')
662  ->will($this->returnValue($linkedProductMock));
663 
664  $store = $this->createMock(\Magento\Store\Model\Store::class);
665  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
666  $store->expects($this->any())->method('getId')->will($this->returnValue(0));
667 
668  $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
669  'save',
670  'getId',
671  'load',
672  'setProductId',
673  'setParentProductId',
674  'setOptionId',
675  'setPosition',
676  'setSelectionQty',
677  'setSelectionPriceType',
678  'setSelectionPriceValue',
679  'setSelectionCanChangeQty',
680  'setIsDefault'
681  ]);
682  $selection->expects($this->once())->method('save');
683  $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
684  $selection->expects($this->any())->method('getId')->will($this->returnValue($id));
685  $selection->expects($this->once())->method('setProductId')->with($linkProductId);
686  $selection->expects($this->once())->method('setParentProductId')->with($parentProductId);
687  $selection->expects($this->once())->method('setOptionId')->with($optionId);
688  $selection->expects($this->once())->method('setPosition')->with($position);
689  $selection->expects($this->once())->method('setSelectionQty')->with($qty);
690  $selection->expects($this->once())->method('setSelectionPriceType')->with($priceType);
691  $selection->expects($this->once())->method('setSelectionPriceValue')->with($price);
692  $selection->expects($this->once())->method('setSelectionCanChangeQty')->with($canChangeQuantity);
693  $selection->expects($this->once())->method('setIsDefault')->with($isDefault);
694 
695  $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
696  $this->assertTrue($this->model->saveChild($bundleProductSku, $productLink));
697  }
698 
702  public function testSaveChildFailedToSave()
703  {
704  $id = 12;
705  $linkProductId = 45;
706  $parentProductId = 32;
707 
708  $productLink = $this->getMockBuilder(\Magento\Bundle\Api\Data\LinkInterface::class)
709  ->setMethods(['getSku', 'getOptionId', 'getSelectionId'])
710  ->disableOriginalConstructor()
711  ->getMockForAbstractClass();
712  $productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
713  $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
714  $productLink->expects($this->any())->method('getSelectionId')->will($this->returnValue(1));
715  $bundleProductSku = 'bundleProductSku';
716 
717  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
718  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
719  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
720  ));
721  $productMock->expects($this->any())->method('getId')->will($this->returnValue($parentProductId));
722 
723  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
724  $linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
725  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
726  $this->productRepository
727  ->expects($this->at(0))
728  ->method('get')
729  ->with($bundleProductSku)
730  ->will($this->returnValue($productMock));
731  $this->productRepository
732  ->expects($this->at(1))
733  ->method('get')
734  ->with('linked_product_sku')
735  ->will($this->returnValue($linkedProductMock));
736 
737  $store = $this->createMock(\Magento\Store\Model\Store::class);
738  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
739  $store->expects($this->any())->method('getId')->will($this->returnValue(0));
740 
741  $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
742  'save',
743  'getId',
744  'load',
745  'setProductId',
746  'setParentProductId',
747  'setSelectionId',
748  'setOptionId',
749  'setPosition',
750  'setSelectionQty',
751  'setSelectionPriceType',
752  'setSelectionPriceValue',
753  'setSelectionCanChangeQty',
754  'setIsDefault'
755  ]);
756  $mockException = $this->createMock(\Exception::class);
757  $selection->expects($this->once())->method('save')->will($this->throwException($mockException));
758  $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
759  $selection->expects($this->any())->method('getId')->will($this->returnValue($id));
760  $selection->expects($this->once())->method('setProductId')->with($linkProductId);
761 
762  $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
763  $this->model->saveChild($bundleProductSku, $productLink);
764  }
765 
769  public function testSaveChildWithoutId()
770  {
771  $bundleProductSku = "bundleSku";
772  $linkedProductSku = 'simple';
773  $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
774  $productLink->expects($this->any())->method('getId')->will($this->returnValue(null));
775  $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
776 
777  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
778  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
779  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
780  ));
781 
782  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
783  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
784  $this->productRepository
785  ->expects($this->at(0))
786  ->method('get')
787  ->with($bundleProductSku)
788  ->will($this->returnValue($productMock));
789  $this->productRepository
790  ->expects($this->at(1))
791  ->method('get')
792  ->with($linkedProductSku)
793  ->will($this->returnValue($linkedProductMock));
794 
795  $this->model->saveChild($bundleProductSku, $productLink);
796  }
797 
802  public function testSaveChildWithInvalidId()
803  {
804  $id = 12345;
805  $linkedProductSku = 'simple';
806  $bundleProductSku = "bundleProductSku";
807  $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
808  $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
809  $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
810 
811  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
812  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
813  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
814  ));
815 
816  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
817  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
818  $this->productRepository
819  ->expects($this->at(0))
820  ->method('get')
821  ->with($bundleProductSku)
822  ->will($this->returnValue($productMock));
823  $this->productRepository
824  ->expects($this->at(1))
825  ->method('get')
826  ->with($linkedProductSku)
827  ->will($this->returnValue($linkedProductMock));
828 
829  $selection = $this->createPartialMock(\Magento\Bundle\Model\Selection::class, [
830  'getId',
831  'load',
832  ]);
833  $selection->expects($this->once())->method('load')->with($id)->will($this->returnSelf());
834  $selection->expects($this->any())->method('getId')->will($this->returnValue(null));
835 
836  $this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
837 
838  $this->model->saveChild($bundleProductSku, $productLink);
839  }
840 
845  {
846  $bundleProductSku = "bundleProductSku";
847  $id = 12;
848  $linkedProductSku = 'simple';
849  $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
850  $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
851  $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
852 
853  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
854  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
855  \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE
856  ));
857 
858  $linkedProductMock = $this->createMock(\Magento\Catalog\Model\Product::class);
859  $linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(true));
860  $this->productRepository
861  ->expects($this->at(0))
862  ->method('get')
863  ->with($bundleProductSku)
864  ->will($this->returnValue($productMock));
865  $this->productRepository
866  ->expects($this->at(1))
867  ->method('get')
868  ->with($linkedProductSku)
869  ->will($this->returnValue($linkedProductMock));
870 
871  $this->model->saveChild($bundleProductSku, $productLink);
872  }
873 
878  {
879  $id = 12;
880  $linkedProductSku = 'simple';
881  $bundleProductSku = "bundleProductSku";
882 
883  $productLink = $this->createMock(\Magento\Bundle\Api\Data\LinkInterface::class);
884  $productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
885  $productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
886 
887  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
888  $productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
889  \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
890  ));
891 
892  $this->productRepository->expects($this->once())->method('get')->with($bundleProductSku)
893  ->willReturn($productMock);
894 
895  $this->model->saveChild($bundleProductSku, $productLink);
896  }
897 
898  public function testRemoveChild()
899  {
900  $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
901  $bundle = $this->createMock(\Magento\Bundle\Model\ResourceModel\Bundle::class);
902  $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle));
903  $productSku = 'productSku';
904  $optionId = 1;
905  $productId = 1;
906  $childSku = 'childSku';
907 
908  $this->product
909  ->expects($this->any())
910  ->method('getTypeId')
911  ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
912 
913  $this->getRemoveOptions();
914 
915  $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
916  ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
917  ->disableOriginalConstructor()
918  ->getMock();
919  $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku));
920  $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
921  $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
922  $selection->expects($this->any())->method('getProductId')->willReturn($productId);
923 
924  $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
925  $this->metadataMock->expects($this->any())->method('getLinkField')->willReturn($this->linkField);
926  $this->product->expects($this->any())
927  ->method('getData')
928  ->with($this->linkField)
929  ->willReturn(3);
930 
931  $bundle->expects($this->once())->method('dropAllUnneededSelections')->with(3, []);
932  $bundle->expects($this->once())->method('removeProductRelations')->with(3, [$productId]);
933  //Params come in lowercase to method
934  $this->assertTrue($this->model->removeChild($productSku, $optionId, $childSku));
935  }
936 
940  public function testRemoveChildForbidden()
941  {
942  $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
943  $productSku = 'productSku';
944  $optionId = 1;
945  $childSku = 'childSku';
946  $this->product
947  ->expects($this->any())
948  ->method('getTypeId')
949  ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE));
950  $this->model->removeChild($productSku, $optionId, $childSku);
951  }
952 
957  {
958  $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
959  $productSku = 'productSku';
960  $optionId = 1;
961  $childSku = 'childSku';
962 
963  $this->product
964  ->expects($this->any())
965  ->method('getTypeId')
966  ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
967 
968  $this->getRemoveOptions();
969 
970  $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
971  ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
972  ->disableOriginalConstructor()
973  ->getMock();
974  $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku));
975  $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId + 1));
976  $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
977  $selection->expects($this->any())->method('getProductId')->will($this->returnValue(1));
978 
979  $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
980  $this->model->removeChild($productSku, $optionId, $childSku);
981  }
982 
987  {
988  $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product));
989  $productSku = 'productSku';
990  $optionId = 1;
991  $childSku = 'childSku';
992 
993  $this->product
994  ->expects($this->any())
995  ->method('getTypeId')
996  ->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE));
997 
998  $this->getRemoveOptions();
999 
1000  $selection = $this->getMockBuilder(\Magento\Bundle\Model\Selection::class)
1001  ->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])
1002  ->disableOriginalConstructor()
1003  ->getMock();
1004  $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku . '_invalid'));
1005  $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId));
1006  $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55));
1007  $selection->expects($this->any())->method('getProductId')->will($this->returnValue(1));
1008 
1009  $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection]));
1010  $this->model->removeChild($productSku, $optionId, $childSku);
1011  }
1012 
1013  private function getOptions()
1014  {
1015  $this->product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->productType));
1016  $this->product->expects($this->once())->method('getStoreId')->will($this->returnValue($this->storeId));
1017  $this->productType->expects($this->once())->method('setStoreFilter')
1018  ->with($this->equalTo($this->storeId), $this->equalTo($this->product));
1019 
1020  $this->productType->expects($this->once())->method('getOptionsCollection')
1021  ->with($this->equalTo($this->product))
1022  ->will($this->returnValue($this->optionCollection));
1023  }
1024 
1025  public function getRemoveOptions()
1026  {
1027  $this->product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->productType));
1028  $this->product->expects($this->once())->method('getStoreId')->will($this->returnValue(1));
1029 
1030  $this->productType->expects($this->once())->method('setStoreFilter');
1031  $this->productType->expects($this->once())->method('getOptionsCollection')
1032  ->with($this->equalTo($this->product))
1033  ->will($this->returnValue($this->optionCollection));
1034 
1035  $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))
1036  ->will($this->returnValue([1, 2, 3]));
1037 
1038  $this->productType->expects($this->once())->method('getSelectionsCollection')
1039  ->will($this->returnValue([]));
1040 
1041  $this->optionCollection->expects($this->any())->method('appendSelections')
1042  ->with($this->equalTo([]), true)
1043  ->will($this->returnValue([$this->option]));
1044  }
1045 }
$helper
Definition: iframe.phtml:13
$id
Definition: fieldset.phtml:14
$price
$priceType
Definition: msrp.phtml:18