3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Test \Unit \Controller \Adminhtml \Product \Attribute ;
7
9
10
+ use Magento \Backend \Model \Session ;
11
+ use Magento \Backend \Model \View \Result \Redirect as ResultRedirect ;
8
12
use Magento \Catalog \Api \Data \ProductAttributeInterface ;
9
13
use Magento \Catalog \Controller \Adminhtml \Product \Attribute \Save ;
10
- use Magento \Eav \Model \Validator \Attribute \Code as AttributeCodeValidator ;
11
- use Magento \Framework \Serialize \Serializer \FormData ;
12
- use Magento \Catalog \Test \Unit \Controller \Adminhtml \Product \AttributeTest ;
13
- use Magento \Catalog \Model \Product \AttributeSet \BuildFactory ;
14
+ use Magento \Catalog \Helper \Product as ProductHelper ;
15
+ use Magento \Catalog \Model \Product \Attribute \Frontend \Inputtype \Presentation ;
14
16
use Magento \Catalog \Model \Product \AttributeSet \Build ;
17
+ use Magento \Catalog \Model \Product \AttributeSet \BuildFactory ;
15
18
use Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory ;
19
+ use Magento \Catalog \Test \Unit \Controller \Adminhtml \Product \AttributeTest ;
16
20
use Magento \Eav \Api \Data \AttributeSetInterface ;
21
+ use Magento \Eav \Model \Adminhtml \System \Config \Source \Inputtype \Validator as InputTypeValidator ;
17
22
use Magento \Eav \Model \Adminhtml \System \Config \Source \Inputtype \ValidatorFactory ;
18
23
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \CollectionFactory ;
24
+ use Magento \Eav \Model \Validator \Attribute \Code as AttributeCodeValidator ;
19
25
use Magento \Framework \Controller \ResultFactory ;
26
+ use Magento \Framework \Exception \NotFoundException ;
20
27
use Magento \Framework \Filter \FilterManager ;
21
- use Magento \Catalog \ Helper \ Product as ProductHelper ;
28
+ use Magento \Framework \ Serialize \ Serializer \ FormData ;
22
29
use Magento \Framework \View \Element \Messages ;
23
30
use Magento \Framework \View \LayoutFactory ;
24
- use Magento \Backend \Model \View \Result \Redirect as ResultRedirect ;
25
- use Magento \Eav \Model \Adminhtml \System \Config \Source \Inputtype \Validator as InputTypeValidator ;
26
31
use Magento \Framework \View \LayoutInterface ;
32
+ use PHPUnit \Framework \MockObject \MockObject ;
27
33
28
34
/**
35
+ * Test product attribute controller
36
+ *
29
37
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38
+ * @SuppressWarnings(PHPMD.TooManyFields)
30
39
*/
31
40
class SaveTest extends AttributeTest
32
41
{
33
42
/**
34
- * @var BuildFactory|\PHPUnit_Framework_MockObject_MockObject
43
+ * @var BuildFactory|MockObject
35
44
*/
36
45
protected $ buildFactoryMock ;
37
46
38
47
/**
39
- * @var FilterManager|\PHPUnit_Framework_MockObject_MockObject
48
+ * @var FilterManager|MockObject
40
49
*/
41
50
protected $ filterManagerMock ;
42
51
43
52
/**
44
- * @var ProductHelper|\PHPUnit_Framework_MockObject_MockObject
53
+ * @var ProductHelper|MockObject
45
54
*/
46
55
protected $ productHelperMock ;
47
56
48
57
/**
49
- * @var AttributeFactory|\PHPUnit_Framework_MockObject_MockObject
58
+ * @var AttributeFactory|MockObject
50
59
*/
51
60
protected $ attributeFactoryMock ;
52
61
53
62
/**
54
- * @var ValidatorFactory|\PHPUnit_Framework_MockObject_MockObject
63
+ * @var ValidatorFactory|MockObject
55
64
*/
56
65
protected $ validatorFactoryMock ;
57
66
58
67
/**
59
- * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
68
+ * @var CollectionFactory|MockObject
60
69
*/
61
70
protected $ groupCollectionFactoryMock ;
62
71
63
72
/**
64
- * @var LayoutFactory|\PHPUnit_Framework_MockObject_MockObject
73
+ * @var LayoutFactory|MockObject
65
74
*/
66
75
protected $ layoutFactoryMock ;
67
76
68
77
/**
69
- * @var ResultRedirect|\PHPUnit_Framework_MockObject_MockObject
78
+ * @var ResultRedirect|MockObject
70
79
*/
71
80
protected $ redirectMock ;
72
81
73
82
/**
74
- * @var AttributeSet|\PHPUnit_Framework_MockObject_MockObject
83
+ * @var AttributeSet|MockObject
75
84
*/
76
85
protected $ attributeSetMock ;
77
86
78
87
/**
79
- * @var Build|\PHPUnit_Framework_MockObject_MockObject
88
+ * @var Build|MockObject
80
89
*/
81
90
protected $ builderMock ;
82
91
83
92
/**
84
- * @var InputTypeValidator|\PHPUnit_Framework_MockObject_MockObject
93
+ * @var InputTypeValidator|MockObject
85
94
*/
86
95
protected $ inputTypeValidatorMock ;
87
96
88
97
/**
89
- * @var FormData|\PHPUnit_Framework_MockObject_MockObject
98
+ * @var FormData|MockObject
99
+ */
100
+ protected $ formDataSerializerMock ;
101
+
102
+ /**
103
+ * @var ProductAttributeInterface|MockObject
104
+ */
105
+ protected $ productAttributeMock ;
106
+
107
+ /**
108
+ * @var Presentation|MockObject
90
109
*/
91
- private $ formDataSerializerMock ;
110
+ protected $ presentationMock ;
92
111
93
112
/**
94
- * @var ProductAttributeInterface|\PHPUnit_Framework_MockObject_MockObject
113
+ * @var Session|MockObject
95
114
*/
96
- private $ productAttributeMock ;
115
+ protected $ sessionMock ;
97
116
98
117
/**
99
- * @var AttributeCodeValidator|\PHPUnit_Framework_MockObject_MockObject
118
+ * @var AttributeCodeValidator|MockObject
100
119
*/
101
- private $ attributeCodeValidatorMock ;
120
+ protected $ attributeCodeValidatorMock ;
102
121
103
- protected function setUp ()
122
+ /**
123
+ * @inheritDoc
124
+ */
125
+ public function setUp (): void
104
126
{
105
127
parent ::setUp ();
128
+ $ this ->filterManagerMock = $ this ->createMock (FilterManager::class);
129
+ $ this ->productHelperMock = $ this ->createMock (ProductHelper::class);
130
+ $ this ->attributeSetMock = $ this ->createMock (AttributeSetInterface::class);
131
+ $ this ->builderMock = $ this ->createMock (Build::class);
132
+ $ this ->inputTypeValidatorMock = $ this ->createMock (InputTypeValidator::class);
133
+ $ this ->formDataSerializerMock = $ this ->createMock (FormData::class);
134
+ $ this ->attributeCodeValidatorMock = $ this ->createMock (AttributeCodeValidator::class);
135
+ $ this ->presentationMock = $ this ->createMock (Presentation::class);
136
+ $ this ->sessionMock = $ this ->createMock (Session::class);
137
+ $ this ->layoutFactoryMock = $ this ->createMock (LayoutFactory::class);
106
138
$ this ->buildFactoryMock = $ this ->getMockBuilder (BuildFactory::class)
107
139
->setMethods (['create ' ])
108
140
->disableOriginalConstructor ()
109
141
->getMock ();
110
- $ this ->filterManagerMock = $ this ->getMockBuilder (FilterManager::class)
111
- ->disableOriginalConstructor ()
112
- ->getMock ();
113
- $ this ->productHelperMock = $ this ->getMockBuilder (ProductHelper::class)
114
- ->disableOriginalConstructor ()
115
- ->getMock ();
116
142
$ this ->attributeFactoryMock = $ this ->getMockBuilder (AttributeFactory::class)
117
143
->setMethods (['create ' ])
118
144
->disableOriginalConstructor ()
@@ -125,32 +151,23 @@ protected function setUp()
125
151
->setMethods (['create ' ])
126
152
->disableOriginalConstructor ()
127
153
->getMock ();
128
- $ this ->layoutFactoryMock = $ this ->getMockBuilder (LayoutFactory::class)
129
- ->disableOriginalConstructor ()
130
- ->getMock ();
131
154
$ this ->redirectMock = $ this ->getMockBuilder (ResultRedirect::class)
132
155
->setMethods (['setData ' , 'setPath ' ])
133
156
->disableOriginalConstructor ()
134
157
->getMock ();
135
- $ this ->attributeSetMock = $ this ->getMockBuilder (AttributeSetInterface::class)
136
- ->disableOriginalConstructor ()
137
- ->getMock ();
138
- $ this ->builderMock = $ this ->getMockBuilder (Build::class)
139
- ->disableOriginalConstructor ()
140
- ->getMock ();
141
- $ this ->inputTypeValidatorMock = $ this ->getMockBuilder (InputTypeValidator::class)
142
- ->disableOriginalConstructor ()
143
- ->getMock ();
144
- $ this ->formDataSerializerMock = $ this ->getMockBuilder (FormData::class)
145
- ->disableOriginalConstructor ()
146
- ->getMock ();
147
- $ this ->attributeCodeValidatorMock = $ this ->getMockBuilder (AttributeCodeValidator::class)
148
- ->disableOriginalConstructor ()
149
- ->getMock ();
150
158
$ this ->productAttributeMock = $ this ->getMockBuilder (ProductAttributeInterface::class)
151
- ->setMethods (['getId ' , 'get ' ])
152
- ->getMockForAbstractClass ();
153
-
159
+ ->setMethods (
160
+ [
161
+ 'getId ' ,
162
+ 'get ' ,
163
+ 'getBackendTypeByInput ' ,
164
+ 'getDefaultValueByInput ' ,
165
+ 'getBackendType ' ,
166
+ 'getFrontendClass ' ,
167
+ 'addData ' ,
168
+ 'save '
169
+ ]
170
+ )->getMockForAbstractClass ();
154
171
$ this ->buildFactoryMock ->expects ($ this ->any ())
155
172
->method ('create ' )
156
173
->willReturn ($ this ->builderMock );
@@ -163,7 +180,7 @@ protected function setUp()
163
180
}
164
181
165
182
/**
166
- * { @inheritdoc}
183
+ * @inheritdoc
167
184
*/
168
185
protected function getModel ()
169
186
{
@@ -180,11 +197,17 @@ protected function getModel()
180
197
'groupCollectionFactory ' => $ this ->groupCollectionFactoryMock ,
181
198
'layoutFactory ' => $ this ->layoutFactoryMock ,
182
199
'formDataSerializer ' => $ this ->formDataSerializerMock ,
183
- 'attributeCodeValidator ' => $ this ->attributeCodeValidatorMock
200
+ 'attributeCodeValidator ' => $ this ->attributeCodeValidatorMock ,
201
+ 'presentation ' => $ this ->presentationMock ,
202
+ '_session ' => $ this ->sessionMock
184
203
]);
185
204
}
186
205
187
- public function testExecuteWithEmptyData ()
206
+ /**
207
+ * @return void
208
+ * @throws NotFoundException
209
+ */
210
+ public function testExecuteWithEmptyData (): void
188
211
{
189
212
$ this ->requestMock ->expects ($ this ->any ())
190
213
->method ('getParam ' )
@@ -210,7 +233,76 @@ public function testExecuteWithEmptyData()
210
233
$ this ->assertInstanceOf (ResultRedirect::class, $ this ->getModel ()->execute ());
211
234
}
212
235
213
- public function testExecute ()
236
+ /**
237
+ * @return void
238
+ * @throws NotFoundException
239
+ */
240
+ public function testExecuteSaveFrontendClass (): void
241
+ {
242
+ $ data = [
243
+ 'frontend_input ' => 'test_frontend_input ' ,
244
+ ];
245
+
246
+ $ this ->requestMock ->expects ($ this ->any ())
247
+ ->method ('getParam ' )
248
+ ->willReturnMap ([
249
+ ['isAjax ' , null , null ],
250
+ ['serialized_options ' , '[] ' , '' ],
251
+ ['set ' , null , 1 ],
252
+ ['attribute_code ' , null , 'test_attribute_code ' ],
253
+ ]);
254
+ $ this ->formDataSerializerMock
255
+ ->expects ($ this ->once ())
256
+ ->method ('unserialize ' )
257
+ ->with ('' )
258
+ ->willReturn ([]);
259
+ $ this ->requestMock ->expects ($ this ->once ())
260
+ ->method ('getPostValue ' )
261
+ ->willReturn ($ data );
262
+ $ this ->inputTypeValidatorMock ->expects ($ this ->any ())
263
+ ->method ('isValid ' )
264
+ ->with ($ data ['frontend_input ' ])
265
+ ->willReturn (true );
266
+ $ this ->presentationMock ->expects ($ this ->once ())
267
+ ->method ('convertPresentationDataToInputType ' )
268
+ ->willReturn ($ data );
269
+ $ this ->productHelperMock ->expects ($ this ->once ())
270
+ ->method ('getAttributeSourceModelByInputType ' )
271
+ ->with ($ data ['frontend_input ' ])
272
+ ->willReturn (null );
273
+ $ this ->productHelperMock ->expects ($ this ->once ())
274
+ ->method ('getAttributeBackendModelByInputType ' )
275
+ ->with ($ data ['frontend_input ' ])
276
+ ->willReturn (null );
277
+ $ this ->productAttributeMock ->expects ($ this ->once ())
278
+ ->method ('getBackendTypeByInput ' )
279
+ ->with ($ data ['frontend_input ' ])
280
+ ->willReturnSelf ('test_backend_type ' );
281
+ $ this ->productAttributeMock ->expects ($ this ->once ())
282
+ ->method ('getDefaultValueByInput ' )
283
+ ->with ($ data ['frontend_input ' ])
284
+ ->willReturn (null );
285
+ $ this ->productAttributeMock ->expects ($ this ->once ())
286
+ ->method ('getBackendType ' )
287
+ ->willReturn ('static ' );
288
+ $ this ->productAttributeMock ->expects ($ this ->once ())
289
+ ->method ('getFrontendClass ' )
290
+ ->willReturn ('static ' );
291
+ $ this ->resultFactoryMock ->expects ($ this ->any ())
292
+ ->method ('create ' )
293
+ ->willReturn ($ this ->redirectMock );
294
+ $ this ->redirectMock ->expects ($ this ->any ())
295
+ ->method ('setPath ' )
296
+ ->willReturnSelf ();
297
+
298
+ $ this ->assertInstanceOf (ResultRedirect::class, $ this ->getModel ()->execute ());
299
+ }
300
+
301
+ /**
302
+ * @return void
303
+ * @throws NotFoundException
304
+ */
305
+ public function testExecute (): void
214
306
{
215
307
$ data = [
216
308
'new_attribute_set_name ' => 'Test attribute set name ' ,
@@ -273,9 +365,10 @@ public function testExecute()
273
365
}
274
366
275
367
/**
276
- * @throws \Magento\Framework\Exception\NotFoundException
368
+ * @return void
369
+ * @throws NotFoundException
277
370
*/
278
- public function testExecuteWithOptionsDataError ()
371
+ public function testExecuteWithOptionsDataError (): void
279
372
{
280
373
$ serializedOptions = '{"key":"value"} ' ;
281
374
$ message = "The attribute couldn't be saved due to an error. Verify your information and try again. "
@@ -305,10 +398,10 @@ public function testExecuteWithOptionsDataError()
305
398
* @param string $path
306
399
* @param array $params
307
400
* @param array $response
308
- * @return mixed
401
+ * @return void
309
402
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
310
403
*/
311
- private function addReturnResultConditions (string $ path = '' , array $ params = [], array $ response = [])
404
+ private function addReturnResultConditions (string $ path = '' , array $ params = [], array $ response = []): void
312
405
{
313
406
$ layoutMock = $ this ->getMockBuilder (LayoutInterface::class)
314
407
->setMethods (['initMessages ' , 'getMessagesBlock ' ])
0 commit comments