Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeCreateTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory;
10 use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
11 
16 class AttributeCreateTest extends \PHPUnit\Framework\TestCase
17 {
22  public function testSetScopeDefault()
23  {
25 
26  $data = [
27  'is_required' => 1,
28  'is_visible_on_front' => 1,
29  'is_visible_in_advanced_search' => 0,
30  'attribute_code' => 'color_swatch',
31  'backend_type' => '',
32  'is_searchable' => 0,
33  'is_filterable' => 0,
34  'is_filterable_in_search' => 0,
35  'frontend_label' => 'Attribute ',
36  ];
38 
39  $data['frontend_input'] = 'swatch_visual';
40  $data['swatch_input_type'] = 'visual';
41  $data['swatchvisual']['value'] = array_reduce(
42  range(1, $optionsPerAttribute),
43  function ($values, $index) use ($optionsPerAttribute) {
44  $values['option_' . $index] = '#'
45  . str_repeat(
46  dechex(255 * $index / $optionsPerAttribute),
47  3
48  );
49  return $values;
50  },
51  []
52  );
53  $data['optionvisual']['value'] = array_reduce(
54  range(1, $optionsPerAttribute),
55  function ($values, $index) use ($optionsPerAttribute) {
56  $values['option_' . $index] = ['option ' . $index];
57  return $values;
58  },
59  []
60  );
61 
62  $data['options']['option'] = array_reduce(
63  range(1, $optionsPerAttribute),
64  function ($values, $index) use ($optionsPerAttribute) {
65  $values[] = [
66  'label' => 'option ' . $index,
67  'value' => 'option_' . $index
68  ];
69  return $values;
70  },
71  []
72  );
73 
74  $options = [];
75  foreach ($data['options']['option'] as $optionData) {
76  $options[] = $om->get(AttributeOptionInterfaceFactory::class)->create(['data' => $optionData]);
77  }
78 
79  $attribute = $om->get(ProductAttributeInterfaceFactory::class)
80  ->create(['data' => $data]);
81  $attribute->setOptions($options);
82  $attribute->setNote('auto');
83 
84  $attribute = $om->get(ProductAttributeRepositoryInterface::class)->save($attribute);
85  $this->assertNotEmpty($attribute->getId());
86  $this->assertEquals('swatch_visual', $attribute->getFrontendInput());
87  }
88 }
$optionData
$values
Definition: options.phtml:88
$om
$index
Definition: list.phtml:44