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