-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathAttributeSetManagementTest.php
236 lines (209 loc) · 7.92 KB
/
AttributeSetManagementTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Api;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
class AttributeSetManagementTest extends WebapiAbstract
{
/**
* @var array
*/
private $createServiceInfo;
protected function setUp(): void
{
$this->createServiceInfo = [
'rest' => [
'resourcePath' => '/V1/products/attribute-sets',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => 'catalogAttributeSetManagementV1',
'serviceVersion' => 'V1',
'operation' => 'catalogAttributeSetManagementV1Create',
],
];
}
public function testCreate()
{
$entityTypeCode = 'catalog_product';
$entityType = $this->getEntityTypeByCode($entityTypeCode);
$attributeSetName = 'new_attribute_set';
$arguments = [
'attributeSet' => [
'attribute_set_name' => $attributeSetName,
'sort_order' => 500,
],
'skeletonId' => $entityType->getDefaultAttributeSetId(),
];
$result = $this->_webApiCall($this->createServiceInfo, $arguments);
$this->assertNotNull($result);
$attributeSet = $this->getAttributeSetByName($attributeSetName);
$this->assertNotNull($attributeSet);
$this->assertEquals($attributeSet->getId(), $result['attribute_set_id']);
$this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
$this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
$this->assertEquals($attributeSet->getEntityTypeId(), $entityType->getId());
$this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
$this->assertEquals($attributeSet->getSortOrder(), 500);
// Clean up database
$attributeSet->delete();
}
/**
*/
public function testCreateThrowsExceptionIfGivenAttributeSetAlreadyHasId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Invalid value');
$entityTypeCode = 'catalog_product';
$entityType = $this->getEntityTypeByCode($entityTypeCode);
$attributeSetName = 'new_attribute_set';
$arguments = [
'attributeSet' => [
'attribute_set_id' => 1,
'attribute_set_name' => $attributeSetName,
'sort_order' => 100,
],
'skeletonId' => $entityType->getDefaultAttributeSetId(),
];
$this->_webApiCall($this->createServiceInfo, $arguments);
}
/**
*/
public function testCreateThrowsExceptionIfGivenSkeletonIdIsInvalid()
{
$this->expectException(\Exception::class);
$attributeSetName = 'new_attribute_set';
$arguments = [
'attributeSet' => [
'attribute_set_name' => $attributeSetName,
'sort_order' => 200,
],
'skeletonId' => 0,
];
$this->_webApiCall($this->createServiceInfo, $arguments);
$this->expectExceptionMessage(
"The attribute set couldn't be created because it's based on a non-existing attribute set."
);
}
/**
*/
public function testCreateThrowsExceptionIfGivenSkeletonIdHasWrongEntityType()
{
$this->expectException(\Exception::class);
$attributeSetName = 'new_attribute_set';
$arguments = [
'attributeSet' => [
'attribute_set_name' => $attributeSetName,
'sort_order' => 200,
],
'skeletonId' => 7,
];
$this->_webApiCall($this->createServiceInfo, $arguments);
$this->expectExceptionMessage(
"The attribute set couldn't be created because it's based on a non-product attribute set."
);
}
/**
*/
public function testCreateThrowsExceptionIfGivenSkeletonAttributeSetDoesNotExist()
{
$this->expectException(\Exception::class);
$attributeSetName = 'new_attribute_set';
$arguments = [
'attributeSet' => [
'attribute_set_name' => $attributeSetName,
'sort_order' => 300,
],
'skeletonId' => 9999,
];
$this->_webApiCall($this->createServiceInfo, $arguments);
$this->expectExceptionMessage(
"The attribute set couldn't be created because it's based on a non-existing attribute set."
);
}
/**
*/
public function testCreateThrowsExceptionIfAttributeSetNameIsEmpty()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The attribute set name is empty. Enter the name and try again.');
$entityTypeCode = 'catalog_product';
$entityType = $this->getEntityTypeByCode($entityTypeCode);
$attributeSetName = '';
$arguments = [
'attributeSet' => [
'attribute_set_name' => $attributeSetName,
'sort_order' => 500,
],
'skeletonId' => $entityType->getDefaultAttributeSetId(),
];
$this->_webApiCall($this->createServiceInfo, $arguments);
}
public function testCreateThrowsExceptionIfAttributeSetWithGivenNameAlreadyExists()
{
$entityTypeCode = 'catalog_product';
$entityType = $this->getEntityTypeByCode($entityTypeCode);
$attributeSetName = 'Default';
$expectedMessage = 'A "Default" attribute set name already exists. Create a new name and try again.';
$arguments = [
'attributeSet' => [
'attribute_set_name' => $attributeSetName,
'sort_order' => 550,
],
'skeletonId' => $entityType->getDefaultAttributeSetId(),
];
try {
$this->_webApiCall($this->createServiceInfo, $arguments);
$this->fail("Expected exception");
} catch (\SoapFault $e) {
$this->assertStringContainsString(
$expectedMessage,
$e->getMessage(),
"SoapFault does not contain expected message."
);
} catch (\Exception $e) {
$errorObj = $this->processRestExceptionResult($e);
$this->assertEquals(
$expectedMessage,
$errorObj['message']
);
$this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
}
}
/**
* Retrieve attribute set based on given name.
* This utility methods assumes that there is only one attribute set with given name,
*
* @param string $attributeSetName
* @return \Magento\Eav\Model\Entity\Attribute\Set|null
*/
protected function getAttributeSetByName($attributeSetName)
{
$objectManager = Bootstrap::getObjectManager();
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class)
->load($attributeSetName, 'attribute_set_name');
if ($attributeSet->getId() === null) {
return null;
}
return $attributeSet;
}
/**
* Retrieve entity type based on given code.
*
* @param string $entityTypeCode
* @return \Magento\Eav\Model\Entity\Type|null
*/
protected function getEntityTypeByCode($entityTypeCode)
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Eav\Model\Entity\Type $entityType */
$entityType = $objectManager->create(\Magento\Eav\Model\Config::class)
->getEntityType($entityTypeCode);
return $entityType;
}
}