10 use Magento\Mtf\Fixture\DataSource;
11 use Magento\Mtf\Fixture\FixtureFactory;
12 use Magento\Mtf\Fixture\FixtureInterface;
13 use Magento\Mtf\Fixture\InjectableFixture;
14 use Magento\Mtf\Repository\RepositoryFactory;
55 private $addMediaGallery =
false;
83 private $bulkImagesPriceQuantity = [];
93 RepositoryFactory $repositoryFactory,
101 if (isset(
$data[
'dataset']) && isset($this->params[
'repository'])) {
102 $dataset = $repositoryFactory->get($this->params[
'repository'])->get(
$data[
'dataset']);
103 unset(
$data[
'dataset']);
106 if (isset(
$data[
'media_gallery'])) {
107 $this->addMediaGallery =
true;
108 unset(
$data[
'media_gallery']);
111 $data = array_replace_recursive(
$data, $dataset);
114 $this->prepareAttributes(
$data);
117 $this->prepareVariationsMatrix(
$data);
119 $this->prepareBulkImagesPriceQuantity(
$data);
129 protected function prepareAttributes(array
$data)
131 if (!isset(
$data[
'attributes'])) {
137 list($fixture, $dataset) = explode(
'::',
$attribute);
139 $attribute = $this->fixtureFactory->createByCode($fixture, [
'dataset' => $dataset]);
156 foreach ($this->attributes as $attributeKey =>
$attribute) {
167 $this->attributesData = array_replace_recursive(
168 isset(
$data[
'attributes_data']) ?
$data[
'attributes_data'] : [],
169 $this->attributesData
180 $assignedProducts = [
'products' => []];
181 foreach (array_keys($this->variationsMatrix) as $variation) {
182 $assignedProducts[
'products'][$variation] =
'catalogProductSimple::default';
196 if (!isset(
$data[
'products'])) {
201 if (!empty($this->attributes)) {
205 foreach ($data[
'products'] as $key =>
$product) {
207 list($fixture, $dataset) = explode(
'::',
$product);
209 $productData = isset($this->variationsMatrix[$key]) ? $this->variationsMatrix[$key] : [];
211 $product = $this->fixtureFactory->createByCode(
214 'dataset' => $dataset,
234 if (!$this->attributeSet) {
235 $this->attributeSet = $this->fixtureFactory->createByCode(
236 'catalogAttributeSet',
238 'dataset' =>
'custom_attribute_set',
240 'assigned_attributes' => [
241 'attributes' => array_values($this->attributes),
246 $this->attributeSet->persist();
260 $compositeKeys = explode(
' ', $key);
263 foreach ($compositeKeys as $compositeKey) {
266 $compositeKey = explode(
':', $compositeKey);
267 $attributeKey = $compositeKey[0];
268 $data[$this->attributesData[$attributeKey][
'attribute_code']] = $attributeId;
283 list($attributeKey, $optionKey) = explode(
':', $compositeKey);
284 return isset($this->attributesData[$attributeKey][
'options'][$optionKey][
'id'])
285 ? $this->attributesData[$attributeKey][
'options'][$optionKey][
'id']
297 protected function prepareVariationsMatrix(array
$data)
302 foreach ($this->attributesData as $attributeKey =>
$attribute) {
306 if (isset(
$data[
'matrix'])) {
310 unset(
$data[
'matrix'][$key][
'sku']);
315 $this->variationsMatrix = isset(
$data[
'matrix'])
320 foreach ($this->variationsMatrix as $key =>
$row) {
321 if (isset($this->products[$key])) {
324 $quantityAndStockStatus =
$product->getQuantityAndStockStatus();
326 'configurable_attribute' =>
$product->getId(),
329 'qty' => $quantityAndStockStatus[
'qty'],
333 $this->variationsMatrix[$key] = array_replace_recursive($this->variationsMatrix[$key],
$productData);
335 $this->variationsMatrix[$key] = array_replace_recursive(
336 $this->variationsMatrix[$key],
363 $variationIsolation = mt_rand(10000, 70000);
366 'name' =>
"In configurable product {$variationIsolation}",
367 'sku' =>
"in_configurable_product_{$variationIsolation}",
373 $randIsolation = mt_rand(1, 100);
374 $rowName =
$row[
'name'];
375 $rowSku =
$row[
'sku'];
380 $compositeKey =
"{$attributeKey}:{$optionKey}";
381 $row[
'name'] = $rowName .
' ' . $randIsolation .
' ' .
$index;
382 $row[
'sku'] = $rowSku .
'_' . $randIsolation .
'_' .
$index;
384 if ($this->addMediaGallery) {
387 $newRowKey = $rowKey ?
"{$rowKey} {$compositeKey}" : $compositeKey;
405 $filePath = $this->getFullPath($filename);
406 if (!file_exists($filePath)) {
407 $optionImage = imagecreate(300, 200);
408 $colorYellow = imagecolorallocate($optionImage, 255, 255, 0);
409 imagefilledrectangle($optionImage, 50, 50, 250, 150, $colorYellow);
410 $directory = dirname($filePath);
411 if (!file_exists($directory)) {
412 mkdir($directory, 0777,
true);
414 imagejpeg($optionImage, $filePath);
415 imagedestroy($optionImage);
424 'label' =>
'1231414',
436 private function getFullPath($filename)
438 return BP . $this->mediaPathTmp . $filename;
463 $variationMatrixFields = [
464 'configurable_attribute',
475 'attributes_data' => [],
478 foreach ($this->attributesData as $attributeKey =>
$attribute) {
481 $attribute[
'options'][$optionKey] = array_intersect_key(
$option, array_flip($optionFields));
488 $this->data[
'attributes_data'][$attributeKey] =
$attribute;
490 foreach ($this->variationsMatrix as $key => $variationMatrix) {
491 $this->data[
'matrix'][$key] = array_intersect_key($variationMatrix, array_flip($variationMatrixFields));
501 private function prepareBulkImagesPriceQuantity(array
$data)
503 if (isset(
$data[
'bulk_images_price_quantity'])) {
504 $this->bulkImagesPriceQuantity =
$data[
'bulk_images_price_quantity'];
535 return $this->bulkImagesPriceQuantity;
prepareAttributesData(array $data)
prepareProducts(array $data)
addVariationMatrix(array $variationsMatrix, array $attribute, $attributeKey)
__construct(RepositoryFactory $repositoryFactory, FixtureFactory $fixtureFactory, array $data, array $params=[])
getProductAttributeData($key)
getAttributeOptionId($compositeKey)
getBulkImagesPriceQuantity()
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
prepareMediaGallery($filename='option_image.jpg')