12
12
use Magento \Eav \Api \Data \AttributeOptionInterface ;
13
13
use Magento \Eav \Api \Data \AttributeOptionLabelInterface ;
14
14
use Magento \Eav \Model \AttributeRepository ;
15
+ use Magento \Framework \DataObject ;
15
16
use Magento \Framework \Webapi \Rest \Request ;
16
17
use Magento \Swatches \Model \ResourceModel \Swatch \Collection ;
17
18
use Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory ;
25
26
class ProductAttributeOptionManagementInterfaceTest extends WebapiAbstract
26
27
{
27
28
private const ATTRIBUTE_CODE = 'select_attribute ' ;
29
+ private const SERVICE_NAME_UPDATE = 'catalogProductAttributeOptionUpdateV1 ' ;
28
30
private const SERVICE_NAME = 'catalogProductAttributeOptionManagementV1 ' ;
29
31
private const SERVICE_VERSION = 'V1 ' ;
30
32
private const RESOURCE_PATH = '/V1/products/attributes ' ;
31
33
32
34
/**
33
35
* Test add option to swatch attribute
34
36
*
37
+ * @dataProvider addDataProvider
35
38
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/select_attribute.php
36
39
* @param array $data
37
40
* @param array $payload
38
41
* @param int $expectedSwatchType
39
42
* @param string $expectedLabel
40
43
* @param string $expectedValue
41
44
*
42
- * @dataProvider addDataProvider
45
+ * @return void
43
46
*/
44
47
public function testAdd (
45
48
array $ data ,
46
49
array $ payload ,
47
50
int $ expectedSwatchType ,
48
51
string $ expectedLabel ,
49
52
string $ expectedValue
50
- ) {
53
+ ): void {
51
54
$ objectManager = Bootstrap::getObjectManager ();
52
55
/** @var $attributeRepository AttributeRepository */
53
56
$ attributeRepository = $ objectManager ->get (AttributeRepository::class);
@@ -74,7 +77,7 @@ public function testAdd(
74
77
);
75
78
76
79
$ this ->assertNotNull ($ response );
77
- $ optionId = (int ) ltrim ($ response , 'id_ ' );
80
+ $ optionId = (int )ltrim ($ response , 'id_ ' );
78
81
$ swatch = $ this ->getSwatch ($ optionId );
79
82
$ this ->assertEquals ($ expectedValue , $ swatch ->getValue ());
80
83
$ this ->assertEquals ($ expectedSwatchType , $ swatch ->getType ());
@@ -83,11 +86,47 @@ public function testAdd(
83
86
$ this ->assertEquals ($ expectedLabel , $ options [2 ]->getLabel ());
84
87
}
85
88
89
+ /**
90
+ * @magentoApiDataFixture Magento/Swatches/_files/text_swatch_attribute.php
91
+ * @return void
92
+ */
93
+ public function testUpdate (): void
94
+ {
95
+ $ testAttributeCode = 'test_configurable ' ;
96
+ $ optionData = [
97
+ AttributeOptionInterface::LABEL => 'Fixture Option Changed ' ,
98
+ AttributeOptionInterface::VALUE => 'option_value ' ,
99
+ ];
100
+
101
+ $ existOptionLabel = 'option 1 ' ;
102
+ $ existAttributeOption = $ this ->getAttributeOption ($ testAttributeCode , $ existOptionLabel );
103
+ $ optionId = $ existAttributeOption ['value ' ];
104
+
105
+ $ response = $ this ->webApiCallAttributeOptions (
106
+ $ testAttributeCode ,
107
+ Request::HTTP_METHOD_PUT ,
108
+ 'update ' ,
109
+ [
110
+ 'attributeCode ' => $ testAttributeCode ,
111
+ 'optionId ' => $ optionId ,
112
+ 'option ' => $ optionData ,
113
+ ],
114
+ $ optionId
115
+ );
116
+ $ this ->assertTrue ($ response );
117
+ $ this ->assertNotNull (
118
+ $ this ->getAttributeOption (
119
+ $ testAttributeCode ,
120
+ $ optionData [AttributeOptionInterface::LABEL ]
121
+ )
122
+ );
123
+ }
124
+
86
125
/**
87
126
* @return array
88
127
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
89
128
*/
90
- public function addDataProvider ()
129
+ public function addDataProvider (): array
91
130
{
92
131
return [
93
132
'visual swatch option with value ' => [
@@ -212,14 +251,99 @@ public function addDataProvider()
212
251
* Get swatch model
213
252
*
214
253
* @param int $optionId
215
- * @return Swatch
254
+ * @return DataObject
216
255
*/
217
- private function getSwatch (int $ optionId )
256
+ private function getSwatch (int $ optionId ): DataObject
218
257
{
219
258
/** @var Collection $collection */
220
259
$ collection = Bootstrap::getObjectManager ()->get (CollectionFactory::class)->create ();
221
260
$ collection ->addFieldToFilter ('option_id ' , $ optionId );
222
261
$ collection ->setPageSize (1 );
262
+
223
263
return $ collection ->getFirstItem ();
224
264
}
265
+
266
+ /**
267
+ * Perform Web API call to the system under test
268
+ *
269
+ * @param string $attributeCode
270
+ * @param string $httpMethod
271
+ * @param string $soapMethod
272
+ * @param array $arguments
273
+ * @param null $storeCode
274
+ * @param null $optionId
275
+ * @return array|bool|float|int|string
276
+ */
277
+ private function webApiCallAttributeOptions (
278
+ string $ attributeCode ,
279
+ string $ httpMethod ,
280
+ string $ soapMethod ,
281
+ array $ arguments = [],
282
+ $ optionId = null ,
283
+ $ storeCode = null
284
+ ) {
285
+ $ resourcePath = self ::RESOURCE_PATH . "/ {$ attributeCode }/options " ;
286
+ if ($ optionId ) {
287
+ $ resourcePath .= '/ ' . $ optionId ;
288
+ }
289
+ $ serviceName = $ soapMethod === 'update ' ? self ::SERVICE_NAME_UPDATE : self ::SERVICE_NAME ;
290
+ $ serviceInfo = [
291
+ 'rest ' => [
292
+ 'resourcePath ' => $ resourcePath ,
293
+ 'httpMethod ' => $ httpMethod ,
294
+ ],
295
+ 'soap ' => [
296
+ 'service ' => $ serviceName ,
297
+ 'serviceVersion ' => self ::SERVICE_VERSION ,
298
+ 'operation ' => $ serviceName . $ soapMethod ,
299
+ ],
300
+ ];
301
+
302
+ return $ this ->_webApiCall ($ serviceInfo , $ arguments , null , $ storeCode );
303
+ }
304
+
305
+ /**
306
+ * Get Attribute options by attribute code
307
+ *
308
+ * @param string $testAttributeCode
309
+ * @param string|null $storeCode
310
+ * @return array|bool|float|int|string
311
+ */
312
+ private function getAttributeOptions (string $ testAttributeCode , ?string $ storeCode = null )
313
+ {
314
+ return $ this ->webApiCallAttributeOptions (
315
+ $ testAttributeCode ,
316
+ Request::HTTP_METHOD_GET ,
317
+ 'getItems ' ,
318
+ ['attributeCode ' => $ testAttributeCode ],
319
+ null ,
320
+ $ storeCode
321
+ );
322
+ }
323
+
324
+ /**
325
+ * Get Attribute option by attribute code
326
+ *
327
+ * @param string $attributeCode
328
+ * @param string $optionLabel
329
+ * @param string|null $storeCode
330
+ * @return array|null
331
+ */
332
+ private function getAttributeOption (
333
+ string $ attributeCode ,
334
+ string $ optionLabel ,
335
+ ?string $ storeCode = null
336
+ ): ?array {
337
+ $ attributeOptions = $ this ->getAttributeOptions ($ attributeCode , $ storeCode );
338
+ $ option = null ;
339
+ /** @var array $attributeOption */
340
+ foreach ($ attributeOptions as $ attributeOption ) {
341
+ if ($ attributeOption ['label ' ] === $ optionLabel ) {
342
+ $ option = $ attributeOption ;
343
+ break ;
344
+ }
345
+ }
346
+
347
+ return $ option ;
348
+ }
225
349
}
0 commit comments