Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SwatchAttributeOptionAddTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Swatches\Model;
7 
10 use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
11 
16 class SwatchAttributeOptionAddTest extends \PHPUnit\Framework\TestCase
17 {
21  private $objectManager;
22 
23  protected function setUp()
24  {
26  }
27 
33  public function testSwatchOptionAdd()
34  {
36  $attribute = $this->objectManager
37  ->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
38  ->load('color_swatch', 'attribute_code');
40 
41  $data['options']['option'] = array_reduce(
42  range(10, $optionsPerAttribute),
43  function ($values, $index) use ($optionsPerAttribute) {
44  $values[] = [
45  'label' => 'option ' . $index,
46  'value' => 'option_' . $index
47  ];
48  return $values;
49  },
50  []
51  );
52 
54  $options = [];
55  foreach ($data['options']['option'] as $optionData) {
56  $options[] = $this->objectManager
57  ->get(AttributeOptionInterfaceFactory::class)
58  ->create(['data' => $optionData]);
59  }
60 
62  $optionManagement = $this->objectManager->get(ProductAttributeOptionManagementInterface::class);
63  foreach ($options as $option) {
64  $optionManagement->add(
65  $attribute->getAttributeCode(),
66  $option
67  );
68  }
69 
70  $items = $optionManagement->getItems($attribute->getAttributeCode());
71  array_walk(
72  $items,
73  function (&$item) {
75  $item = $item->getLabel();
76  }
77  );
78  foreach ($options as $option) {
79  $this->assertTrue(in_array($option->getLabel(), $items));
80  }
81  }
82 }
$optionData
$values
Definition: options.phtml:88
$index
Definition: list.phtml:44
$items