|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types = 1); |
| 7 | + |
| 8 | +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 9 | + |
| 10 | +$defaultAttributeSet = $objectManager->get(Magento\Eav\Model\Config::class) |
| 11 | + ->getEntityType('catalog_product') |
| 12 | + ->getDefaultAttributeSetId(); |
| 13 | + |
| 14 | +$productRepository = $objectManager->create( |
| 15 | + \Magento\Catalog\Api\ProductRepositoryInterface::class |
| 16 | +); |
| 17 | + |
| 18 | +$categoryLinkRepository = $objectManager->create( |
| 19 | + \Magento\Catalog\Api\CategoryLinkRepositoryInterface::class, |
| 20 | + [ |
| 21 | + 'productRepository' => $productRepository |
| 22 | + ] |
| 23 | +); |
| 24 | + |
| 25 | +/** @var Magento\Catalog\Api\CategoryLinkManagementInterface $linkManagement */ |
| 26 | +$categoryLinkManagement = $objectManager->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class); |
| 27 | +$reflectionClass = new \ReflectionClass(get_class($categoryLinkManagement)); |
| 28 | +$properties = [ |
| 29 | + 'productRepository' => $productRepository, |
| 30 | + 'categoryLinkRepository' => $categoryLinkRepository |
| 31 | +]; |
| 32 | +foreach ($properties as $key => $value) { |
| 33 | + if ($reflectionClass->hasProperty($key)) { |
| 34 | + $reflectionProperty = $reflectionClass->getProperty($key); |
| 35 | + $reflectionProperty->setAccessible(true); |
| 36 | + $reflectionProperty->setValue($categoryLinkManagement, $value); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * After installation system has two categories: root one with ID:1 and Default category with ID:2 |
| 42 | + */ |
| 43 | +/** @var $category \Magento\Catalog\Model\Category */ |
| 44 | +$category = $objectManager->create(\Magento\Catalog\Model\Category::class); |
| 45 | +$category->isObjectNew(true); |
| 46 | +$category->setId(3) |
| 47 | + ->setName('Category 1') |
| 48 | + ->setParentId(2) |
| 49 | + ->setPath('1/2/3') |
| 50 | + ->setLevel(2) |
| 51 | + ->setAvailableSortBy('name') |
| 52 | + ->setDefaultSortBy('name') |
| 53 | + ->setIsActive(true) |
| 54 | + ->setPosition(1) |
| 55 | + ->save(); |
| 56 | + |
| 57 | +$category = $objectManager->create(\Magento\Catalog\Model\Category::class); |
| 58 | +$category->isObjectNew(true); |
| 59 | +$category->setId(5) |
| 60 | + ->setName('Category 1.1') |
| 61 | + ->setParentId(3) |
| 62 | + ->setPath('1/2/3/4/5') |
| 63 | + ->setLevel(4) |
| 64 | + ->setAvailableSortBy('name') |
| 65 | + ->setDefaultSortBy('name') |
| 66 | + ->setIsActive(true) |
| 67 | + ->setIsAnchor(true) |
| 68 | + ->setPosition(1) |
| 69 | + ->save(); |
| 70 | + |
| 71 | +/** @var $product \Magento\Catalog\Model\Product */ |
| 72 | +$product = $objectManager->create(\Magento\Catalog\Model\Product::class); |
| 73 | +$product->isObjectNew(true); |
| 74 | +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) |
| 75 | + ->setAttributeSetId($defaultAttributeSet) |
| 76 | + ->setStoreId(1) |
| 77 | + ->setWebsiteIds([1]) |
| 78 | + ->setName('Simple Product') |
| 79 | + ->setSku('simple') |
| 80 | + ->setPrice(10) |
| 81 | + ->setWeight(18) |
| 82 | + ->setStockData(['use_config_manage_stock' => 0]) |
| 83 | + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 84 | + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) |
| 85 | + ->save(); |
| 86 | + |
| 87 | +$categoryLinkManagement->assignProductToCategories( |
| 88 | + $product->getSku(), |
| 89 | + [5] |
| 90 | +); |
0 commit comments