|
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
| 8 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
8 | 9 | use Magento\Catalog\Api\Data\ProductInterfaceFactory;
|
9 | 10 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
10 | 11 | use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
|
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\Store\Model\Store;
|
| 16 | +use Magento\Store\Model\StoreManagerInterface; |
15 | 17 | use Magento\TestFramework\Helper\Bootstrap;
|
16 | 18 |
|
17 | 19 | $objectManager = Bootstrap::getObjectManager();
|
| 20 | +$storeManager = $objectManager->get(StoreManagerInterface::class); |
18 | 21 | $categoryFactory = $objectManager->get(CategoryInterfaceFactory::class);
|
19 | 22 | $productFactory = $objectManager->get(ProductInterfaceFactory::class);
|
20 | 23 | $productRepository = $objectManager->get(ProductRepositoryInterface::class);
|
| 24 | +$categoryRepository = $objectManager->get(CategoryRepositoryInterface::class); |
| 25 | +$currentStoreId = $storeManager->getStore()->getId(); |
21 | 26 |
|
22 |
| -$category = $categoryFactory->create(); |
23 |
| -$category->isObjectNew(true); |
24 |
| -$category->setName('Category 999') |
25 |
| - ->setParentId(2)->setPath('1/2') |
26 |
| - ->setLevel(2) |
27 |
| - ->setAvailableSortBy('name') |
28 |
| - ->setDefaultSortBy('name') |
29 |
| - ->setIsActive(true) |
30 |
| - ->setPosition(1) |
31 |
| - ->setStoreId(Store::DEFAULT_STORE_ID) |
32 |
| - ->setAvailableSortBy(['position']) |
33 |
| - ->save(); |
| 27 | +try { |
| 28 | + $storeManager->setCurrentStore(Store::DEFAULT_STORE_ID); |
| 29 | + $category = $categoryFactory->create(); |
| 30 | + $category->isObjectNew(true); |
| 31 | + $category->setName('Category 999') |
| 32 | + ->setParentId(2) |
| 33 | + ->setLevel(2) |
| 34 | + ->setAvailableSortBy('name') |
| 35 | + ->setDefaultSortBy('name') |
| 36 | + ->setIsActive(true) |
| 37 | + ->setPosition(1); |
| 38 | + $category = $categoryRepository->save($category); |
| 39 | +} finally { |
| 40 | + $storeManager->setCurrentStore($currentStoreId); |
| 41 | +} |
34 | 42 |
|
35 | 43 | $product = $productFactory->create();
|
36 | 44 | $product->setTypeId(Type::TYPE_SIMPLE)
|
|
0 commit comments