Skip to content

Commit 4f38525

Browse files
committed
MAGETWO-61556: Fixture generation (Configurables, Attribute Set)
1 parent 732d445 commit 4f38525

File tree

4 files changed

+413
-188
lines changed

4 files changed

+413
-188
lines changed

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

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,27 @@ class AttributeSetsFixture extends Fixture
1616
*/
1717
protected $priority = 25;
1818

19+
/**
20+
* Cache for attribute IDs.
21+
*
22+
* @var array
23+
*/
24+
private $attributeIdsCache = [];
25+
26+
/**
27+
* Quantity of unique attributes to generate. Zero means infinity.
28+
*
29+
* @var int
30+
*/
31+
protected $uniqueAttributesQuantity = 0;
32+
1933
/**
2034
* {@inheritdoc}
2135
*/
2236
public function execute()
2337
{
24-
$attributeSets = $this->fixtureModel->getValue('attribute_sets', null);
38+
$this->populateUniqueAttributesQuantity();
39+
$attributeSets = $this->getAttributeSetsFixtureValue();
2540
if ($attributeSets === null) {
2641
return;
2742
}
@@ -71,37 +86,81 @@ public function execute()
7186
$attributesData = array_key_exists(0, $attributeSetData['attributes']['attribute'])
7287
? $attributeSetData['attributes']['attribute'] : [$attributeSetData['attributes']['attribute']];
7388
foreach ($attributesData as $attributeData) {
74-
//Create Attribute
75-
/** @var \Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory $attributeFactory */
76-
$attributeFactory = $this->fixtureModel->getObjectManager()->create(
77-
\Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory::class
78-
);
79-
80-
$optionsData = array_key_exists(0, $attributeData['options']['option'])
81-
? $attributeData['options']['option'] : [$attributeData['options']['option']];
82-
$options = [];
83-
foreach ($optionsData as $optionData) {
84-
/** @var \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory */
85-
$optionFactory = $this->fixtureModel->getObjectManager()->create(
86-
\Magento\Eav\Api\Data\AttributeOptionInterfaceFactory::class
89+
if ($this->uniqueAttributesQuantity === 0
90+
|| (count($this->attributeIdsCache) < $this->uniqueAttributesQuantity)) {
91+
//Create Attribute
92+
/** @var \Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory $attributeFactory */
93+
$attributeFactory = $this->fixtureModel->getObjectManager()->create(
94+
\Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory::class
8795
);
88-
$option = $optionFactory->create(['data' => $optionData]);
89-
$options[] = $option;
90-
}
9196

92-
$attribute = $attributeFactory->create(['data' => $attributeData]);
93-
$attribute->setOptions($options);
97+
$optionsData = array_key_exists(0, $attributeData['options']['option'])
98+
? $attributeData['options']['option'] : [$attributeData['options']['option']];
99+
$options = [];
100+
foreach ($optionsData as $optionData) {
101+
/** @var \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory */
102+
$optionFactory = $this->fixtureModel->getObjectManager()->create(
103+
\Magento\Eav\Api\Data\AttributeOptionInterfaceFactory::class
104+
);
105+
$option = $optionFactory->create(['data' => $optionData]);
106+
$options[] = $option;
107+
}
94108

95-
$result = $attributeRepository->save($attribute);
96-
$attributeId = $result->getAttributeId();
109+
$attribute = $attributeFactory->create(['data' => $attributeData]);
110+
$attribute->setOptions($options);
97111

112+
$result = $attributeRepository->save($attribute);
113+
$attributeId = $result->getAttributeId();
114+
$this->fillAttributeIdsCache($attributeId);
115+
} else {
116+
$attributeId = $this->getAttributeIdFromCache();
117+
}
98118
//Associate Attribute to Attribute Set
99119
$sortOrder = 3;
120+
100121
$attributeManagement->assign($attributeSetId, $attributeGroupId, $attributeId, $sortOrder);
101122
}
102123
}
103124
}
104125

126+
/**
127+
* Get attribute ID from cache.
128+
*
129+
* @return int
130+
*/
131+
private function getAttributeIdFromCache()
132+
{
133+
$attributeId = next($this->attributeIdsCache);
134+
if ($attributeId === false) {
135+
$attributeId = reset($this->attributeIdsCache);
136+
}
137+
138+
return $attributeId;
139+
}
140+
141+
/**
142+
* Fill attribute IDs cache.
143+
*
144+
* @param int $attributeId
145+
* @return void
146+
*/
147+
private function fillAttributeIdsCache($attributeId)
148+
{
149+
if ($this->uniqueAttributesQuantity !== 0) {
150+
$this->attributeIdsCache[] = $attributeId;
151+
}
152+
}
153+
154+
/**
155+
* Populate quantity of unique attributes to generate.
156+
*
157+
* @return void
158+
*/
159+
protected function populateUniqueAttributesQuantity()
160+
{
161+
$this->uniqueAttributesQuantity = $this->fixtureModel->getValue('unique_attributes_quantity', 0);
162+
}
163+
105164
/**
106165
* {@inheritdoc}
107166
*/
@@ -119,4 +178,14 @@ public function introduceParamLabels()
119178
'attribute_sets' => 'Attribute Sets'
120179
];
121180
}
181+
182+
/**
183+
* Get attribute sets fixture value.
184+
*
185+
* @return array|null
186+
*/
187+
protected function getAttributeSetsFixtureValue()
188+
{
189+
return $this->fixtureModel->getValue('attribute_sets', null);
190+
}
122191
}

0 commit comments

Comments
 (0)