Skip to content

Commit 43e5b5c

Browse files
MC-36960: Create automated test for "Create product for "all" store views using API service"
1 parent 4c1243c commit 43e5b5c

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryAllStoreViewsTest.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
namespace Magento\Catalog\Api;
99

1010
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1213
use Magento\Catalog\Model\Product\Type;
1314
use Magento\Catalog\Model\Product\Visibility;
1415
use Magento\Catalog\Model\ResourceModel\Product\Website\Link;
1516
use Magento\Eav\Model\Config;
17+
use Magento\Framework\Exception\NoSuchEntityException;
1618
use Magento\Framework\ObjectManagerInterface;
1719
use Magento\Framework\Registry;
1820
use Magento\Framework\Webapi\Rest\Request;
@@ -24,6 +26,7 @@
2426
* Tests for products creation for all store views.
2527
*
2628
* @magentoAppIsolation enabled
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2730
*/
2831
class ProductRepositoryAllStoreViewsTest extends WebapiAbstract
2932
{
@@ -55,6 +58,7 @@ class ProductRepositoryAllStoreViewsTest extends WebapiAbstract
5558
* @var Link
5659
*/
5760
private $productWebsiteLink;
61+
5862
/**
5963
* @var Config
6064
*/
@@ -87,9 +91,11 @@ protected function tearDown(): void
8791
{
8892
$this->registry->unregister('isSecureArea');
8993
$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+
}
9399
$this->registry->unregister('isSecureArea');
94100
$this->registry->register('isSecureArea', false);
95101

@@ -98,6 +104,7 @@ protected function tearDown(): void
98104

99105
/**
100106
* @magentoApiDataFixture Magento/Catalog/_files/category.php
107+
* @return void
101108
*/
102109
public function testCreateProduct(): void
103110
{
@@ -110,6 +117,7 @@ public function testCreateProduct(): void
110117
/**
111118
* @magentoApiDataFixture Magento/Catalog/_files/category.php
112119
* @magentoApiDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
120+
* @return void
113121
*/
114122
public function testCreateProductOnMultipleWebsites(): void
115123
{
@@ -120,12 +128,12 @@ public function testCreateProductOnMultipleWebsites(): void
120128
}
121129

122130
/**
123-
* Saves Product via API.
131+
* Saves product via API.
124132
*
125-
* @param $product
133+
* @param array $product
126134
* @return array
127135
*/
128-
private function saveProduct($product): array
136+
private function saveProduct(array $product): array
129137
{
130138
$serviceInfo = [
131139
'rest' => ['resourcePath' =>self::PRODUCTS_RESOURCE_PATH, 'httpMethod' => Request::HTTP_METHOD_POST],
@@ -146,22 +154,22 @@ private function saveProduct($product): array
146154
*/
147155
private function getProductData(): array
148156
{
149-
$setId =(int)$this->eavConfig->getEntityType(ProductAttributeInterface::ENTITY_TYPE_CODE)
157+
$setId = (int)$this->eavConfig->getEntityType(ProductAttributeInterface::ENTITY_TYPE_CODE)
150158
->getDefaultAttributeSetId();
151159

152160
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 => [
162170
'stock_item' => ['is_in_stock' => true, 'qty' => 1000]
163171
],
164-
'custom_attributes' => [
172+
ProductInterface::CUSTOM_ATTRIBUTES => [
165173
['attribute_code' => 'url_key', 'value' => 'simple'],
166174
['attribute_code' => 'tax_class_id', 'value' => 2],
167175
['attribute_code' => 'category_ids', 'value' => [333]]
@@ -219,8 +227,7 @@ private function assertProductData(array $productData, array $resultData, array
219227
private function getAllWebsiteIds(): array
220228
{
221229
$websiteIds = [];
222-
$websites = $this->storeManager->getWebsites();
223-
foreach ($websites as $website) {
230+
foreach ($this->storeManager->getWebsites() as $website) {
224231
$websiteIds[] = $website->getId();
225232
}
226233

0 commit comments

Comments
 (0)