1212use Magento \Eav \Api \Data \AttributeOptionInterface ;
1313use Magento \Eav \Api \Data \AttributeOptionLabelInterface ;
1414use Magento \Eav \Model \AttributeRepository ;
15+ use Magento \Framework \DataObject ;
1516use Magento \Framework \Webapi \Rest \Request ;
1617use Magento \Swatches \Model \ResourceModel \Swatch \Collection ;
1718use Magento \Swatches \Model \ResourceModel \Swatch \CollectionFactory ;
2526class ProductAttributeOptionManagementInterfaceTest extends WebapiAbstract
2627{
2728 private const ATTRIBUTE_CODE = 'select_attribute ' ;
29+ private const SERVICE_NAME_UPDATE = 'catalogProductAttributeOptionUpdateV1 ' ;
2830 private const SERVICE_NAME = 'catalogProductAttributeOptionManagementV1 ' ;
2931 private const SERVICE_VERSION = 'V1 ' ;
3032 private const RESOURCE_PATH = '/V1/products/attributes ' ;
3133
3234 /**
3335 * Test add option to swatch attribute
3436 *
37+ * @dataProvider addDataProvider
3538 * @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/select_attribute.php
3639 * @param array $data
3740 * @param array $payload
3841 * @param int $expectedSwatchType
3942 * @param string $expectedLabel
4043 * @param string $expectedValue
4144 *
42- * @dataProvider addDataProvider
45+ * @return void
4346 */
4447 public function testAdd (
4548 array $ data ,
4649 array $ payload ,
4750 int $ expectedSwatchType ,
4851 string $ expectedLabel ,
4952 string $ expectedValue
50- ) {
53+ ): void {
5154 $ objectManager = Bootstrap::getObjectManager ();
5255 /** @var $attributeRepository AttributeRepository */
5356 $ attributeRepository = $ objectManager ->get (AttributeRepository::class);
@@ -74,7 +77,7 @@ public function testAdd(
7477 );
7578
7679 $ this ->assertNotNull ($ response );
77- $ optionId = (int ) ltrim ($ response , 'id_ ' );
80+ $ optionId = (int )ltrim ($ response , 'id_ ' );
7881 $ swatch = $ this ->getSwatch ($ optionId );
7982 $ this ->assertEquals ($ expectedValue , $ swatch ->getValue ());
8083 $ this ->assertEquals ($ expectedSwatchType , $ swatch ->getType ());
@@ -83,11 +86,47 @@ public function testAdd(
8386 $ this ->assertEquals ($ expectedLabel , $ options [2 ]->getLabel ());
8487 }
8588
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+
86125 /**
87126 * @return array
88127 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
89128 */
90- public function addDataProvider ()
129+ public function addDataProvider (): array
91130 {
92131 return [
93132 'visual swatch option with value ' => [
@@ -212,14 +251,99 @@ public function addDataProvider()
212251 * Get swatch model
213252 *
214253 * @param int $optionId
215- * @return Swatch
254+ * @return DataObject
216255 */
217- private function getSwatch (int $ optionId )
256+ private function getSwatch (int $ optionId ): DataObject
218257 {
219258 /** @var Collection $collection */
220259 $ collection = Bootstrap::getObjectManager ()->get (CollectionFactory::class)->create ();
221260 $ collection ->addFieldToFilter ('option_id ' , $ optionId );
222261 $ collection ->setPageSize (1 );
262+
223263 return $ collection ->getFirstItem ();
224264 }
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+ }
225349}
0 commit comments