|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Api; |
| 7 | + |
| 8 | +use Magento\Eav\Api\Data\AttributeOptionInterface; |
| 9 | +use Magento\Eav\Api\Data\AttributeOptionLabelInterface; |
| 10 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 11 | + |
| 12 | +class ProductSwatchAttributeOptionManagementInterfaceTest extends WebapiAbstract |
| 13 | +{ |
| 14 | + const SERVICE_NAME = 'catalogProductAttributeOptionManagementV1'; |
| 15 | + const SERVICE_VERSION = 'V1'; |
| 16 | + const RESOURCE_PATH = '/V1/products/attributes'; |
| 17 | + |
| 18 | + /** |
| 19 | + * @magentoApiDataFixture Magento/Swatches/_files/swatch_attribute.php |
| 20 | + * @dataProvider addDataProvider |
| 21 | + */ |
| 22 | + public function testAdd($optionData) |
| 23 | + { |
| 24 | + $testAttributeCode = 'color_swatch'; |
| 25 | + $serviceInfo = [ |
| 26 | + 'rest' => [ |
| 27 | + 'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options', |
| 28 | + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, |
| 29 | + ], |
| 30 | + 'soap' => [ |
| 31 | + 'service' => self::SERVICE_NAME, |
| 32 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 33 | + 'operation' => self::SERVICE_NAME . 'add', |
| 34 | + ], |
| 35 | + ]; |
| 36 | + |
| 37 | + $response = $this->_webApiCall( |
| 38 | + $serviceInfo, |
| 39 | + [ |
| 40 | + 'attributeCode' => $testAttributeCode, |
| 41 | + 'option' => $optionData, |
| 42 | + ] |
| 43 | + ); |
| 44 | + |
| 45 | + $this->assertTrue($response); |
| 46 | + $updatedData = $this->getAttributeOptions($testAttributeCode); |
| 47 | + $lastOption = array_pop($updatedData); |
| 48 | + $this->assertEquals( |
| 49 | + $optionData[AttributeOptionInterface::STORE_LABELS][0][AttributeOptionLabelInterface::LABEL], |
| 50 | + $lastOption['label'] |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + public function addDataProvider() |
| 58 | + { |
| 59 | + $optionPayload = [ |
| 60 | + AttributeOptionInterface::LABEL => 'new color', |
| 61 | + AttributeOptionInterface::SORT_ORDER => 100, |
| 62 | + AttributeOptionInterface::IS_DEFAULT => true, |
| 63 | + AttributeOptionInterface::STORE_LABELS => [ |
| 64 | + [ |
| 65 | + AttributeOptionLabelInterface::LABEL => 'DE label', |
| 66 | + AttributeOptionLabelInterface::STORE_ID => 1, |
| 67 | + ], |
| 68 | + ], |
| 69 | + AttributeOptionInterface::VALUE => '' |
| 70 | + ]; |
| 71 | + |
| 72 | + return [ |
| 73 | + 'option_without_value_node' => [ |
| 74 | + $optionPayload |
| 75 | + ], |
| 76 | + 'option_with_value_node_that_starts_with_text' => [ |
| 77 | + array_merge($optionPayload, [AttributeOptionInterface::VALUE => 'some_text']) |
| 78 | + ], |
| 79 | + 'option_with_value_node_that_starts_with_a_number' => [ |
| 80 | + array_merge($optionPayload, [AttributeOptionInterface::VALUE => '123_some_text']) |
| 81 | + ], |
| 82 | + |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param $testAttributeCode |
| 88 | + * @return array|bool|float|int|string |
| 89 | + */ |
| 90 | + private function getAttributeOptions($testAttributeCode) |
| 91 | + { |
| 92 | + $serviceInfo = [ |
| 93 | + 'rest' => [ |
| 94 | + 'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options', |
| 95 | + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, |
| 96 | + ], |
| 97 | + 'soap' => [ |
| 98 | + 'service' => self::SERVICE_NAME, |
| 99 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 100 | + 'operation' => self::SERVICE_NAME . 'getItems', |
| 101 | + ], |
| 102 | + ]; |
| 103 | + return $this->_webApiCall($serviceInfo, ['attributeCode' => $testAttributeCode]); |
| 104 | + } |
| 105 | +} |
0 commit comments