Skip to content

Commit dfdbd9c

Browse files
committed
MAGETWO-69862: Error on the medium profile during repeated generation
1 parent 711ad81 commit dfdbd9c

File tree

4 files changed

+31
-62
lines changed

4 files changed

+31
-62
lines changed

setup/src/Magento/Setup/Fixtures/ConfigurableProductsFixture.php

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Model\Product\Visibility;
1111
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
1212
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
13+
use Magento\Eav\Model\Entity\Attribute;
1314
use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
1415
use Magento\Framework\Exception\ValidatorException;
1516
use Magento\Setup\Model\DataGenerator;
@@ -518,9 +519,14 @@ private function getConfigurableProductConfig()
518519
$attributes = count($attributes);
519520
} elseif ($attributes && $options) {
520521
$attributes = (int)$attributes;
521-
// process dynamic attribute sets
522-
$attributeSet = $this->getAttributeSet($attributes, $options, $config['swatches']);
522+
// convert attributes and options to array for process custom attribute set creation
523+
$attributesData = array_map(function ($options) use ($config) {
524+
return ['options' => $options, 'swatches' => $config['swatches']];
525+
}, array_fill(0, $attributes, $options));
526+
527+
$attributeSet = $this->getCustomAttributeSet($attributesData);
523528
}
529+
524530
// do not process if any required option is missed
525531
if (count(array_filter([$attributeSet, $attributes, $options])) !== 3) {
526532
unset($configurableProductConfig[$i]);
@@ -624,36 +630,6 @@ private function getConfigurableSkuPattern($config, $attributeSetName)
624630
return $sku;
625631
}
626632

627-
/**
628-
* @param int $attributes
629-
* @param int $options
630-
* @param string $swatches
631-
* @return array
632-
*/
633-
private function getAttributeSet($attributes, $options, $swatches)
634-
{
635-
$attributeCode = 'configurable_attribute' . $attributes . '_' . $options . '_';
636-
637-
return $this->attributeSetsFixture->createAttributeSet(
638-
$this->attributePattern->generateAttributeSet(
639-
$this->getAttributeSetName($attributes, $options),
640-
$attributes,
641-
$options,
642-
function ($index, $attribute) use ($attributeCode, $options, $swatches) {
643-
$data = [
644-
'attribute_code' => $attributeCode . $index,
645-
'frontend_label' => 'Big Attribute ' . $attributeCode . $index,
646-
];
647-
return array_replace_recursive(
648-
$attribute,
649-
$data,
650-
$this->swatchesGenerator->generateSwatchData($options, $attributeCode . $index, $swatches)
651-
);
652-
}
653-
)
654-
);
655-
}
656-
657633
/**
658634
* Provide attribute set based on attributes configuration
659635
*
@@ -662,24 +638,26 @@ function ($index, $attribute) use ($attributeCode, $options, $swatches) {
662638
*/
663639
private function getCustomAttributeSet(array $attributes)
664640
{
665-
$attributeSetName = $this->getAttributeSetName(
666-
count($attributes),
667-
implode(',', array_column($attributes, 'options'))
668-
);
641+
$attributeSetHash = md5(json_encode($attributes));
642+
$attributeSetName = sprintf('Dynamic Attribute Set %s', $attributeSetHash);
669643

670644
$pattern = $this->attributePattern->generateAttributeSet(
671645
$attributeSetName,
672646
count($attributes),
673647
array_column($attributes, 'options'),
674-
function ($index, $attribute) use ($attributeSetName, $attributes) {
648+
function ($index, $attribute) use ($attributeSetName, $attributes, $attributeSetHash) {
675649
$swatch = [];
676-
$attributeCode = substr(uniqid(sprintf('custom_attribute_%s_', $index)), 0, 30);
650+
$attributeCode = substr(
651+
sprintf('ca_%s_%s', $index, $attributeSetHash),
652+
0,
653+
Attribute::ATTRIBUTE_CODE_MAX_LENGTH
654+
);
677655
$data = [
678656
'attribute_code' => $attributeCode,
679657
'frontend_label' => 'Dynamic Attribute ' . $attributeCode,
680658
];
681659

682-
if (isset($attributes[$index-1]['swatches'])) {
660+
if (isset($attributes[$index - 1]['swatches'])) {
683661
$data['is_visible_in_advanced_search'] = 1;
684662
$data['is_searchable'] = 1;
685663
$data['is_filterable'] = 1;
@@ -704,18 +682,6 @@ function ($index, $attribute) use ($attributeSetName, $attributes) {
704682
return $this->attributeSetsFixture->createAttributeSet($pattern);
705683
}
706684

707-
/**
708-
* Provide attribute set name based on amount of attributes and options per attribute set
709-
*
710-
* @param int $attributesCount
711-
* @param int $optionsCount
712-
* @return string
713-
*/
714-
private function getAttributeSetName($attributesCount, $optionsCount)
715-
{
716-
return uniqid(sprintf('Dynamic Attribute Set %s-%s-', $attributesCount, $optionsCount)) ;
717-
}
718-
719685
/**
720686
* @return array
721687
*/
@@ -906,7 +872,7 @@ private function convertAttributesToDBFormat(array $attributeSet)
906872
'attribute_code',
907873
$attributeData['name']
908874
);
909-
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
875+
/** @var Attribute $attribute */
910876
foreach ($attributeCollection as $attribute) {
911877
$attributeSetId = $attribute->getAttributeSetId();
912878
$values = [];

setup/src/Magento/Setup/Fixtures/OrdersFixture.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ private function getMaxEntityId($tableName, $resourceName, $column = 'entity_id'
563563
* @param string $typeId
564564
* @param int $limit
565565
* @return array
566+
* @throws \Exception
566567
*/
567568
private function getProductIds(\Magento\Store\Api\Data\StoreInterface $store, $typeId, $limit = null)
568569
{
@@ -580,8 +581,11 @@ private function getProductIds(\Magento\Store\Api\Data\StoreInterface $store, $t
580581
$productCollection->getSelect()->where(" type_id = '$typeId' ");
581582
$productCollection->getSelect()->where(" sku NOT LIKE 'Big%' ");
582583
}
583-
584-
return $productCollection->getAllIds($limit);
584+
$ids = $productCollection->getAllIds($limit);
585+
if ($limit && count($ids) < $limit) {
586+
throw new \Exception('Not enough products of type: ' . $typeId);
587+
}
588+
return $ids;
585589
}
586590

587591
/**
@@ -595,6 +599,7 @@ private function getProductIds(\Magento\Store\Api\Data\StoreInterface $store, $t
595599
private function prepareSimpleProducts(array $productIds = [])
596600
{
597601
$productsResult = [];
602+
598603
foreach ($productIds as $key => $simpleId) {
599604
$simpleProduct = $this->productRepository->getById($simpleId);
600605
$productsResult[$key]['id'] = $simpleId;
@@ -625,7 +630,8 @@ private function prepareConfigurableProducts(array $productIds = [])
625630
foreach ($productIds as $key => $configurableId) {
626631
$configurableProduct = $this->productRepository->getById($configurableId);
627632
$options = $this->optionRepository->getList($configurableProduct->getSku());
628-
$configurableChild = $this->linkManagement->getChildren($configurableProduct->getSku())[0];
633+
$configurableChild = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
634+
$configurableChild = reset($configurableChild);
629635
$simpleSku = $configurableChild->getSku();
630636
$simpleId = $this->productRepository->get($simpleSku)->getId();
631637

setup/src/Magento/Setup/Fixtures/WebsiteCategoryProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ public function __construct(
6060
*
6161
* @param int $productIndex Index of generated product
6262
* @return array
63+
* @throws \Exception
6364
*/
6465
public function getWebsiteIds($productIndex)
6566
{
6667
if ($this->isAssignToAllWebsites()) {
6768
return $this->getAllWebsites();
6869
} else {
6970
$categoriesPerWebsite = $this->getCategoriesAndWebsites();
71+
if (!count($categoriesPerWebsite)) {
72+
throw new \Exception('Cannot find categories. Please, be sure that you have generated categories');
73+
}
7074
return [$categoriesPerWebsite[$productIndex % count($categoriesPerWebsite)]['website']];
7175
}
7276
}

setup/src/Magento/Setup/Test/Unit/Fixtures/ConfigurableProductsFixtureTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ public function setUp()
5252
->disableOriginalConstructor()
5353
->getMock();
5454

55-
$this->attributePatternMock = $this->getMockBuilder(CollectionFactory::class)
56-
->disableOriginalConstructor()
57-
->getMock();
58-
$this->attributePatternMock = $this->getMockBuilder(Pattern::class)
59-
->disableOriginalConstructor()
60-
->getMock();
61-
6255
$this->model = (new ObjectManager($this))->getObject(ConfigurableProductsFixture::class, [
6356
'fixtureModel' => $this->fixtureModelMock,
6457
'attributeSetsFixture' => $this->attributeSetsFixtureMock,

0 commit comments

Comments
 (0)