Skip to content

Commit 0383cd8

Browse files
author
Yu Tang
committed
MAGETWO-28256: Bundle Integration API Refactoring
- Addressed CR comments
1 parent 3b4615f commit 0383cd8

File tree

13 files changed

+266
-62
lines changed

13 files changed

+266
-62
lines changed

app/code/Magento/Bundle/Api/ProductLinkManagementInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ public function getChildren($productSku, $optionId = null);
3333
public function addChildByProductSku($sku, $optionId, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct);
3434

3535
/**
36-
* @param \Magento\Catalog\Api\Data\ProductInterface $product
37-
* @param Data\LinkInterface $linkedProduct
36+
* @param string $sku
37+
* @param \Magento\Bundle\Api\Data\LinkInterface $linkedProduct
3838
* @throws \Magento\Framework\Exception\NoSuchEntityException
3939
* @throws \Magento\Framework\Exception\CouldNotSaveException
4040
* @throws \Magento\Framework\Exception\InputException
41-
* @return int
41+
* @return bool
4242
*/
4343
public function saveChild(
44-
\Magento\Catalog\Api\Data\ProductInterface $product,
44+
$sku,
4545
\Magento\Bundle\Api\Data\LinkInterface $linkedProduct
4646
);
4747

4848
/**
4949
* @param \Magento\Catalog\Api\Data\ProductInterface $product
5050
* @param int $optionId
51-
* @param Data\LinkInterface $linkedProduct
51+
* @param \Magento\Bundle\Api\Data\LinkInterface $linkedProduct
5252
* @throws \Magento\Framework\Exception\NoSuchEntityException
5353
* @throws \Magento\Framework\Exception\CouldNotSaveException
5454
* @throws \Magento\Framework\Exception\InputException

app/code/Magento/Bundle/Model/LinkManagement.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ public function addChildByProductSku($sku, $optionId, \Magento\Bundle\Api\Data\L
111111
* @SuppressWarnings(PHPMD.NPathComplexity)
112112
*/
113113
public function saveChild(
114-
\Magento\Catalog\Api\Data\ProductInterface $product,
114+
$sku,
115115
\Magento\Bundle\Api\Data\LinkInterface $linkedProduct
116116
) {
117+
$product = $this->productRepository->get($sku);
117118
if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
118119
throw new InputException(
119120
__('Product with specified sku: "%1" is not a bundle product', [$product->getSku()])
@@ -150,7 +151,7 @@ public function saveChild(
150151
throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
151152
}
152153

153-
return $selectionModel->getId();
154+
return true;
154155
}
155156

156157
/**

app/code/Magento/Bundle/Model/OptionRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ public function save(
181181
$optionCollection->setIdFilter($option->getOptionId());
182182

183183
/** @var \Magento\Bundle\Model\Option $existingOption */
184-
$existingOption = $optionCollection->getFirstItem();
184+
$existingOption = $optionCollection->getItemById($option->getOptionId());
185185

186-
if (!$existingOption->getOptionId()) {
186+
if (!isset($existingOption) || !$existingOption->getOptionId()) {
187187
throw new NoSuchEntityException(__('Requested option doesn\'t exist'));
188188
}
189189

@@ -234,7 +234,7 @@ protected function updateOptionSelection(
234234
$linksToDelete = array_udiff($existingLinks, $linksToUpdate, [$this, 'compareLinks']);
235235
}
236236
foreach ($linksToUpdate as $linkedProduct) {
237-
$this->linkManagement->saveChild($product, $linkedProduct);
237+
$this->linkManagement->saveChild($product->getSku(), $linkedProduct);
238238
}
239239
foreach ($linksToDelete as $linkedProduct) {
240240
$this->linkManagement->removeChild(

app/code/Magento/Bundle/Model/Plugin/BundleSaveOptions.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@ class BundleSaveOptions
1414
*/
1515
protected $optionRepository;
1616

17-
/**
18-
* @var \Magento\Catalog\Api\Data\ProductInterfaceFactory
19-
*/
20-
protected $productInterfaceFactory;
21-
2217
/**
2318
* @param \Magento\Bundle\Api\ProductOptionRepositoryInterface $optionRepository
24-
* @param \Magento\Catalog\Api\Data\ProductInterfaceFactory $productInterfaceFactory
2519
*/
2620
public function __construct(
27-
\Magento\Bundle\Api\ProductOptionRepositoryInterface $optionRepository,
28-
\Magento\Catalog\Api\Data\ProductInterfaceFactory $productInterfaceFactory
21+
\Magento\Bundle\Api\ProductOptionRepositoryInterface $optionRepository
2922
) {
3023
$this->optionRepository = $optionRepository;
31-
$this->productInterfaceFactory = $productInterfaceFactory;
3224
}
3325

3426
/**
@@ -81,7 +73,6 @@ public function aroundSave(
8173
foreach ($optionIdsToDelete as $optionId) {
8274
$this->optionRepository->delete($existingOptionsMap[$optionId]);
8375
}
84-
$product = $this->productInterfaceFactory->create()->setSku($result->getSku());
85-
return $subject->save($product);
76+
return $subject->get($result->getSku(), false, $result->getStoreId(), true);
8677
}
8778
}

app/code/Magento/Bundle/Test/Unit/Model/LinkManagementTest.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ public function testSaveChild()
559559
$isDefault = true;
560560
$linkProductId = 45;
561561
$parentProductId = 32;
562+
$bundleProductSku = 'bundleProductSku';
563+
562564
$productLink = $this->getMock('\Magento\Bundle\Api\Data\LinkInterface');
563565
$productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
564566
$productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
@@ -580,7 +582,12 @@ public function testSaveChild()
580582
$linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
581583
$linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
582584
$this->productRepository
583-
->expects($this->once())
585+
->expects($this->at(0))
586+
->method('get')
587+
->with($bundleProductSku)
588+
->will($this->returnValue($productMock));
589+
$this->productRepository
590+
->expects($this->at(1))
584591
->method('get')
585592
->with('linked_product_sku')
586593
->will($this->returnValue($linkedProductMock));
@@ -623,8 +630,7 @@ public function testSaveChild()
623630
$selection->expects($this->once())->method('setIsDefault')->with($isDefault);
624631

625632
$this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
626-
$result = $this->model->saveChild($productMock, $productLink);
627-
$this->assertEquals($id, $result);
633+
$this->assertTrue($this->model->saveChild($bundleProductSku, $productLink));
628634
}
629635

630636
/**
@@ -638,6 +644,7 @@ public function testSaveChildFailedToSave()
638644
$productLink = $this->getMock('\Magento\Bundle\Api\Data\LinkInterface');
639645
$productLink->expects($this->any())->method('getSku')->will($this->returnValue('linked_product_sku'));
640646
$productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
647+
$bundleProductSku = 'bundleProductSku';
641648

642649
$productMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
643650
$productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(
@@ -649,7 +656,12 @@ public function testSaveChildFailedToSave()
649656
$linkedProductMock->expects($this->any())->method('getId')->will($this->returnValue($linkProductId));
650657
$linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
651658
$this->productRepository
652-
->expects($this->once())
659+
->expects($this->at(0))
660+
->method('get')
661+
->with($bundleProductSku)
662+
->will($this->returnValue($productMock));
663+
$this->productRepository
664+
->expects($this->at(1))
653665
->method('get')
654666
->with('linked_product_sku')
655667
->will($this->returnValue($linkedProductMock));
@@ -686,14 +698,15 @@ public function testSaveChildFailedToSave()
686698
$selection->expects($this->once())->method('setProductId')->with($linkProductId);
687699

688700
$this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
689-
$this->model->saveChild($productMock, $productLink);
701+
$this->model->saveChild($bundleProductSku, $productLink);
690702
}
691703

692704
/**
693705
* @expectedException \Magento\Framework\Exception\InputException
694706
*/
695707
public function testSaveChildWithoutId()
696708
{
709+
$bundleProductSku = "bundleSku";
697710
$linkedProductSku = 'simple';
698711
$productLink = $this->getMock('\Magento\Bundle\Api\Data\LinkInterface');
699712
$productLink->expects($this->any())->method('getId')->will($this->returnValue(null));
@@ -707,12 +720,17 @@ public function testSaveChildWithoutId()
707720
$linkedProductMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
708721
$linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
709722
$this->productRepository
710-
->expects($this->once())
723+
->expects($this->at(0))
724+
->method('get')
725+
->with($bundleProductSku)
726+
->will($this->returnValue($productMock));
727+
$this->productRepository
728+
->expects($this->at(1))
711729
->method('get')
712730
->with($linkedProductSku)
713731
->will($this->returnValue($linkedProductMock));
714732

715-
$this->model->saveChild($productMock, $productLink);
733+
$this->model->saveChild($bundleProductSku, $productLink);
716734
}
717735

718736
/**
@@ -723,6 +741,7 @@ public function testSaveChildWithInvalidId()
723741
{
724742
$id = 12345;
725743
$linkedProductSku = 'simple';
744+
$bundleProductSku = "bundleProductSku";
726745
$productLink = $this->getMock('\Magento\Bundle\Api\Data\LinkInterface');
727746
$productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
728747
$productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
@@ -735,7 +754,12 @@ public function testSaveChildWithInvalidId()
735754
$linkedProductMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
736755
$linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(false));
737756
$this->productRepository
738-
->expects($this->once())
757+
->expects($this->at(0))
758+
->method('get')
759+
->with($bundleProductSku)
760+
->will($this->returnValue($productMock));
761+
$this->productRepository
762+
->expects($this->at(1))
739763
->method('get')
740764
->with($linkedProductSku)
741765
->will($this->returnValue($linkedProductMock));
@@ -755,14 +779,15 @@ public function testSaveChildWithInvalidId()
755779

756780
$this->bundleSelectionMock->expects($this->once())->method('create')->will($this->returnValue($selection));
757781

758-
$this->model->saveChild($productMock, $productLink);
782+
$this->model->saveChild($bundleProductSku, $productLink);
759783
}
760784

761785
/**
762786
* @expectedException \Magento\Framework\Exception\InputException
763787
*/
764788
public function testSaveChildWithCompositeProductLink()
765789
{
790+
$bundleProductSku = "bundleProductSku";
766791
$id = 12;
767792
$linkedProductSku = 'simple';
768793
$productLink = $this->getMock('\Magento\Bundle\Api\Data\LinkInterface');
@@ -777,12 +802,17 @@ public function testSaveChildWithCompositeProductLink()
777802
$linkedProductMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
778803
$linkedProductMock->expects($this->once())->method('isComposite')->will($this->returnValue(true));
779804
$this->productRepository
780-
->expects($this->once())
805+
->expects($this->at(0))
806+
->method('get')
807+
->with($bundleProductSku)
808+
->will($this->returnValue($productMock));
809+
$this->productRepository
810+
->expects($this->at(1))
781811
->method('get')
782812
->with($linkedProductSku)
783813
->will($this->returnValue($linkedProductMock));
784814

785-
$this->model->saveChild($productMock, $productLink);
815+
$this->model->saveChild($bundleProductSku, $productLink);
786816
}
787817

788818
/**
@@ -792,6 +822,8 @@ public function testSaveChildWithSimpleProduct()
792822
{
793823
$id = 12;
794824
$linkedProductSku = 'simple';
825+
$bundleProductSku = "bundleProductSku";
826+
795827
$productLink = $this->getMock('\Magento\Bundle\Api\Data\LinkInterface');
796828
$productLink->expects($this->any())->method('getId')->will($this->returnValue($id));
797829
$productLink->expects($this->any())->method('getSku')->will($this->returnValue($linkedProductSku));
@@ -801,7 +833,10 @@ public function testSaveChildWithSimpleProduct()
801833
\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
802834
));
803835

804-
$this->model->saveChild($productMock, $productLink);
836+
$this->productRepository->expects($this->once())->method('get')->with($bundleProductSku)
837+
->willReturn($productMock);
838+
839+
$this->model->saveChild($bundleProductSku, $productLink);
805840
}
806841

807842
public function testRemoveChild()

app/code/Magento/Bundle/Test/Unit/Model/Plugin/BundleSaveOptionsTest.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,16 @@ protected function setUp()
5959
$this->productOptionRepositoryMock = $this->getMock('Magento\Bundle\Api\ProductOptionRepositoryInterface');
6060
$this->productMock = $this->getMock(
6161
'Magento\Catalog\Model\Product',
62-
['getExtensionAttributes', 'getTypeId', 'getSku'],
62+
['getExtensionAttributes', 'getTypeId', 'getSku', 'getStoreId'],
6363
[],
6464
'',
6565
false
6666
);
6767
$this->closureMock = function () {
6868
return $this->productMock;
6969
};
70-
$this->productInterfaceFactoryMock = $this->getMockBuilder('Magento\Catalog\Api\Data\ProductInterfaceFactory')
71-
->disableOriginalConstructor()->getMock();
7270
$this->plugin = new BundleSaveOptions(
73-
$this->productOptionRepositoryMock,
74-
$this->productInterfaceFactoryMock
71+
$this->productOptionRepositoryMock
7572
);
7673
$this->productExtensionMock = $this->getMock(
7774
'Magento\Catalog\Api\Data\ProductExtension',
@@ -142,16 +139,9 @@ public function testAroundSaveWhenProductIsBundleWithOptions()
142139

143140
$newProductMock = $this->getMockBuilder('Magento\Catalog\Api\Data\ProductInterface')
144141
->disableOriginalConstructor()->getMock();
145-
$newProductMock->expects($this->once())
146-
->method('setSku')
147-
->with($productSku)
148-
->willReturnSelf();
149-
$this->productInterfaceFactoryMock->expects($this->once())
150-
->method('create')
151-
->willReturn($newProductMock);
152142
$this->productRepositoryMock->expects($this->once())
153-
->method('save')
154-
->with($newProductMock)
143+
->method('get')
144+
->with($productSku, false, null, true)
155145
->willReturn($newProductMock);
156146

157147
$this->assertEquals(
@@ -219,16 +209,9 @@ public function testAroundSaveWhenProductIsBundleWithOptionsAndExistingOptions()
219209

220210
$newProductMock = $this->getMockBuilder('Magento\Catalog\Api\Data\ProductInterface')
221211
->disableOriginalConstructor()->getMock();
222-
$newProductMock->expects($this->once())
223-
->method('setSku')
224-
->with($productSku)
225-
->willReturnSelf();
226-
$this->productInterfaceFactoryMock->expects($this->once())
227-
->method('create')
228-
->willReturn($newProductMock);
229212
$this->productRepositoryMock->expects($this->once())
230-
->method('save')
231-
->with($newProductMock)
213+
->method('get')
214+
->with($productSku, false, null, true)
232215
->willReturn($newProductMock);
233216

234217
$this->assertEquals(

app/code/Magento/Bundle/etc/webapi.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<resource ref="Magento_Catalog::products"/>
1414
</resources>
1515
</route>
16+
<route url="/V1/bundle-products/:sku/links" method="PUT">
17+
<service class="Magento\Bundle\Api\ProductLinkManagementInterface" method="saveChild"/>
18+
<resources>
19+
<resource ref="Magento_Catalog::products"/>
20+
</resources>
21+
</route>
1622
<route url="/V1/bundle-products/:productSku/children" method="GET">
1723
<service class="Magento\Bundle\Api\ProductLinkManagementInterface" method="getChildren"/>
1824
<resources>

app/code/Magento/Catalog/Api/ProductRepositoryInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,23 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
2727
* @param string $sku
2828
* @param bool $editMode
2929
* @param null|int $storeId
30+
* @param bool $forceReload
3031
* @return \Magento\Catalog\Api\Data\ProductInterface
3132
* @throws \Magento\Framework\Exception\NoSuchEntityException
3233
*/
33-
public function get($sku, $editMode = false, $storeId = null);
34+
public function get($sku, $editMode = false, $storeId = null, $forceReload = false);
3435

3536
/**
3637
* Get info about product by product id
3738
*
3839
* @param int $productId
3940
* @param bool $editMode
4041
* @param null|int $storeId
42+
* @param bool $forceReload
4143
* @return \Magento\Catalog\Api\Data\ProductInterface
4244
* @throws \Magento\Framework\Exception\NoSuchEntityException
4345
*/
44-
public function getById($productId, $editMode = false, $storeId = null);
46+
public function getById($productId, $editMode = false, $storeId = null, $forceReload = false);
4547

4648
/**
4749
* Delete product

0 commit comments

Comments
 (0)