120 $this->productRepository = $this->getMockBuilder(\
Magento\Catalog\Model\ProductRepository::class)
121 ->setMethods([
'get'])
122 ->disableOriginalConstructor()
124 $this->productType = $this->getMockBuilder(\
Magento\Bundle\Model\
Product\
Type\Interceptor::class)
125 ->setMethods([
'getOptionsCollection',
'setStoreFilter',
'getSelectionsCollection',
'getOptionsIds'])
126 ->disableOriginalConstructor()
128 $this->option = $this->getMockBuilder(\
Magento\Bundle\Model\Option::class)
129 ->setMethods([
'getSelections',
'getOptionId',
'__wakeup'])
130 ->disableOriginalConstructor()
133 ->setMethods([
'appendSelections'])
134 ->disableOriginalConstructor()
136 $this->selectionCollection = $this->getMockBuilder(
138 )->disableOriginalConstructor()->getMock();
139 $this->product = $this->getMockBuilder(\
Magento\Catalog\Model\Product::class)
140 ->setMethods([
'getTypeInstance',
'getStoreId',
'getTypeId',
'__wakeup',
'getId',
'getData'])
141 ->disableOriginalConstructor()
143 $this->link = $this->getMockBuilder(\
Magento\Bundle\Api\Data\LinkInterface::class)
144 ->disableOriginalConstructor()
146 $this->linkFactory = $this->getMockBuilder(\
Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
147 ->setMethods([
'create'])
148 ->disableOriginalConstructor()
150 $this->bundleSelectionMock = $this->createPartialMock(
151 \
Magento\Bundle\Model\SelectionFactory::class,
154 $this->bundleFactoryMock = $this->createPartialMock(
158 $this->optionCollectionFactoryMock = $this->createPartialMock(
162 $this->storeManagerMock = $this->createMock(\
Magento\
Store\Model\StoreManagerInterface::class);
163 $this->metadataPoolMock = $this->getMockBuilder(\
Magento\Framework\EntityManager\MetadataPool::class)
164 ->disableOriginalConstructor()
166 $this->metadataMock = $this->getMockBuilder(\
Magento\Framework\EntityManager\EntityMetadata::class)
167 ->disableOriginalConstructor()
169 $this->metadataPoolMock->expects($this->any())->method(
'getMetadata')
170 ->with(\
Magento\Catalog\Api\Data\ProductInterface::class)
171 ->willReturn($this->metadataMock);
173 $this->dataObjectHelperMock = $this->getMockBuilder(\
Magento\Framework\Api\DataObjectHelper::class)
174 ->disableOriginalConstructor()
176 $this->model =
$helper->getObject(
178 LinkManagement::class,
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,
189 $refClass = new \ReflectionClass(LinkManagement::class);
190 $refProperty = $refClass->getProperty(
'metadataPool');
191 $refProperty->setAccessible(
true);
192 $refProperty->setValue($this->model, $this->metadataPoolMock);
197 $productSku =
'productSku';
201 $this->productRepository->expects($this->any())->method(
'get')->with($this->equalTo($productSku))
202 ->will($this->returnValue($this->product));
204 $this->product->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
'bundle'));
206 $this->productType->expects($this->once())->method(
'setStoreFilter')->with(
207 $this->equalTo($this->storeId),
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));
216 $this->optionCollection->expects($this->once())->method(
'appendSelections')
217 ->with($this->equalTo($this->selectionCollection))
218 ->will($this->returnValue([$this->option]));
220 $this->option->expects($this->any())->method(
'getSelections')->willReturn([$this->product]);
221 $this->product->expects($this->any())->method(
'getData')->willReturn([]);
223 $this->dataObjectHelperMock->expects($this->once())
224 ->method(
'populateWithArray')
225 ->with($this->link, $this->anything(), \
Magento\Bundle\Api\Data\LinkInterface::class)
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);
235 $this->assertEquals([$this->link], $this->model->getChildren($productSku));
240 $productSku =
'productSku';
244 $this->productRepository->expects($this->any())->method(
'get')->with($this->equalTo($productSku))
245 ->will($this->returnValue($this->product));
247 $this->product->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
'bundle'));
249 $this->productType->expects($this->once())->method(
'setStoreFilter')->with(
250 $this->equalTo($this->storeId),
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));
259 $this->optionCollection->expects($this->once())->method(
'appendSelections')
260 ->with($this->equalTo($this->selectionCollection))
261 ->will($this->returnValue([$this->option]));
263 $this->option->expects($this->any())->method(
'getOptionId')->will($this->returnValue(10));
264 $this->option->expects($this->once())->method(
'getSelections')->willReturn([1, 2]);
266 $this->dataObjectHelperMock->expects($this->never())->method(
'populateWithArray');
268 $this->assertEquals([], $this->model->getChildren($productSku, 1));
276 $productSku =
'productSku';
278 $this->productRepository->expects($this->once())->method(
'get')->with($this->equalTo($productSku))
279 ->will($this->returnValue($this->product));
281 $this->product->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
'simple'));
283 $this->assertEquals([$this->link], $this->model->getChildren($productSku));
292 $productLink->expects($this->any())->method(
'getOptionId')->will($this->returnValue(1));
294 $productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
295 $productMock->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
307 $productLink->expects($this->any())->method(
'getOptionId')->will($this->returnValue(1));
309 $productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
310 $productMock->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
315 $this->storeManagerMock->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
316 $store->expects($this->any())->method(
'getId')->will($this->returnValue(0));
318 $emptyOption = $this->getMockBuilder(\
Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
319 ->setMethods([
'getId',
'__wakeup'])
321 $emptyOption->expects($this->once())
323 ->will($this->returnValue(
null));
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));
334 $this->optionCollectionFactoryMock->expects($this->any())->method(
'create')->will(
335 $this->returnValue($optionsCollectionMock)
347 $productLink->expects($this->any())->method(
'getSku')->will($this->returnValue(
'linked_product_sku'));
348 $productLink->expects($this->any())->method(
'getOptionId')->will($this->returnValue(1));
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(
355 $productMock->expects($this->any())
357 ->with($this->linkField)
358 ->willReturn($this->linkField);
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())
366 ->with(
'linked_product_sku')
367 ->will($this->returnValue($linkedProductMock));
370 $this->storeManagerMock->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
371 $store->expects($this->any())->method(
'getId')->will($this->returnValue(0));
373 $option = $this->getMockBuilder(\
Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
374 ->setMethods([
'getId',
'__wakeup'])
376 $option->expects($this->once())->method(
'getId')->will($this->returnValue(1));
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)
391 $bundle->expects($this->once())->method(
'getSelectionsData')->with($this->linkField)->willReturn([]);
392 $this->bundleFactoryMock->expects($this->once())->method(
'create')->will($this->returnValue($bundle));
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));
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']
414 $productMock->expects($this->once())->method(
'getTypeId')->willReturn(
418 $productMock->expects($this->any())
420 ->with($this->linkField)
421 ->willReturn($this->linkField);
422 $productMock->expects($this->any())->method(
'getCopyFromView')->will($this->returnValue(
false));
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())
430 ->with(
'linked_product_sku')
431 ->will($this->returnValue($linkedProductMock));
434 $this->storeManagerMock->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
435 $store->expects($this->any())->method(
'getId')->will($this->returnValue(0));
437 $option = $this->getMockBuilder(\
Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
438 ->setMethods([
'getId',
'__wakeup'])
440 $option->expects($this->once())->method(
'getId')->will($this->returnValue(1));
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)
455 [
'option_id' => 1,
'product_id' => 12,
'parent_product_id' =>
'product_id'],
456 [
'option_id' => 1,
'product_id' => 13,
'parent_product_id' =>
'product_id'],
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));
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));
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(
484 $productMock->expects($this->any())
486 ->with($this->linkField)
487 ->willReturn($this->linkField);
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())
495 ->with(
'linked_product_sku')
496 ->will($this->returnValue($linkedProductMock));
499 $this->storeManagerMock->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
500 $store->expects($this->any())->method(
'getId')->will($this->returnValue(0));
502 $option = $this->getMockBuilder(\
Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
503 ->setMethods([
'getId',
'__wakeup'])
505 $option->expects($this->once())->method(
'getId')->will($this->returnValue(1));
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)
520 [
'option_id' => 1,
'product_id' => 11],
521 [
'option_id' => 1,
'product_id' => 12],
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));
529 $selection = $this->createPartialMock(\
Magento\Bundle\Model\Selection::class, [
'save']);
530 $selection->expects($this->once())->method(
'save')
532 $this->returnCallback(
534 throw new \Exception(
'message');
538 $this->bundleSelectionMock->expects($this->once())->method(
'create')->will($this->returnValue($selection));
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));
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(
557 $productMock->expects($this->any())
559 ->with($this->linkField)
560 ->willReturn($this->linkField);
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())
568 ->with(
'linked_product_sku')
569 ->will($this->returnValue($linkedProductMock));
572 $this->storeManagerMock->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
573 $store->expects($this->any())->method(
'getId')->will($this->returnValue(0));
575 $option = $this->getMockBuilder(\
Magento\Bundle\Model\Option::class)->disableOriginalConstructor()
576 ->setMethods([
'getId',
'__wakeup'])
578 $option->expects($this->once())->method(
'getId')->will($this->returnValue(1));
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)
593 [
'option_id' => 1,
'product_id' => 11],
594 [
'option_id' => 1,
'product_id' => 12],
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));
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));
607 $this->assertEquals(42,
$result);
618 $canChangeQuantity =
true;
621 $parentProductId = 32;
622 $bundleProductSku =
'bundleProductSku';
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));
631 $productLink->expects($this->any())->method(
'getPosition')->will($this->returnValue($position));
632 $productLink->expects($this->any())->method(
'getQty')->will($this->returnValue($qty));
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));
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(
645 $productMock->expects($this->any())
647 ->with($this->linkField)
648 ->willReturn($parentProductId);
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))
656 ->with($bundleProductSku)
657 ->will($this->returnValue($productMock));
658 $this->productRepository
659 ->expects($this->at(1))
661 ->with(
'linked_product_sku')
662 ->will($this->returnValue($linkedProductMock));
665 $this->storeManagerMock->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
666 $store->expects($this->any())->method(
'getId')->will($this->returnValue(0));
668 $selection = $this->createPartialMock(\
Magento\Bundle\Model\Selection::class, [
673 'setParentProductId',
677 'setSelectionPriceType',
678 'setSelectionPriceValue',
679 'setSelectionCanChangeQty',
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);
695 $this->bundleSelectionMock->expects($this->once())->method(
'create')->will($this->returnValue($selection));
696 $this->assertTrue($this->model->saveChild($bundleProductSku,
$productLink));
706 $parentProductId = 32;
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';
717 $productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
718 $productMock->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
721 $productMock->expects($this->any())->method(
'getId')->will($this->returnValue($parentProductId));
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))
729 ->with($bundleProductSku)
730 ->will($this->returnValue($productMock));
731 $this->productRepository
732 ->expects($this->at(1))
734 ->with(
'linked_product_sku')
735 ->will($this->returnValue($linkedProductMock));
738 $this->storeManagerMock->expects($this->any())->method(
'getStore')->will($this->returnValue(
$store));
739 $store->expects($this->any())->method(
'getId')->will($this->returnValue(0));
741 $selection = $this->createPartialMock(\
Magento\Bundle\Model\Selection::class, [
746 'setParentProductId',
751 'setSelectionPriceType',
752 'setSelectionPriceValue',
753 'setSelectionCanChangeQty',
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);
762 $this->bundleSelectionMock->expects($this->once())->method(
'create')->will($this->returnValue($selection));
763 $this->model->saveChild($bundleProductSku,
$productLink);
771 $bundleProductSku =
"bundleSku";
772 $linkedProductSku =
'simple';
774 $productLink->expects($this->any())->method(
'getId')->will($this->returnValue(
null));
775 $productLink->expects($this->any())->method(
'getSku')->will($this->returnValue($linkedProductSku));
777 $productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
778 $productMock->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
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))
787 ->with($bundleProductSku)
788 ->will($this->returnValue($productMock));
789 $this->productRepository
790 ->expects($this->at(1))
792 ->with($linkedProductSku)
793 ->will($this->returnValue($linkedProductMock));
795 $this->model->saveChild($bundleProductSku,
$productLink);
805 $linkedProductSku =
'simple';
806 $bundleProductSku =
"bundleProductSku";
808 $productLink->expects($this->any())->method(
'getId')->will($this->returnValue(
$id));
809 $productLink->expects($this->any())->method(
'getSku')->will($this->returnValue($linkedProductSku));
811 $productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
812 $productMock->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
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))
821 ->with($bundleProductSku)
822 ->will($this->returnValue($productMock));
823 $this->productRepository
824 ->expects($this->at(1))
826 ->with($linkedProductSku)
827 ->will($this->returnValue($linkedProductMock));
829 $selection = $this->createPartialMock(\
Magento\Bundle\Model\Selection::class, [
833 $selection->expects($this->once())->method(
'load')->with(
$id)->will($this->returnSelf());
834 $selection->expects($this->any())->method(
'getId')->will($this->returnValue(
null));
836 $this->bundleSelectionMock->expects($this->once())->method(
'create')->will($this->returnValue($selection));
838 $this->model->saveChild($bundleProductSku,
$productLink);
846 $bundleProductSku =
"bundleProductSku";
848 $linkedProductSku =
'simple';
850 $productLink->expects($this->any())->method(
'getId')->will($this->returnValue(
$id));
851 $productLink->expects($this->any())->method(
'getSku')->will($this->returnValue($linkedProductSku));
853 $productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
854 $productMock->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
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))
863 ->with($bundleProductSku)
864 ->will($this->returnValue($productMock));
865 $this->productRepository
866 ->expects($this->at(1))
868 ->with($linkedProductSku)
869 ->will($this->returnValue($linkedProductMock));
871 $this->model->saveChild($bundleProductSku,
$productLink);
880 $linkedProductSku =
'simple';
881 $bundleProductSku =
"bundleProductSku";
884 $productLink->expects($this->any())->method(
'getId')->will($this->returnValue(
$id));
885 $productLink->expects($this->any())->method(
'getSku')->will($this->returnValue($linkedProductSku));
887 $productMock = $this->createMock(\
Magento\Catalog\Model\Product::class);
888 $productMock->expects($this->once())->method(
'getTypeId')->will($this->returnValue(
892 $this->productRepository->expects($this->once())->method(
'get')->with($bundleProductSku)
893 ->willReturn($productMock);
895 $this->model->saveChild($bundleProductSku,
$productLink);
900 $this->productRepository->expects($this->any())->method(
'get')->will($this->returnValue($this->product));
902 $this->bundleFactoryMock->expects($this->once())->method(
'create')->will($this->returnValue($bundle));
903 $productSku =
'productSku';
906 $childSku =
'childSku';
909 ->expects($this->any())
910 ->method(
'getTypeId')
915 $selection = $this->getMockBuilder(\
Magento\Bundle\Model\Selection::class)
916 ->setMethods([
'getSku',
'getOptionId',
'getSelectionId',
'getProductId',
'__wakeup'])
917 ->disableOriginalConstructor()
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);
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())
928 ->with($this->linkField)
931 $bundle->expects($this->once())->method(
'dropAllUnneededSelections')->with(3, []);
932 $bundle->expects($this->once())->method(
'removeProductRelations')->with(3, [
$productId]);
934 $this->assertTrue($this->model->removeChild($productSku,
$optionId, $childSku));
942 $this->productRepository->expects($this->any())->method(
'get')->will($this->returnValue($this->product));
943 $productSku =
'productSku';
945 $childSku =
'childSku';
947 ->expects($this->any())
948 ->method(
'getTypeId')
950 $this->model->removeChild($productSku,
$optionId, $childSku);
958 $this->productRepository->expects($this->any())->method(
'get')->will($this->returnValue($this->product));
959 $productSku =
'productSku';
961 $childSku =
'childSku';
964 ->expects($this->any())
965 ->method(
'getTypeId')
970 $selection = $this->getMockBuilder(\
Magento\Bundle\Model\Selection::class)
971 ->setMethods([
'getSku',
'getOptionId',
'getSelectionId',
'getProductId',
'__wakeup'])
972 ->disableOriginalConstructor()
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));
979 $this->option->expects($this->any())->method(
'getSelections')->will($this->returnValue([$selection]));
980 $this->model->removeChild($productSku,
$optionId, $childSku);
988 $this->productRepository->expects($this->any())->method(
'get')->will($this->returnValue($this->product));
989 $productSku =
'productSku';
991 $childSku =
'childSku';
994 ->expects($this->any())
995 ->method(
'getTypeId')
1000 $selection = $this->getMockBuilder(\
Magento\Bundle\Model\Selection::class)
1001 ->setMethods([
'getSku',
'getOptionId',
'getSelectionId',
'getProductId',
'__wakeup'])
1002 ->disableOriginalConstructor()
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));
1009 $this->option->expects($this->any())->method(
'getSelections')->will($this->returnValue([$selection]));
1010 $this->model->removeChild($productSku,
$optionId, $childSku);
1013 private function getOptions()
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));
1020 $this->productType->expects($this->once())->method(
'getOptionsCollection')
1021 ->with($this->equalTo($this->product))
1022 ->will($this->returnValue($this->optionCollection));
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));
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));
1035 $this->productType->expects($this->once())->method(
'getOptionsIds')->with($this->equalTo($this->product))
1036 ->will($this->returnValue([1, 2, 3]));
1038 $this->productType->expects($this->once())->method(
'getSelectionsCollection')
1039 ->will($this->returnValue([]));
1041 $this->optionCollection->expects($this->any())->method(
'appendSelections')
1042 ->with($this->equalTo([]),
true)
1043 ->will($this->returnValue([$this->option]));
testRemoveChildInvalidOptionId()
testSaveChildWithInvalidId()
testAddChildCouldNotSave()
testGetChildrenWithOptionId()
testRemoveChildForbidden()
testSaveChildFailedToSave()
testGetChildrenException()
testAddChildToNotBundleProduct()
$optionCollectionFactoryMock
testSaveChildWithSimpleProduct()
testSaveChildWithCompositeProductLink()
testAddChildProductAlreadyExistsInOption()
testRemoveChildInvalidChildSku()
testAddChildLinkedProductIsComposite()
testAddChildNonExistingOption()