Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductAttributeManagementTest.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Catalog\Api;
9 
11 use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
12 
14 {
15  const SERVICE_NAME = 'catalogProductAttributeManagementV1';
16  const SERVICE_VERSION = 'V1';
17  const RESOURCE_PATH = '/V1/products/attribute-sets';
18 
19  public function testGetAttributes()
20  {
21  $attributeSetId = 4;
22 
23  $serviceInfo = [
24  'rest' => [
25  'resourcePath' => self::RESOURCE_PATH . '/' . $attributeSetId . '/attributes',
27  ],
28  'soap' => [
29  'service' => self::SERVICE_NAME,
30  'serviceVersion' => self::SERVICE_VERSION,
31  'operation' => self::SERVICE_NAME . 'GetAttributes',
32  ],
33  ];
34  $attributes = $this->_webApiCall($serviceInfo, ['attributeSetId' => $attributeSetId]);
35 
36  $this->assertTrue(count($attributes) > 0);
37  $this->assertArrayHasKey('attribute_code', $attributes[0]);
38  $this->assertArrayHasKey('attribute_id', $attributes[0]);
39  $this->assertArrayHasKey('default_frontend_label', $attributes[0]);
40  $this->assertNotNull($attributes[0]['attribute_code']);
41  $this->assertNotNull($attributes[0]['attribute_id']);
42  $this->assertNotNull($attributes[0]['default_frontend_label']);
43  }
44 
45  public function testAssignAttribute()
46  {
47  $this->assertNotNull(
48  $this->_webApiCall(
49  $this->getAssignServiceInfo(),
50  $this->getAttributeData()
51  )
52  );
53  }
54 
56  {
57  $payload = $this->getAttributeData();
58  $payload['attributeSetId'] = -1;
59 
60  $expectedMessage = 'The AttributeSet with a "%1" ID doesn\'t exist. Verify the attributeSet and try again.';
61 
62  try {
63  $this->_webApiCall($this->getAssignServiceInfo(), $payload);
64  $this->fail("Expected exception");
65  } catch (\SoapFault $e) {
66  $this->assertContains(
67  $expectedMessage,
68  $e->getMessage(),
69  "SoapFault does not contain expected message."
70  );
71  } catch (\Exception $e) {
72  $errorObj = $this->processRestExceptionResult($e);
73  $this->assertEquals($expectedMessage, $errorObj['message']);
74  $this->assertEquals([$payload['attributeSetId']], $errorObj['parameters']);
75  $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode());
76  }
77  }
78 
80  {
81  $payload = $this->getAttributeData();
82  $payload['attributeGroupId'] = -1;
83  $expectedMessage = 'The group with the "%1" ID doesn\'t exist. Verify the ID and try again.';
84 
85  try {
86  $this->_webApiCall($this->getAssignServiceInfo(), $payload);
87  $this->fail("Expected exception");
88  } catch (\SoapFault $e) {
89  $this->assertContains(
90  $expectedMessage,
91  $e->getMessage(),
92  "SoapFault does not contain expected message."
93  );
94  } catch (\Exception $e) {
95  $errorObj = $this->processRestExceptionResult($e);
96  $this->assertEquals($expectedMessage, $errorObj['message']);
97  $this->assertEquals([$payload['attributeGroupId']], $errorObj['parameters']);
98  $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode());
99  }
100  }
101 
103  {
104  $payload = $this->getAttributeData();
105  $payload['attributeCode'] = 'badCode';
106  $expectedMessage =
107  'The attribute with a "%1" attributeCode doesn\'t exist. Verify the attribute and try again.';
108 
109  try {
110  $this->_webApiCall($this->getAssignServiceInfo(), $payload);
111  $this->fail("Expected exception");
112  } catch (\SoapFault $e) {
113  $this->assertContains(
114  $expectedMessage,
115  $e->getMessage(),
116  "SoapFault does not contain expected message."
117  );
118  } catch (\Exception $e) {
119  $errorObj = $this->processRestExceptionResult($e);
120  $this->assertEquals($expectedMessage, $errorObj['message']);
121  $this->assertEquals([$payload['attributeCode']], $errorObj['parameters']);
122  $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode());
123  }
124  }
125 
126  public function testUnassignAttribute()
127  {
128  $payload = $this->getAttributeData();
129 
130  //Assign attribute to attribute set
132  $attributeManagement = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\AttributeManagement::class);
133  $attributeManagement->assign(
134  \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
135  $payload['attributeSetId'],
136  $payload['attributeGroupId'],
137  $payload['attributeCode'],
138  $payload['sortOrder']
139  );
140 
141  $serviceInfo = [
142  'rest' => [
143  'resourcePath' => self::RESOURCE_PATH .
144  '/' . $payload['attributeSetId'] .
145  '/attributes/' .
146  $payload['attributeCode'],
148  ],
149  'soap' => [
150  'service' => self::SERVICE_NAME,
151  'serviceVersion' => self::SERVICE_VERSION,
152  'operation' => self::SERVICE_NAME . 'Unassign',
153  ],
154  ];
155  $this->assertTrue(
156  $this->_webApiCall(
157  $serviceInfo,
158  [
159  'attributeSetId' => $payload['attributeSetId'],
160  'attributeCode' => $payload['attributeCode'],
161  ]
162  )
163  );
164  }
165 
166  protected function getAttributeData()
167  {
168  return [
169  'attributeSetId' => 4,
170  'attributeGroupId' => 8,
171  'attributeCode' => 'cost',
172  'sortOrder' => 3,
173  ];
174  }
175 
176  protected function getAssignServiceInfo()
177  {
178  return [
179  'rest' => [
180  'resourcePath' => self::RESOURCE_PATH . '/attributes',
182  ],
183  'soap' => [
184  'service' => self::SERVICE_NAME,
185  'serviceVersion' => self::SERVICE_VERSION,
186  'operation' => self::SERVICE_NAME . 'Assign',
187  ],
188  ];
189  }
190 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$attributes
Definition: matrix.phtml:13