44 * Copyright © Magento, Inc. All rights reserved.
55 * See COPYING.txt for license details.
66 */
7+ declare (strict_types=1 );
8+
79namespace Magento \Catalog \Api ;
810
11+ use Magento \Framework \ObjectManagerInterface ;
12+ use Magento \Framework \Webapi \Rest \Request ;
913use Magento \TestFramework \Helper \Bootstrap ;
1014use Magento \TestFramework \TestCase \WebapiAbstract ;
1115
16+ /**
17+ * Class ProductLinkRepositoryInterfaceTest
18+ *
19+ * @see \Magento\Catalog\Api\ProductLinkRepository
20+ */
1221class ProductLinkRepositoryInterfaceTest extends WebapiAbstract
1322{
23+ /**
24+ * @var string
25+ */
1426 const SERVICE_NAME = 'catalogProductLinkRepositoryV1 ' ;
27+
28+ /**
29+ * @var string
30+ */
1531 const SERVICE_VERSION = 'V1 ' ;
32+
33+ /**
34+ * @var string
35+ */
1636 const RESOURCE_PATH = '/V1/products/ ' ;
1737
1838 /**
19- * @var \Magento\Framework\ ObjectManagerInterface
39+ * @var ObjectManagerInterface
2040 */
21- protected $ objectManager ;
41+ private $ objectManager ;
2242
43+ /**
44+ * @var ProductLinkManagementInterface
45+ */
46+ private $ linkManagement ;
47+
48+ /**
49+ * @inheritdoc
50+ */
2351 protected function setUp (): void
2452 {
53+ parent ::setUp ();
54+
2555 $ this ->objectManager = Bootstrap::getObjectManager ();
56+ $ this ->linkManagement = $ this ->objectManager ->get (ProductLinkManagementInterface::class);
2657 }
2758
2859 /**
2960 * @magentoApiDataFixture Magento/Catalog/_files/products_related_multiple.php
30- * @magentoAppIsolation enabled
61+ *
62+ * @return void
3163 */
32- public function testDelete ()
64+ public function testDelete (): void
3365 {
3466 $ productSku = 'simple_with_cross ' ;
35- $ linkedSku = 'simple ' ;
3667 $ linkType = 'related ' ;
37- $ this ->_webApiCall (
38- [
39- 'rest ' => [
40- 'resourcePath ' => self ::RESOURCE_PATH . $ productSku . '/links/ ' . $ linkType . '/ ' . $ linkedSku ,
41- 'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_DELETE ,
42- ],
43- 'soap ' => [
44- 'service ' => self ::SERVICE_NAME ,
45- 'serviceVersion ' => self ::SERVICE_VERSION ,
46- 'operation ' => self ::SERVICE_NAME . 'DeleteById ' ,
47- ],
48- ],
49- [
50- 'sku ' => $ productSku ,
51- 'type ' => $ linkType ,
52- 'linkedProductSku ' => $ linkedSku
53- ]
54- );
55- /** @var \Magento\Catalog\Model\ProductLink\Management $linkManagement */
56- $ linkManagement = $ this ->objectManager ->create (\Magento \Catalog \Api \ProductLinkManagementInterface::class);
57- $ linkedProducts = $ linkManagement ->getLinkedItemsByType ($ productSku , $ linkType );
68+ $ this ->deleteApiCall ($ productSku , $ linkType , 'simple ' );
69+ $ linkedProducts = $ this ->linkManagement ->getLinkedItemsByType ($ productSku , $ linkType );
5870 $ this ->assertCount (1 , $ linkedProducts );
59- /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $product */
6071 $ product = current ($ linkedProducts );
61- $ this ->assertEquals ($ product ->getLinkedProductSku (), 'simple_with_cross_two ' );
72+ $ this ->assertEquals ('simple_with_cross_two ' , $ product ->getLinkedProductSku ());
73+ }
74+
75+ /**
76+ * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
77+ *
78+ * @return void
79+ */
80+ public function testDeleteNotExistedProductLink (): void
81+ {
82+ $ this ->expectException (\Exception::class);
83+ $ this ->expectExceptionMessage ((string )__ ("Product %1 doesn't have linked %2 as %3 " ));
84+ $ this ->deleteApiCall ('simple ' , 'related ' , 'not_exists_product ' );
6285 }
6386
6487 /**
@@ -68,36 +91,85 @@ public function testSave()
6891 {
6992 $ productSku = 'simple_with_cross ' ;
7093 $ linkType = 'related ' ;
94+ $ data = [
95+ 'entity ' => [
96+ 'sku ' => 'simple_with_cross ' ,
97+ 'link_type ' => 'related ' ,
98+ 'linked_product_sku ' => 'simple ' ,
99+ 'linked_product_type ' => 'simple ' ,
100+ 'position ' => 1000 ,
101+ ],
102+ ];
103+ $ this ->saveApiCall ($ productSku , $ data );
104+ $ actual = $ this ->linkManagement ->getLinkedItemsByType ($ productSku , $ linkType );
105+ $ this ->assertCount (1 , $ actual , 'Invalid actual linked products count ' );
106+ $ this ->assertEquals (1000 , $ actual [0 ]->getPosition (), 'Product position is not updated ' );
107+ }
71108
72- $ serviceInfo = [
109+ /**
110+ * Get service info for api call
111+ *
112+ * @param string $resourcePath
113+ * @param string $httpMethod
114+ * @param string $operation
115+ * @return array
116+ */
117+ private function getServiceInfo (string $ resourcePath , string $ httpMethod , string $ operation ): array
118+ {
119+ return [
73120 'rest ' => [
74- 'resourcePath ' => self ::RESOURCE_PATH . $ productSku . ' /links ' ,
75- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \Request:: HTTP_METHOD_PUT ,
121+ 'resourcePath ' => self ::RESOURCE_PATH . $ resourcePath ,
122+ 'httpMethod ' => $ httpMethod ,
76123 ],
77124 'soap ' => [
78125 'service ' => self ::SERVICE_NAME ,
79126 'serviceVersion ' => self ::SERVICE_VERSION ,
80- 'operation ' => self ::SERVICE_NAME . ' Save ' ,
127+ 'operation ' => self ::SERVICE_NAME . $ operation ,
81128 ],
82129 ];
130+ }
83131
84- $ this ->_webApiCall (
132+ /**
133+ * Make api call to delete product link
134+ *
135+ * @param string $productSku
136+ * @param string $linkType
137+ * @param string $linkedSku
138+ * @return array|int|string|float|bool
139+ */
140+ private function deleteApiCall (string $ productSku , string $ linkType , string $ linkedSku )
141+ {
142+ $ serviceInfo = $ this ->getServiceInfo (
143+ $ productSku . '/links/ ' . $ linkType . '/ ' . $ linkedSku ,
144+ Request::HTTP_METHOD_DELETE ,
145+ 'DeleteById '
146+ );
147+
148+ return $ this ->_webApiCall (
85149 $ serviceInfo ,
86150 [
87- 'entity ' => [
88- 'sku ' => 'simple_with_cross ' ,
89- 'link_type ' => 'related ' ,
90- 'linked_product_sku ' => 'simple ' ,
91- 'linked_product_type ' => 'simple ' ,
92- 'position ' => 1000 ,
93- ]
151+ 'sku ' => $ productSku ,
152+ 'type ' => $ linkType ,
153+ 'linkedProductSku ' => $ linkedSku ,
94154 ]
95155 );
156+ }
96157
97- /** @var \Magento\Catalog\Model\ProductLink\Management $linkManagement */
98- $ linkManagement = $ this ->objectManager ->get (\Magento \Catalog \Api \ProductLinkManagementInterface::class);
99- $ actual = $ linkManagement ->getLinkedItemsByType ($ productSku , $ linkType );
100- $ this ->assertCount (1 , $ actual , 'Invalid actual linked products count ' );
101- $ this ->assertEquals (1000 , $ actual [0 ]->getPosition (), 'Product position is not updated ' );
158+ /**
159+ * Make api call to save product link
160+ *
161+ * @param $productSku
162+ * @param $data
163+ * @return array|bool|float|int|string
164+ */
165+ private function saveApiCall ($ productSku , $ data )
166+ {
167+ $ serviceInfo = $ this ->getServiceInfo (
168+ $ productSku . '/links ' ,
169+ Request::HTTP_METHOD_PUT ,
170+ 'Save '
171+ );
172+
173+ return $ this ->_webApiCall ($ serviceInfo , $ data );
102174 }
103175}
0 commit comments