|
3 | 3 | * Copyright © Magento, Inc. All rights reserved. |
4 | 4 | * See COPYING.txt for license details. |
5 | 5 | */ |
| 6 | + |
6 | 7 | use Magento\Catalog\Api\ProductRepositoryInterface; |
7 | 8 | use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceIndexerProcessor; |
8 | 9 | use Magento\Catalog\Model\Product; |
9 | 10 | use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 11 | +use Magento\Catalog\Model\Product\Media\Config; |
10 | 12 | use Magento\Catalog\Model\Product\Type; |
11 | 13 | use Magento\Catalog\Model\Product\Visibility; |
12 | 14 | use Magento\Catalog\Setup\CategorySetup; |
|
15 | 17 | use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
16 | 18 | use Magento\Eav\Api\Data\AttributeOptionInterface; |
17 | 19 | use Magento\Framework\Api\Data\ImageContentInterface; |
| 20 | +use Magento\Framework\Filesystem; |
| 21 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
18 | 22 | use Magento\TestFramework\Helper\Bootstrap; |
19 | 23 | use Magento\TestFramework\Workaround\Override\Fixture\Resolver; |
| 24 | +use Magento\Framework\App\Filesystem\DirectoryList; |
20 | 25 |
|
21 | 26 | Resolver::getInstance()->requireDataFixture( |
22 | 27 | 'Magento/ConfigurableProduct/_files/configurable_attribute_first.php' |
|
25 | 30 | 'Magento/ConfigurableProduct/_files/configurable_attribute_second.php' |
26 | 31 | ); |
27 | 32 |
|
| 33 | +$objectManager = Bootstrap::getObjectManager(); |
| 34 | + |
28 | 35 | /** @var ProductRepositoryInterface $productRepository */ |
29 | | -$productRepository = Bootstrap::getObjectManager() |
30 | | - ->get(ProductRepositoryInterface::class); |
| 36 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
31 | 37 |
|
32 | 38 | /** @var $installer CategorySetup */ |
33 | | -$installer = Bootstrap::getObjectManager()->create(CategorySetup::class); |
| 39 | +$installer = $objectManager->create(CategorySetup::class); |
34 | 40 |
|
35 | 41 | /** @var \Magento\Eav\Model\Config $eavConfig */ |
36 | | -$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class); |
| 42 | +$eavConfig = $objectManager->get(\Magento\Eav\Model\Config::class); |
37 | 43 | $firstAttribute = $eavConfig->getAttribute(Product::ENTITY, 'test_configurable_first'); |
38 | 44 | $secondAttribute = $eavConfig->getAttribute(Product::ENTITY, 'test_configurable_second'); |
39 | 45 |
|
| 46 | +/** @var Config $config */ |
| 47 | +$config = $objectManager->get(Config::class); |
| 48 | + |
| 49 | +/** @var Filesystem $filesystem */ |
| 50 | +$filesystem = $objectManager->get(Filesystem::class); |
| 51 | + |
| 52 | +/** @var WriteInterface $mediaDirectory */ |
| 53 | +$mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 54 | +$mediaPath = $mediaDirectory->getAbsolutePath(); |
| 55 | +$baseTmpMediaPath = $config->getBaseTmpMediaPath(); |
| 56 | +$mediaDirectory->create($baseTmpMediaPath); |
| 57 | + |
40 | 58 | /* Create simple products per each option value*/ |
41 | 59 | /** @var AttributeOptionInterface[] $firstAttributeOptions */ |
42 | 60 | $firstAttributeOptions = $firstAttribute->getOptions(); |
|
48 | 66 | $firstAttributeValues = []; |
49 | 67 | $secondAttributeValues = []; |
50 | 68 | $testImagePath = __DIR__ . '/magento_image.jpg'; |
| 69 | +$mediaImage = $mediaPath . '/' . $baseTmpMediaPath . '/magento_image.jpg'; |
| 70 | +copy($testImagePath, $mediaImage); |
51 | 71 |
|
52 | 72 | array_shift($firstAttributeOptions); |
53 | 73 | array_shift($secondAttributeOptions); |
|
65 | 85 | $qty = 100; |
66 | 86 | $isInStock = 1; |
67 | 87 | } |
68 | | - $product = Bootstrap::getObjectManager()->create(Product::class); |
| 88 | + |
| 89 | + $image = '/m/a/magento_image.jpg'; |
| 90 | + |
| 91 | + $product = $objectManager->create(Product::class); |
69 | 92 | $product->setTypeId(Type::TYPE_SIMPLE) |
70 | 93 | ->setAttributeSetId($attributeSetId) |
71 | 94 | ->setWebsiteIds([1]) |
|
79 | 102 | ->setStockData( |
80 | 103 | ['use_config_manage_stock' => 1, 'qty' => $qty, 'is_qty_decimal' => 0, 'is_in_stock' => $isInStock] |
81 | 104 | ) |
82 | | - ->setImage('/m/a/magento_image.jpg') |
83 | | - ->setSmallImage('/m/a/magento_image.jpg') |
84 | | - ->setThumbnail('/m/a/magento_image.jpg') |
| 105 | + ->setImage($image) |
| 106 | + ->setSmallImage($image) |
| 107 | + ->setThumbnail($image) |
85 | 108 | ->setData( |
86 | 109 | 'media_gallery', |
87 | 110 | [ |
88 | 111 | 'images' => [ |
89 | 112 | [ |
90 | | - 'file' => '/m/a/magento_image.jpg', |
| 113 | + 'file' => $image, |
91 | 114 | 'position' => 1, |
92 | 115 | 'label' => 'Image Alt Text', |
93 | 116 | 'disabled' => 0, |
|
113 | 136 | foreach ($customAttributes as $attributeCode => $attributeValue) { |
114 | 137 | $product->setCustomAttributes($customAttributes); |
115 | 138 | } |
| 139 | + |
116 | 140 | $product = $productRepository->save($product); |
117 | 141 | $associatedProductIds[] = $product->getId(); |
118 | 142 |
|
119 | | - /** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */ |
120 | | - $stockItem = Bootstrap::getObjectManager()->create(Item::class); |
| 143 | + /** @var Item $stockItem */ |
| 144 | + $stockItem = $objectManager->create(Item::class); |
121 | 145 | $stockItem->load($product->getId(), 'product_id'); |
122 | 146 |
|
123 | 147 | if (!$stockItem->getProductId()) { |
|
135 | 159 | 'value_index' => $secondAttributeOption->getValue(), |
136 | 160 | ]; |
137 | 161 | } |
138 | | - |
139 | 162 | } |
140 | 163 |
|
141 | | -$indexerProcessor = Bootstrap::getObjectManager()->get(PriceIndexerProcessor::class); |
| 164 | +$indexerProcessor = $objectManager->get(PriceIndexerProcessor::class); |
142 | 165 | $indexerProcessor->reindexList($associatedProductIds, true); |
143 | 166 |
|
144 | 167 | /** @var $product Product */ |
145 | | -$product = Bootstrap::getObjectManager()->create(Product::class); |
| 168 | +$product = $objectManager->create(Product::class); |
146 | 169 |
|
147 | 170 | /** @var Factory $optionsFactory */ |
148 | | -$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class); |
| 171 | +$optionsFactory = $objectManager->create(Factory::class); |
149 | 172 |
|
150 | 173 | $configurableAttributesData = [ |
151 | 174 | [ |
|
180 | 203 | ->setSku('configurable_12345') |
181 | 204 | ->setVisibility(Visibility::VISIBILITY_BOTH) |
182 | 205 | ->setStatus(Status::STATUS_ENABLED) |
183 | | - ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]); |
| 206 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) |
| 207 | + ->addImageToMediaGallery( |
| 208 | + $mediaImage, |
| 209 | + ['image', 'small_image', 'thumbnail'], |
| 210 | + false, |
| 211 | + false |
| 212 | + ); |
184 | 213 | $productRepository->cleanCache(); |
185 | 214 | $product = $productRepository->save($product); |
186 | 215 |
|
187 | | -$indexerProcessor = Bootstrap::getObjectManager()->get(PriceIndexerProcessor::class); |
| 216 | +$indexerProcessor = $objectManager->get(PriceIndexerProcessor::class); |
188 | 217 | $indexerProcessor->reindexRow($product->getId(), true); |
0 commit comments