4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
+ declare (strict_types=1 );
8
+
7
9
namespace Magento \Catalog \Api ;
8
10
11
+ use Magento \Framework \ObjectManagerInterface ;
12
+ use Magento \Framework \Webapi \Rest \Request ;
9
13
use Magento \TestFramework \Helper \Bootstrap ;
10
14
use Magento \TestFramework \TestCase \WebapiAbstract ;
11
15
16
+ /**
17
+ * Class ProductLinkRepositoryInterfaceTest
18
+ *
19
+ * @see \Magento\Catalog\Api\ProductLinkRepository
20
+ */
12
21
class ProductLinkRepositoryInterfaceTest extends WebapiAbstract
13
22
{
23
+ /**
24
+ * @var string
25
+ */
14
26
const SERVICE_NAME = 'catalogProductLinkRepositoryV1 ' ;
27
+
28
+ /**
29
+ * @var string
30
+ */
15
31
const SERVICE_VERSION = 'V1 ' ;
32
+
33
+ /**
34
+ * @var string
35
+ */
16
36
const RESOURCE_PATH = '/V1/products/ ' ;
17
37
18
38
/**
19
- * @var \Magento\Framework\ ObjectManagerInterface
39
+ * @var ObjectManagerInterface
20
40
*/
21
- protected $ objectManager ;
41
+ private $ objectManager ;
22
42
43
+ /**
44
+ * @var ProductLinkManagementInterface
45
+ */
46
+ private $ linkManagement ;
47
+
48
+ /**
49
+ * @inheritdoc
50
+ */
23
51
protected function setUp (): void
24
52
{
53
+ parent ::setUp ();
54
+
25
55
$ this ->objectManager = Bootstrap::getObjectManager ();
56
+ $ this ->linkManagement = $ this ->objectManager ->get (ProductLinkManagementInterface::class);
26
57
}
27
58
28
59
/**
29
60
* @magentoApiDataFixture Magento/Catalog/_files/products_related_multiple.php
30
- * @magentoAppIsolation enabled
61
+ *
62
+ * @return void
31
63
*/
32
- public function testDelete ()
64
+ public function testDelete (): void
33
65
{
34
66
$ productSku = 'simple_with_cross ' ;
35
- $ linkedSku = 'simple ' ;
36
67
$ 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 );
58
70
$ this ->assertCount (1 , $ linkedProducts );
59
- /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $product */
60
71
$ 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 ' );
62
85
}
63
86
64
87
/**
@@ -68,36 +91,85 @@ public function testSave()
68
91
{
69
92
$ productSku = 'simple_with_cross ' ;
70
93
$ 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
+ }
71
108
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 [
73
120
'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 ,
76
123
],
77
124
'soap ' => [
78
125
'service ' => self ::SERVICE_NAME ,
79
126
'serviceVersion ' => self ::SERVICE_VERSION ,
80
- 'operation ' => self ::SERVICE_NAME . ' Save ' ,
127
+ 'operation ' => self ::SERVICE_NAME . $ operation ,
81
128
],
82
129
];
130
+ }
83
131
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 (
85
149
$ serviceInfo ,
86
150
[
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 ,
94
154
]
95
155
);
156
+ }
96
157
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 );
102
174
}
103
175
}
0 commit comments