15
15
use Magento \Framework \ObjectManagerInterface ;
16
16
use Magento \Framework \View \LayoutFactory ;
17
17
use Magento \Framework \View \LayoutInterface ;
18
- use Magento \Framework \DataObject ;
19
18
20
19
/**
21
20
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -62,24 +61,20 @@ class ValidateTest extends AttributeTest
62
61
*/
63
62
protected $ layoutMock ;
64
63
65
- /**
66
- * @var DataObject
67
- */
68
- protected $ responseDataObject ;
69
-
70
64
protected function setUp ()
71
65
{
72
66
parent ::setUp ();
73
67
$ this ->resultJsonFactoryMock = $ this ->getMockBuilder (ResultJsonFactory::class)
74
68
->disableOriginalConstructor ()
75
- ->setMethods (['create ' ])
76
69
->getMock ();
77
70
$ this ->resultJson = $ this ->getMockBuilder (ResultJson::class)
78
71
->disableOriginalConstructor ()
79
72
->getMock ();
80
73
$ this ->layoutFactoryMock = $ this ->getMockBuilder (LayoutFactory::class)
81
74
->disableOriginalConstructor ()
82
75
->getMock ();
76
+ $ this ->objectManagerMock = $ this ->getMockBuilder (ObjectManagerInterface::class)
77
+ ->getMockForAbstractClass ();
83
78
$ this ->attributeMock = $ this ->getMockBuilder (Attribute::class)
84
79
->disableOriginalConstructor ()
85
80
->getMock ();
@@ -91,9 +86,10 @@ protected function setUp()
91
86
->getMock ();
92
87
$ this ->layoutMock = $ this ->getMockBuilder (LayoutInterface::class)
93
88
->getMockForAbstractClass ();
94
- $ this ->responseDataObject = $ this ->getMockBuilder (DataObject::class)
95
- ->setMethods (null )
96
- ->getMock ();
89
+
90
+ $ this ->contextMock ->expects ($ this ->any ())
91
+ ->method ('getObjectManager ' )
92
+ ->willReturn ($ this ->objectManagerMock );
97
93
}
98
94
99
95
/**
@@ -104,17 +100,13 @@ protected function getModel()
104
100
return $ this ->objectManager ->getObject (
105
101
Validate::class,
106
102
[
107
- 'context ' => $ this ->contextMock ,
108
- 'attributeLabelCache ' => $ this ->attributeLabelCacheMock ,
109
- 'coreRegistry ' => $ this ->coreRegistryMock ,
110
- 'resultPageFactory ' => $ this ->resultPageFactoryMock ,
111
- 'resultJsonFactory ' => $ this ->resultJsonFactoryMock ,
112
- 'layoutFactory ' => $ this ->layoutFactoryMock ,
113
- 'multipleAttributeList ' => ['select ' => 'option ' , 'multipleselect ' => 'option ' ],
114
- 'attributeResource ' => $ this ->attributeMock ,
115
- 'attributeSetResource ' => $ this ->attributeSetMock ,
116
- 'escaper ' => $ this ->escaperMock ,
117
- 'responseObject ' => $ this ->responseDataObject ,
103
+ 'context ' => $ this ->contextMock ,
104
+ 'attributeLabelCache ' => $ this ->attributeLabelCacheMock ,
105
+ 'coreRegistry ' => $ this ->coreRegistryMock ,
106
+ 'resultPageFactory ' => $ this ->resultPageFactoryMock ,
107
+ 'resultJsonFactory ' => $ this ->resultJsonFactoryMock ,
108
+ 'layoutFactory ' => $ this ->layoutFactoryMock ,
109
+ 'multipleAttributeList ' => ['select ' => 'option ' ]
118
110
]
119
111
);
120
112
}
@@ -128,9 +120,22 @@ public function testExecute()
128
120
['attribute_code ' , null , 'test_attribute_code ' ],
129
121
['new_attribute_set_name ' , null , 'test_attribute_set_name ' ],
130
122
]);
123
+ $ this ->objectManagerMock ->expects ($ this ->exactly (2 ))
124
+ ->method ('create ' )
125
+ ->willReturnMap ([
126
+ [\Magento \Catalog \Model \ResourceModel \Eav \Attribute::class, [], $ this ->attributeMock ],
127
+ [\Magento \Eav \Model \Entity \Attribute \Set::class, [], $ this ->attributeSetMock ]
128
+ ]);
131
129
$ this ->attributeMock ->expects ($ this ->once ())
132
130
->method ('loadByCode ' )
133
131
->willReturnSelf ();
132
+ $ this ->requestMock ->expects ($ this ->once ())
133
+ ->method ('has ' )
134
+ ->with ('new_attribute_set_name ' )
135
+ ->willReturn (true );
136
+ $ this ->attributeSetMock ->expects ($ this ->once ())
137
+ ->method ('setEntityTypeId ' )
138
+ ->willReturnSelf ();
134
139
$ this ->attributeSetMock ->expects ($ this ->once ())
135
140
->method ('load ' )
136
141
->willReturnSelf ();
@@ -150,11 +155,13 @@ public function testExecute()
150
155
/**
151
156
* @dataProvider provideUniqueData
152
157
* @param array $options
158
+ * @param boolean $isError
153
159
* @throws \Magento\Framework\Exception\NotFoundException
154
160
*/
155
- public function testUniqueValidation (array $ options )
161
+ public function testUniqueValidation (array $ options, $ isError )
156
162
{
157
- $ this ->requestMock ->expects ($ this ->any ())
163
+ $ countFunctionCalls = ($ isError ) ? 6 : 5 ;
164
+ $ this ->requestMock ->expects ($ this ->exactly ($ countFunctionCalls ))
158
165
->method ('getParam ' )
159
166
->willReturnMap ([
160
167
['frontend_label ' , null , null ],
@@ -164,10 +171,19 @@ public function testUniqueValidation(array $options)
164
171
['message_key ' , null , Validate::DEFAULT_MESSAGE_KEY ]
165
172
]);
166
173
174
+ $ this ->objectManagerMock ->expects ($ this ->once ())
175
+ ->method ('create ' )
176
+ ->willReturn ($ this ->attributeMock );
177
+
167
178
$ this ->attributeMock ->expects ($ this ->once ())
168
179
->method ('loadByCode ' )
169
180
->willReturnSelf ();
170
181
182
+ $ this ->requestMock ->expects ($ this ->once ())
183
+ ->method ('has ' )
184
+ ->with ('new_attribute_set_name ' )
185
+ ->willReturn (false );
186
+
171
187
$ this ->resultJsonFactoryMock ->expects ($ this ->once ())
172
188
->method ('create ' )
173
189
->willReturn ($ this ->resultJson );
@@ -189,7 +205,7 @@ public function provideUniqueData()
189
205
"option_1 " => "" ,
190
206
"option_2 " => "" ,
191
207
]
192
- ]
208
+ ], false
193
209
],
194
210
'valid options ' => [
195
211
[
@@ -203,7 +219,7 @@ public function provideUniqueData()
203
219
"option_1 " => "" ,
204
220
"option_2 " => "" ,
205
221
]
206
- ]
222
+ ], false
207
223
],
208
224
'duplicate options ' => [
209
225
[
@@ -217,7 +233,7 @@ public function provideUniqueData()
217
233
"option_1 " => "" ,
218
234
"option_2 " => "" ,
219
235
]
220
- ]
236
+ ], true
221
237
],
222
238
'duplicate and deleted ' => [
223
239
[
@@ -231,7 +247,7 @@ public function provideUniqueData()
231
247
"option_1 " => "1 " ,
232
248
"option_2 " => "" ,
233
249
]
234
- ]
250
+ ], false
235
251
],
236
252
];
237
253
}
@@ -253,9 +269,13 @@ public function testEmptyOption(array $options, $result)
253
269
['attribute_code ' , null , "test_attribute_code " ],
254
270
['new_attribute_set_name ' , null , 'test_attribute_set_name ' ],
255
271
['option ' , null , $ options ],
256
- [Validate:: ARRAY_MESSAGE_KEY , Validate::DEFAULT_MESSAGE_KEY , Validate:: DEFAULT_MESSAGE_KEY ]
272
+ [' message_key ' , Validate::DEFAULT_MESSAGE_KEY , ' message ' ],
257
273
]);
258
274
275
+ $ this ->objectManagerMock ->expects ($ this ->once ())
276
+ ->method ('create ' )
277
+ ->willReturn ($ this ->attributeMock );
278
+
259
279
$ this ->attributeMock ->expects ($ this ->once ())
260
280
->method ('loadByCode ' )
261
281
->willReturnSelf ();
@@ -289,9 +309,7 @@ public function provideEmptyOption()
289
309
],
290
310
(object ) [
291
311
'error ' => true ,
292
- 'message ' => [
293
- 'The value of Admin scope can \'t be empty. '
294
- ]
312
+ 'message ' => 'The value of Admin scope can \'t be empty. ' ,
295
313
]
296
314
],
297
315
'not empty admin scope options ' => [
0 commit comments