AttributeCreateTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Model;
  7. use Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory;
  8. use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
  9. use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
  10. /**
  11. * Test save of swatch attribute
  12. *
  13. */
  14. class AttributeCreateTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @magentoAppArea adminhtml
  18. * @magentoDbIsolation enabled
  19. */
  20. public function testSetScopeDefault()
  21. {
  22. $om = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  23. $data = [
  24. 'is_required' => 1,
  25. 'is_visible_on_front' => 1,
  26. 'is_visible_in_advanced_search' => 0,
  27. 'attribute_code' => 'color_swatch',
  28. 'backend_type' => '',
  29. 'is_searchable' => 0,
  30. 'is_filterable' => 0,
  31. 'is_filterable_in_search' => 0,
  32. 'frontend_label' => 'Attribute ',
  33. ];
  34. $optionsPerAttribute = 3;
  35. $data['frontend_input'] = 'swatch_visual';
  36. $data['swatch_input_type'] = 'visual';
  37. $data['swatchvisual']['value'] = array_reduce(
  38. range(1, $optionsPerAttribute),
  39. function ($values, $index) use ($optionsPerAttribute) {
  40. $values['option_' . $index] = '#'
  41. . str_repeat(
  42. dechex(255 * $index / $optionsPerAttribute),
  43. 3
  44. );
  45. return $values;
  46. },
  47. []
  48. );
  49. $data['optionvisual']['value'] = array_reduce(
  50. range(1, $optionsPerAttribute),
  51. function ($values, $index) use ($optionsPerAttribute) {
  52. $values['option_' . $index] = ['option ' . $index];
  53. return $values;
  54. },
  55. []
  56. );
  57. $data['options']['option'] = array_reduce(
  58. range(1, $optionsPerAttribute),
  59. function ($values, $index) use ($optionsPerAttribute) {
  60. $values[] = [
  61. 'label' => 'option ' . $index,
  62. 'value' => 'option_' . $index
  63. ];
  64. return $values;
  65. },
  66. []
  67. );
  68. $options = [];
  69. foreach ($data['options']['option'] as $optionData) {
  70. $options[] = $om->get(AttributeOptionInterfaceFactory::class)->create(['data' => $optionData]);
  71. }
  72. $attribute = $om->get(ProductAttributeInterfaceFactory::class)
  73. ->create(['data' => $data]);
  74. $attribute->setOptions($options);
  75. $attribute->setNote('auto');
  76. $attribute = $om->get(ProductAttributeRepositoryInterface::class)->save($attribute);
  77. $this->assertNotEmpty($attribute->getId());
  78. $this->assertEquals('swatch_visual', $attribute->getFrontendInput());
  79. }
  80. }