Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeSetFixtureTest.php
Go to the documentation of this file.
1 <?php
8 
12 class AttributeSetFixtureTest extends \PHPUnit\Framework\TestCase
13 {
14  public function testExecute()
15  {
16  $attributeSets = [
17  'name' => 'attribute set name',
18  'attributes' => [
19  'attribute' => [
20  [
21  'is_required' => 1,
22  'is_visible_on_front' => 1,
23  'is_visible_in_advanced_search' => 0,
24  'is_filterable' => 0,
25  'is_filterable_in_search' => 0,
26  'attribute_code' => 'attribute_1',
27  'is_searchable' => 0,
28  'frontend_label' => 'Attribute 1',
29  'frontend_input' => 'select',
30  'backend_type' => 1,
31  'default_option' => 'option 1',
32  'options' => [
33  'option' => [
34  [
35  'label' => 'option 1',
36  'value' => 'option_1'
37  ],
38  ]
39  ]
40  ]
41  ]
42  ]
43  ];
44 
45  // Mock Attribute Sets
46  $attributeSetMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeSetInterface::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $attributeSetMock->expects($this->once())
50  ->method('setAttributeSetName')
51  ->with("attribute set name");
52  $attributeSetMock->expects($this->once())
53  ->method('setEntityTypeId')
54  ->with(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE);
55  $attributeSetMock->expects($this->any())
56  ->method('getAttributeSetName')
57  ->willReturn($attributeSets['name']);
58 
59  $attributeSetFactoryMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeSetInterfaceFactory::class)
60  ->disableOriginalConstructor()
61  ->setMethods(['create'])
62  ->getMock();
63  $attributeSetFactoryMock->expects($this->once())
64  ->method('create')
65  ->willReturn($attributeSetMock);
66 
67  $attributeSetManagementMock = $this->getMockBuilder(\Magento\Catalog\Api\AttributeSetManagementInterface::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $attributeSetManagementMock->expects($this->once())
71  ->method('create')
72  ->with($attributeSetMock, '4')
73  ->willReturn($attributeSetMock);
74 
75  //Mock Attribute Groups
76  $attributeGroupMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeGroupInterface::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79  $attributeGroupMock->expects($this->once())
80  ->method('setAttributeGroupName')
81  ->with($attributeSetMock->getAttributeSetName() . ' - Group');
82  $attributeGroupMock->expects($this->once())
83  ->method('setAttributeSetId')
84  ->with($attributeSetMock->getAttributeSetId());
85 
86  $attributeGroupFactoryMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeGroupInterfaceFactory::class)
87  ->disableOriginalConstructor()
88  ->setMethods(['create'])
89  ->getMock();
90  $attributeGroupFactoryMock->expects($this->once())
91  ->method('create')
92  ->willReturn($attributeGroupMock);
93 
94  $productAttributeGroupRepoMock = $this->getMockBuilder(
95  \Magento\Catalog\Api\ProductAttributeGroupRepositoryInterface::class
96  )
97  ->disableOriginalConstructor()
98  ->getMock();
99  $productAttributeGroupRepoMock->expects($this->once())
100  ->method('save')
101  ->with($attributeGroupMock)
102  ->willReturn($attributeGroupMock);
103 
104  // Mock Attributes
105  $attributeMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductAttributeInterface::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108 
109  $attributeFactoryMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory::class)
110  ->disableOriginalConstructor()
111  ->setMethods(['create'])
112  ->getMock();
113  $attributeFactoryMock->expects($this->once())
114  ->method('create')
115  ->willReturn($attributeMock);
116 
117  //Mock Attribute Options
118  $optionMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeOptionInterface::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121 
122  $optionFactoryMock = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeOptionInterfaceFactory::class)
123  ->disableOriginalConstructor()
124  ->setMethods(['create'])
125  ->getMock();
126  $optionFactoryMock->expects($this->once())
127  ->method('create')
128  ->willReturn($optionMock);
129 
130  $productAttributeRepoMock = $this->getMockBuilder(
131  \Magento\Catalog\Api\ProductAttributeRepositoryInterface::class
132  )
133  ->disableOriginalConstructor()
134  ->getMock();
135  $productAttributeRepoMock->expects($this->once())
136  ->method('save')
137  ->with($attributeMock)
138  ->willReturn($attributeMock);
139 
140  $productAttributeManagementMock = $this->getMockBuilder(
141  \Magento\Catalog\Api\ProductAttributeManagementInterface::class
142  )
143  ->disableOriginalConstructor()
144  ->getMock();
145  $productAttributeManagementMock->expects($this->once())
146  ->method('assign')
147  ->willReturn($attributeMock->getAttributeId());
148 
149  $attributeSet = new \Magento\Setup\Fixtures\AttributeSet\AttributeSetFixture(
150  $attributeSetManagementMock,
151  $productAttributeGroupRepoMock,
152  $productAttributeRepoMock,
153  $productAttributeManagementMock,
154  $attributeFactoryMock,
155  $optionFactoryMock,
156  $attributeSetFactoryMock,
157  $attributeGroupFactoryMock
158  );
159  $attributeSet->createAttributeSet($attributeSets);
160  }
161 }