8
8
namespace Magento \Catalog \Api ;
9
9
10
10
use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
11
12
use Magento \Catalog \Model \Product \Attribute \Source \Status ;
12
13
use Magento \Catalog \Model \Product \Type ;
13
14
use Magento \Catalog \Model \Product \Visibility ;
14
15
use Magento \Catalog \Model \ResourceModel \Product \Website \Link ;
15
16
use Magento \Eav \Model \Config ;
17
+ use Magento \Framework \Exception \NoSuchEntityException ;
16
18
use Magento \Framework \ObjectManagerInterface ;
17
19
use Magento \Framework \Registry ;
18
20
use Magento \Framework \Webapi \Rest \Request ;
24
26
* Tests for products creation for all store views.
25
27
*
26
28
* @magentoAppIsolation enabled
29
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
30
*/
28
31
class ProductRepositoryAllStoreViewsTest extends WebapiAbstract
29
32
{
@@ -55,6 +58,7 @@ class ProductRepositoryAllStoreViewsTest extends WebapiAbstract
55
58
* @var Link
56
59
*/
57
60
private $ productWebsiteLink ;
61
+
58
62
/**
59
63
* @var Config
60
64
*/
@@ -87,9 +91,11 @@ protected function tearDown(): void
87
91
{
88
92
$ this ->registry ->unregister ('isSecureArea ' );
89
93
$ this ->registry ->register ('isSecureArea ' , true );
90
- $ this ->productRepository ->delete (
91
- $ this ->productRepository ->get ($ this ->productSku )
92
- );
94
+ try {
95
+ $ this ->productRepository ->deleteById ($ this ->productSku );
96
+ } catch (NoSuchEntityException $ e ) {
97
+ //already deleted
98
+ }
93
99
$ this ->registry ->unregister ('isSecureArea ' );
94
100
$ this ->registry ->register ('isSecureArea ' , false );
95
101
@@ -98,6 +104,7 @@ protected function tearDown(): void
98
104
99
105
/**
100
106
* @magentoApiDataFixture Magento/Catalog/_files/category.php
107
+ * @return void
101
108
*/
102
109
public function testCreateProduct (): void
103
110
{
@@ -110,6 +117,7 @@ public function testCreateProduct(): void
110
117
/**
111
118
* @magentoApiDataFixture Magento/Catalog/_files/category.php
112
119
* @magentoApiDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
120
+ * @return void
113
121
*/
114
122
public function testCreateProductOnMultipleWebsites (): void
115
123
{
@@ -120,12 +128,12 @@ public function testCreateProductOnMultipleWebsites(): void
120
128
}
121
129
122
130
/**
123
- * Saves Product via API.
131
+ * Saves product via API.
124
132
*
125
- * @param $product
133
+ * @param array $product
126
134
* @return array
127
135
*/
128
- private function saveProduct ($ product ): array
136
+ private function saveProduct (array $ product ): array
129
137
{
130
138
$ serviceInfo = [
131
139
'rest ' => ['resourcePath ' =>self ::PRODUCTS_RESOURCE_PATH , 'httpMethod ' => Request::HTTP_METHOD_POST ],
@@ -146,22 +154,22 @@ private function saveProduct($product): array
146
154
*/
147
155
private function getProductData (): array
148
156
{
149
- $ setId =(int )$ this ->eavConfig ->getEntityType (ProductAttributeInterface::ENTITY_TYPE_CODE )
157
+ $ setId = (int )$ this ->eavConfig ->getEntityType (ProductAttributeInterface::ENTITY_TYPE_CODE )
150
158
->getDefaultAttributeSetId ();
151
159
152
160
return [
153
- ' sku ' => $ this ->productSku ,
154
- ' name ' => 'simple ' ,
155
- ' type_id ' => Type::TYPE_SIMPLE ,
156
- ' weight ' => 1 ,
157
- ' attribute_set_id ' => $ setId ,
158
- ' price ' => 10 ,
159
- ' status ' => Status::STATUS_ENABLED ,
160
- ' visibility ' => Visibility::VISIBILITY_BOTH ,
161
- ' extension_attributes ' => [
161
+ ProductInterface:: SKU => $ this ->productSku ,
162
+ ProductInterface:: NAME => 'simple ' ,
163
+ ProductInterface:: TYPE_ID => Type::TYPE_SIMPLE ,
164
+ ProductInterface:: WEIGHT => 1 ,
165
+ ProductInterface:: ATTRIBUTE_SET_ID => $ setId ,
166
+ ProductInterface:: PRICE => 10 ,
167
+ ProductInterface:: STATUS => Status::STATUS_ENABLED ,
168
+ ProductInterface:: VISIBILITY => Visibility::VISIBILITY_BOTH ,
169
+ ProductInterface:: EXTENSION_ATTRIBUTES_KEY => [
162
170
'stock_item ' => ['is_in_stock ' => true , 'qty ' => 1000 ]
163
171
],
164
- ' custom_attributes ' => [
172
+ ProductInterface:: CUSTOM_ATTRIBUTES => [
165
173
['attribute_code ' => 'url_key ' , 'value ' => 'simple ' ],
166
174
['attribute_code ' => 'tax_class_id ' , 'value ' => 2 ],
167
175
['attribute_code ' => 'category_ids ' , 'value ' => [333 ]]
@@ -219,8 +227,7 @@ private function assertProductData(array $productData, array $resultData, array
219
227
private function getAllWebsiteIds (): array
220
228
{
221
229
$ websiteIds = [];
222
- $ websites = $ this ->storeManager ->getWebsites ();
223
- foreach ($ websites as $ website ) {
230
+ foreach ($ this ->storeManager ->getWebsites () as $ website ) {
224
231
$ websiteIds [] = $ website ->getId ();
225
232
}
226
233
0 commit comments