objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); } /** * @magentoAppArea adminhtml * @magentoDbIsolation enabled * @magentoDataFixture Magento/Swatches/_files/swatch_attribute.php */ public function testSwatchOptionAdd() { /** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */ $attribute = $this->objectManager ->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class) ->load('color_swatch', 'attribute_code'); $optionsPerAttribute = 3; $data['options']['option'] = array_reduce( range(10, $optionsPerAttribute), function ($values, $index) use ($optionsPerAttribute) { $values[] = [ 'label' => 'option ' . $index, 'value' => 'option_' . $index ]; return $values; }, [] ); /** @var AttributeOptionInterface[] $options */ $options = []; foreach ($data['options']['option'] as $optionData) { $options[] = $this->objectManager ->get(AttributeOptionInterfaceFactory::class) ->create(['data' => $optionData]); } /** @var ProductAttributeOptionManagementInterface $optionManagement */ $optionManagement = $this->objectManager->get(ProductAttributeOptionManagementInterface::class); foreach ($options as $option) { $optionManagement->add( $attribute->getAttributeCode(), $option ); } $items = $optionManagement->getItems($attribute->getAttributeCode()); array_walk( $items, function (&$item) { /** @var AttributeOptionInterface $item */ $item = $item->getLabel(); } ); foreach ($options as $option) { $this->assertTrue(in_array($option->getLabel(), $items)); } } }