|
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
8 |
| -// phpcs:ignore Magento2.Security.IncludeFile |
9 | 8 | require __DIR__ . '/../../Catalog/_files/attribute_set_based_on_default_set.php';
|
10 |
| -// phpcs:ignore Magento2.Security.IncludeFile |
11 | 9 | require __DIR__ . '/../../Catalog/_files/categories.php';
|
12 | 10 |
|
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
| 13 | +use Magento\Catalog\Setup\CategorySetup; |
| 14 | +use Magento\Eav\Model\Config; |
| 15 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 16 | +use Magento\Indexer\Model\Indexer; |
| 17 | +use Magento\Indexer\Model\Indexer\Collection as IndexerCollection; |
13 | 18 | use Magento\TestFramework\Helper\Bootstrap;
|
14 | 19 | use Magento\Eav\Api\AttributeRepositoryInterface;
|
15 | 20 |
|
16 |
| -$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class); |
17 |
| -$attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable'); |
18 |
| - |
19 |
| -$eavConfig->clear(); |
20 |
| - |
21 |
| -$attribute1 = $eavConfig->getAttribute('catalog_product', ' second_test_configurable'); |
| 21 | +$objectManager = Bootstrap::getObjectManager(); |
| 22 | +/** @var Config $eavConfig */ |
| 23 | +$eavConfig = $objectManager->get(Config::class); |
| 24 | +/** @var AttributeRepositoryInterface $attributeRepository */ |
| 25 | +$attributeRepository = $objectManager->create(AttributeRepositoryInterface::class); |
| 26 | +/** @var CategorySetup $installer */ |
| 27 | +$installer = $objectManager->create(CategorySetup::class); |
22 | 28 | $eavConfig->clear();
|
23 | 29 |
|
24 |
| -/** @var $installer \Magento\Catalog\Setup\CategorySetup */ |
25 |
| -$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class); |
26 |
| - |
| 30 | +$attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable'); |
27 | 31 | if (!$attribute->getId()) {
|
28 |
| - |
29 |
| - /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ |
30 |
| - $attribute = Bootstrap::getObjectManager()->create( |
31 |
| - \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class |
32 |
| - ); |
33 |
| - |
34 |
| - /** @var AttributeRepositoryInterface $attributeRepository */ |
35 |
| - $attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class); |
36 |
| - |
37 |
| - $attribute->setData( |
38 |
| - [ |
39 |
| - 'attribute_code' => 'test_configurable', |
40 |
| - 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), |
41 |
| - 'is_global' => 1, |
42 |
| - 'is_user_defined' => 1, |
43 |
| - 'frontend_input' => 'select', |
44 |
| - 'is_unique' => 0, |
45 |
| - 'is_required' => 0, |
46 |
| - 'is_searchable' => 1, |
47 |
| - 'is_visible_in_advanced_search' => 1, |
48 |
| - 'is_comparable' => 1, |
49 |
| - 'is_filterable' => 1, |
50 |
| - 'is_filterable_in_search' => 1, |
51 |
| - 'is_used_for_promo_rules' => 0, |
52 |
| - 'is_html_allowed_on_front' => 1, |
53 |
| - 'is_visible_on_front' => 1, |
54 |
| - 'used_in_product_listing' => 1, |
55 |
| - 'used_for_sort_by' => 1, |
56 |
| - 'frontend_label' => ['Test Configurable'], |
57 |
| - 'backend_type' => 'int', |
58 |
| - 'option' => [ |
59 |
| - 'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']], |
60 |
| - 'order' => ['option_0' => 1, 'option_1' => 2], |
61 |
| - ], |
62 |
| - 'default_value' => 'option_0' |
63 |
| - ] |
64 |
| - ); |
65 |
| - |
| 32 | + /** @var $attribute Attribute */ |
| 33 | + $attribute->setData([ |
| 34 | + 'attribute_code' => 'test_configurable', |
| 35 | + 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), |
| 36 | + 'is_global' => 1, |
| 37 | + 'is_user_defined' => 1, |
| 38 | + 'frontend_input' => 'select', |
| 39 | + 'is_unique' => 0, |
| 40 | + 'is_required' => 0, |
| 41 | + 'is_searchable' => 0, |
| 42 | + 'is_visible_in_advanced_search' => 0, |
| 43 | + 'is_comparable' => 1, |
| 44 | + 'is_filterable' => 1, |
| 45 | + 'is_filterable_in_search' => 1, |
| 46 | + 'is_used_for_promo_rules' => 0, |
| 47 | + 'is_html_allowed_on_front' => 1, |
| 48 | + 'is_visible_on_front' => 1, |
| 49 | + 'used_in_product_listing' => 1, |
| 50 | + 'used_for_sort_by' => 1, |
| 51 | + 'frontend_label' => ['Test Configurable'], |
| 52 | + 'backend_type' => 'int', |
| 53 | + 'option' => [ |
| 54 | + 'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']], |
| 55 | + 'order' => ['option_0' => 1, 'option_1' => 2], |
| 56 | + ], |
| 57 | + 'default_value' => 'option_0' |
| 58 | + ]); |
66 | 59 | $attributeRepository->save($attribute);
|
67 | 60 |
|
68 | 61 | /* Assign attribute to attribute set */
|
69 | 62 | $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
|
70 | 63 | }
|
71 |
| -// create a second attribute |
72 |
| -if (!$attribute1->getId()) { |
73 |
| - |
74 |
| - /** @var $attribute1 \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ |
75 |
| - $attribute1 = Bootstrap::getObjectManager()->create( |
76 |
| - \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class |
77 |
| - ); |
78 |
| - |
79 |
| - /** @var AttributeRepositoryInterface $attributeRepository */ |
80 |
| - $attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class); |
81 | 64 |
|
82 |
| - $attribute1->setData( |
83 |
| - [ |
84 |
| - 'attribute_code' => 'second_test_configurable', |
85 |
| - 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), |
86 |
| - 'is_global' => 1, |
87 |
| - 'is_user_defined' => 1, |
88 |
| - 'frontend_input' => 'select', |
89 |
| - 'is_unique' => 0, |
90 |
| - 'is_required' => 0, |
91 |
| - 'is_searchable' => 1, |
92 |
| - 'is_visible_in_advanced_search' => 1, |
93 |
| - 'is_comparable' => 1, |
94 |
| - 'is_filterable' => 1, |
95 |
| - 'is_filterable_in_search' => 1, |
96 |
| - 'is_used_for_promo_rules' => 0, |
97 |
| - 'is_html_allowed_on_front' => 1, |
98 |
| - 'is_visible_on_front' => 1, |
99 |
| - 'used_in_product_listing' => 1, |
100 |
| - 'used_for_sort_by' => 1, |
101 |
| - 'frontend_label' => ['Second Test Configurable'], |
102 |
| - 'backend_type' => 'int', |
103 |
| - 'option' => [ |
104 |
| - 'value' => ['option_0' => ['Option 3'], 'option_1' => ['Option 4']], |
105 |
| - 'order' => ['option_0' => 1, 'option_1' => 2], |
106 |
| - ], |
107 |
| - 'default' => ['option_0'] |
108 |
| - ] |
109 |
| - ); |
110 |
| - |
111 |
| - $attributeRepository->save($attribute1); |
| 65 | +// create a second attribute |
| 66 | +/** @var Attribute $secondAttribute */ |
| 67 | +$secondAttribute = $eavConfig->getAttribute('catalog_product', ' second_test_configurable'); |
| 68 | +if (!$secondAttribute->getId()) { |
| 69 | + $secondAttribute->setData([ |
| 70 | + 'attribute_code' => 'second_test_configurable', |
| 71 | + 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), |
| 72 | + 'is_global' => 1, |
| 73 | + 'is_user_defined' => 1, |
| 74 | + 'frontend_input' => 'select', |
| 75 | + 'is_unique' => 0, |
| 76 | + 'is_required' => 0, |
| 77 | + 'is_searchable' => 0, |
| 78 | + 'is_visible_in_advanced_search' => 0, |
| 79 | + 'is_comparable' => 1, |
| 80 | + 'is_filterable' => 1, |
| 81 | + 'is_filterable_in_search' => 1, |
| 82 | + 'is_used_for_promo_rules' => 0, |
| 83 | + 'is_html_allowed_on_front' => 1, |
| 84 | + 'is_visible_on_front' => 1, |
| 85 | + 'used_in_product_listing' => 1, |
| 86 | + 'used_for_sort_by' => 1, |
| 87 | + 'frontend_label' => ['Second Test Configurable'], |
| 88 | + 'backend_type' => 'int', |
| 89 | + 'option' => [ |
| 90 | + 'value' => ['option_0' => ['Option 3'], 'option_1' => ['Option 4']], |
| 91 | + 'order' => ['option_0' => 1, 'option_1' => 2], |
| 92 | + ], |
| 93 | + 'default' => ['option_0'] |
| 94 | + ]); |
| 95 | + $attributeRepository->save($secondAttribute); |
112 | 96 |
|
113 | 97 | /* Assign attribute to attribute set */
|
114 | 98 | $installer->addAttributeToGroup(
|
115 | 99 | 'catalog_product',
|
116 | 100 | $attributeSet->getId(),
|
117 | 101 | $attributeSet->getDefaultGroupId(),
|
118 |
| - $attribute1->getId() |
| 102 | + $secondAttribute->getId() |
119 | 103 | );
|
120 | 104 | }
|
121 | 105 |
|
122 | 106 | $eavConfig->clear();
|
123 | 107 |
|
124 |
| -/** @var \Magento\Framework\ObjectManagerInterface $objectManager */ |
125 |
| -$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
126 |
| - |
127 |
| -/** @var $productRepository \Magento\Catalog\Api\ProductRepositoryInterface */ |
128 |
| -$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 108 | +/** @var ProductRepositoryInterface $productRepository */ |
| 109 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
129 | 110 | $productsWithNewAttributeSet = ['simple', '12345', 'simple-4'];
|
130 | 111 |
|
131 | 112 | foreach ($productsWithNewAttributeSet as $sku) {
|
|
139 | 120 | 'is_in_stock' => 1]
|
140 | 121 | );
|
141 | 122 | $productRepository->save($product);
|
142 |
| - } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
| 123 | + } catch (NoSuchEntityException $e) { |
143 | 124 |
|
144 | 125 | }
|
145 | 126 | }
|
146 |
| -/** @var \Magento\Indexer\Model\Indexer\Collection $indexerCollection */ |
147 |
| -$indexerCollection = Bootstrap::getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class); |
148 |
| -$indexerCollection->load(); |
149 |
| -/** @var \Magento\Indexer\Model\Indexer $indexer */ |
| 127 | + |
| 128 | +/** @var IndexerCollection $indexerCollection */ |
| 129 | +$indexerCollection = $objectManager->get(IndexerCollection::class)->load(); |
| 130 | +/** @var Indexer $indexer */ |
150 | 131 | foreach ($indexerCollection->getItems() as $indexer) {
|
151 | 132 | $indexer->reindexAll();
|
152 | 133 | }
|
0 commit comments