|
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
8 |
| -use Magento\Catalog\Api\ProductRepositoryInterface; |
9 |
| -use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
10 |
| -use Magento\Catalog\Setup\CategorySetup; |
11 |
| -use Magento\Eav\Model\Config; |
12 |
| -use Magento\Framework\Exception\NoSuchEntityException; |
13 |
| -use Magento\Indexer\Model\Indexer; |
14 |
| -use Magento\Indexer\Model\Indexer\Collection as IndexerCollection; |
15 | 8 | use Magento\TestFramework\Helper\Bootstrap;
|
16 | 9 | use Magento\Eav\Api\AttributeRepositoryInterface;
|
| 10 | +use Magento\TestFramework\Helper\CacheCleaner; |
17 | 11 | use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
18 |
| -use Magento\TestFramework\Eav\Model\GetAttributeSetByName; |
19 | 12 |
|
20 | 13 | Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/attribute_set_based_on_default_set.php');
|
21 | 14 | Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/categories.php');
|
22 | 15 |
|
23 |
| -$objectManager = Bootstrap::getObjectManager(); |
24 |
| -/** @var GetAttributeSetByName $getAttributeSetByName */ |
25 |
| -$getAttributeSetByName = $objectManager->get(GetAttributeSetByName::class); |
26 |
| -$attributeSet = $getAttributeSetByName->execute('second_attribute_set'); |
27 |
| -/** @var Config $eavConfig */ |
28 |
| -$eavConfig = $objectManager->get(Config::class); |
29 |
| -/** @var AttributeRepositoryInterface $attributeRepository */ |
30 |
| -$attributeRepository = $objectManager->create(AttributeRepositoryInterface::class); |
31 |
| -/** @var CategorySetup $installer */ |
32 |
| -$installer = $objectManager->create(CategorySetup::class); |
| 16 | +$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class); |
| 17 | +$attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable'); |
| 18 | + |
33 | 19 | $eavConfig->clear();
|
34 | 20 |
|
35 |
| -$attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable'); |
| 21 | +$attribute1 = $eavConfig->getAttribute('catalog_product', ' second_test_configurable'); |
| 22 | +$eavConfig->clear(); |
| 23 | + |
| 24 | +/** @var $installer \Magento\Catalog\Setup\CategorySetup */ |
| 25 | +$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class); |
| 26 | + |
36 | 27 | if (!$attribute->getId()) {
|
37 |
| - /** @var $attribute Attribute */ |
38 |
| - $attribute->setData([ |
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' => 0, |
47 |
| - 'is_visible_in_advanced_search' => 0, |
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 |
| - ]); |
| 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 | + |
64 | 66 | $attributeRepository->save($attribute);
|
65 | 67 |
|
66 | 68 | /* Assign attribute to attribute set */
|
67 | 69 | $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
|
68 | 70 | }
|
69 |
| - |
70 | 71 | // create a second attribute
|
71 |
| -/** @var Attribute $secondAttribute */ |
72 |
| -$secondAttribute = $eavConfig->getAttribute('catalog_product', ' second_test_configurable'); |
73 |
| -if (!$secondAttribute->getId()) { |
74 |
| - $secondAttribute->setData([ |
75 |
| - 'attribute_code' => 'second_test_configurable', |
76 |
| - 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), |
77 |
| - 'is_global' => 1, |
78 |
| - 'is_user_defined' => 1, |
79 |
| - 'frontend_input' => 'select', |
80 |
| - 'is_unique' => 0, |
81 |
| - 'is_required' => 0, |
82 |
| - 'is_searchable' => 0, |
83 |
| - 'is_visible_in_advanced_search' => 0, |
84 |
| - 'is_comparable' => 1, |
85 |
| - 'is_filterable' => 1, |
86 |
| - 'is_filterable_in_search' => 1, |
87 |
| - 'is_used_for_promo_rules' => 0, |
88 |
| - 'is_html_allowed_on_front' => 1, |
89 |
| - 'is_visible_on_front' => 1, |
90 |
| - 'used_in_product_listing' => 1, |
91 |
| - 'used_for_sort_by' => 1, |
92 |
| - 'frontend_label' => ['Second Test Configurable'], |
93 |
| - 'backend_type' => 'int', |
94 |
| - 'option' => [ |
95 |
| - 'value' => ['option_0' => ['Option 3'], 'option_1' => ['Option 4']], |
96 |
| - 'order' => ['option_0' => 1, 'option_1' => 2], |
97 |
| - ], |
98 |
| - 'default' => ['option_0'] |
99 |
| - ]); |
100 |
| - $attributeRepository->save($secondAttribute); |
| 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 | + |
| 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); |
101 | 112 |
|
102 | 113 | /* Assign attribute to attribute set */
|
103 | 114 | $installer->addAttributeToGroup(
|
104 | 115 | 'catalog_product',
|
105 | 116 | $attributeSet->getId(),
|
106 | 117 | $attributeSet->getDefaultGroupId(),
|
107 |
| - $secondAttribute->getId() |
| 118 | + $attribute1->getId() |
108 | 119 | );
|
109 | 120 | }
|
110 | 121 |
|
111 | 122 | $eavConfig->clear();
|
112 | 123 |
|
113 |
| -/** @var ProductRepositoryInterface $productRepository */ |
114 |
| -$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 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); |
115 | 129 | $productsWithNewAttributeSet = ['simple', '12345', 'simple-4'];
|
116 | 130 |
|
117 | 131 | foreach ($productsWithNewAttributeSet as $sku) {
|
|
125 | 139 | 'is_in_stock' => 1]
|
126 | 140 | );
|
127 | 141 | $productRepository->save($product);
|
128 |
| - } catch (NoSuchEntityException $e) { |
| 142 | + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
129 | 143 |
|
130 | 144 | }
|
131 | 145 | }
|
| 146 | +CacheCleaner::cleanAll(); |
132 | 147 |
|
133 |
| -/** @var IndexerCollection $indexerCollection */ |
134 |
| -$indexerCollection = $objectManager->get(IndexerCollection::class)->load(); |
135 |
| -/** @var Indexer $indexer */ |
136 |
| -foreach ($indexerCollection->getItems() as $indexer) { |
137 |
| - $indexer->reindexAll(); |
138 |
| -} |
| 148 | +// Run from CLI due to some classes involved in reindex process have state which do not allow to reindex |
| 149 | +$appDir = dirname(Bootstrap::getInstance()->getAppTempDir()); |
| 150 | +$out = ''; |
| 151 | +// phpcs:ignore Magento2.Security.InsecureFunction |
| 152 | +exec("php -f {$appDir}/bin/magento indexer:reindex catalogsearch_fulltext", $out); |
0 commit comments