EavSetupTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Setup;
  7. /**
  8. * Test class for Magento\Eav\Setup\EavSetup.
  9. * @magentoDbIsolation enabled
  10. */
  11. class EavSetupTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * Eav setup.
  15. *
  16. * @var \Magento\Eav\Setup\EavSetup
  17. */
  18. private $eavSetup;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp()
  23. {
  24. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  25. $this->eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
  26. }
  27. /**
  28. * Verify that add attribute work correct attribute_code.
  29. *
  30. * @param string $attributeCode
  31. *
  32. * @dataProvider addAttributeDataProvider
  33. */
  34. public function testAddAttribute($attributeCode)
  35. {
  36. $attributeData = $this->getAttributeData();
  37. $this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
  38. $attribute = $this->eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
  39. $this->assertEmpty(array_diff($attributeData, $attribute));
  40. }
  41. /**
  42. * Data provider for testAddAttributeThrowException().
  43. *
  44. * @return array
  45. */
  46. public function addAttributeDataProvider()
  47. {
  48. return [
  49. ['eav_setup_test'],
  50. ['_59_characters_59_characters_59_characters_59_characters_59'],
  51. ];
  52. }
  53. /**
  54. * Verify that add attribute throw exception if attribute_code is not valid.
  55. *
  56. * @param string|null $attributeCode
  57. *
  58. * @dataProvider addAttributeThrowExceptionDataProvider
  59. * @expectedException \Magento\Framework\Exception\LocalizedException
  60. * @expectedExceptionMessage An attribute code must not be less than 1 and more than 60 characters.
  61. */
  62. public function testAddAttributeThrowException($attributeCode)
  63. {
  64. $attributeData = $this->getAttributeData();
  65. $this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
  66. }
  67. /**
  68. * Data provider for testAddAttributeThrowException().
  69. *
  70. * @return array
  71. */
  72. public function addAttributeThrowExceptionDataProvider()
  73. {
  74. return [
  75. [null],
  76. [''],
  77. [' '],
  78. ['more_than_60_characters_more_than_more_than_60_characters_more'],
  79. ];
  80. }
  81. /**
  82. * Get simple attribute data.
  83. */
  84. private function getAttributeData()
  85. {
  86. $attributeData = [
  87. 'type' => 'varchar',
  88. 'backend' => '',
  89. 'frontend' => '',
  90. 'label' => 'Eav Setup Test',
  91. 'input' => 'text',
  92. 'class' => '',
  93. 'source' => '',
  94. 'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
  95. 'visible' => 0,
  96. 'required' => 0,
  97. 'user_defined' => 1,
  98. 'default' => 'none',
  99. 'searchable' => 0,
  100. 'filterable' => 0,
  101. 'comparable' => 0,
  102. 'visible_on_front' => 0,
  103. 'unique' => 0,
  104. 'apply_to' => 'category',
  105. ];
  106. return $attributeData;
  107. }
  108. }