Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductAttributeOptionManagementInterfaceTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Api;
7 
11 
13 {
14  const SERVICE_NAME = 'catalogProductAttributeOptionManagementV1';
15  const SERVICE_VERSION = 'V1';
16  const RESOURCE_PATH = '/V1/products/attributes';
17 
18  public function testGetItems()
19  {
20  $testAttributeCode = 'quantity_and_stock_status';
21  $expectedOptions = [
22  [
24  AttributeOptionInterface::LABEL => 'In Stock',
25  ],
26  [
28  AttributeOptionInterface::LABEL => 'Out of Stock',
29  ],
30  ];
31 
32  $serviceInfo = [
33  'rest' => [
34  'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options',
36  ],
37  'soap' => [
38  'service' => self::SERVICE_NAME,
39  'serviceVersion' => self::SERVICE_VERSION,
40  'operation' => self::SERVICE_NAME . 'getItems',
41  ],
42  ];
43 
44  $response = $this->_webApiCall($serviceInfo, ['attributeCode' => $testAttributeCode]);
45 
46  $this->assertTrue(is_array($response));
47  $this->assertEquals($expectedOptions, $response);
48  }
49 
54  public function testAdd($optionData)
55  {
56  $testAttributeCode = 'select_attribute';
57  $serviceInfo = [
58  'rest' => [
59  'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options',
61  ],
62  'soap' => [
63  'service' => self::SERVICE_NAME,
64  'serviceVersion' => self::SERVICE_VERSION,
65  'operation' => self::SERVICE_NAME . 'add',
66  ],
67  ];
68 
69  $response = $this->_webApiCall(
70  $serviceInfo,
71  [
72  'attributeCode' => $testAttributeCode,
73  'option' => $optionData,
74  ]
75  );
76 
77  $this->assertNotNull($response);
78  $updatedData = $this->getAttributeOptions($testAttributeCode);
79  $lastOption = array_pop($updatedData);
80  $this->assertEquals(
82  $lastOption['label']
83  );
84  }
85 
89  public function addDataProvider()
90  {
91  $optionPayload = [
92  AttributeOptionInterface::LABEL => 'new color',
96  [
99  ],
100  ],
102  ];
103 
104  return [
105  'option_without_value_node' => [
106  $optionPayload
107  ],
108  'option_with_value_node_that_starts_with_text' => [
109  array_merge($optionPayload, [AttributeOptionInterface::VALUE => 'some_text'])
110  ],
111  'option_with_value_node_that_starts_with_a_number' => [
112  array_merge($optionPayload, [AttributeOptionInterface::VALUE => '123_some_text'])
113  ],
114 
115  ];
116  }
117 
121  public function testDelete()
122  {
123  $attributeCode = 'select_attribute';
124  //get option Id
125  $optionList = $this->getAttributeOptions($attributeCode);
126  $this->assertGreaterThan(0, count($optionList));
127  $lastOption = array_pop($optionList);
128  $this->assertNotEmpty($lastOption['value']);
129  $optionId = $lastOption['value'];
130 
131  $serviceInfo = [
132  'rest' => [
133  'resourcePath' => self::RESOURCE_PATH . '/' . $attributeCode . '/options/' . $optionId,
135  ],
136  'soap' => [
137  'service' => self::SERVICE_NAME,
138  'serviceVersion' => self::SERVICE_VERSION,
139  'operation' => self::SERVICE_NAME . 'delete',
140  ],
141  ];
142  $this->assertTrue($this->_webApiCall(
143  $serviceInfo,
144  [
145  'attributeCode' => $attributeCode,
146  'optionId' => $optionId,
147  ]
148  ));
149  $updatedOptions = $this->getAttributeOptions($attributeCode);
150  $this->assertEquals($optionList, $updatedOptions);
151  }
152 
157  private function getAttributeOptions($testAttributeCode)
158  {
159  $serviceInfo = [
160  'rest' => [
161  'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options',
163  ],
164  'soap' => [
165  'service' => self::SERVICE_NAME,
166  'serviceVersion' => self::SERVICE_VERSION,
167  'operation' => self::SERVICE_NAME . 'getItems',
168  ],
169  ];
170  return $this->_webApiCall($serviceInfo, ['attributeCode' => $testAttributeCode]);
171  }
172 }
$response
Definition: 404.php:11
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$optionData
$attributeCode
Definition: extend.phtml:12