3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \ConfigurableProduct \Test \Unit \Model ;
7
8
8
9
use Magento \ConfigurableProduct \Model \AttributeOptionProvider ;
10
+ use Magento \Eav \Model \Entity \Attribute \Source \AbstractSource ;
9
11
use Magento \Framework \DB \Select ;
10
12
use Magento \Framework \App \ScopeResolverInterface ;
11
13
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
@@ -95,7 +97,7 @@ protected function setUp()
95
97
->disableOriginalConstructor ()
96
98
->getMockForAbstractClass ();
97
99
$ this ->select = $ this ->getMockBuilder (Select::class)
98
- ->setMethods (['from ' , 'joinInner ' , 'joinLeft ' , 'where ' ])
100
+ ->setMethods (['from ' , 'joinInner ' , 'joinLeft ' , 'where ' , ' columns ' ])
99
101
->disableOriginalConstructor ()
100
102
->getMock ();
101
103
$ this ->connectionMock ->expects ($ this ->any ())
@@ -109,7 +111,7 @@ protected function setUp()
109
111
->disableOriginalConstructor ()
110
112
->getMockForAbstractClass ();
111
113
$ this ->abstractAttribute = $ this ->getMockBuilder (AbstractAttribute::class)
112
- ->setMethods (['getBackendTable ' , 'getAttributeId ' ])
114
+ ->setMethods (['getBackendTable ' , 'getAttributeId ' , ' getSourceModel ' , ' getSource ' ])
113
115
->disableOriginalConstructor ()
114
116
->getMockForAbstractClass ();
115
117
$ this ->scope = $ this ->getMockBuilder (ScopeInterface::class)
@@ -157,14 +159,65 @@ public function testGetAttributeOptions(array $options)
157
159
$ this ->scopeResolver ->expects ($ this ->any ())->method ('getScope ' )->willReturn ($ this ->scope );
158
160
$ this ->scope ->expects ($ this ->any ())->method ('getId ' )->willReturn (123 );
159
161
160
- $ this ->select ->expects ($ this ->any ())->method ('from ' )->willReturnSelf ();
161
- $ this ->select ->expects ($ this ->any ())->method ('joinInner ' )->willReturnSelf ();
162
- $ this ->select ->expects ($ this ->any ())->method ('joinLeft ' )->willReturnSelf ();
163
- $ this ->select ->expects ($ this ->any ())->method ('where ' )->willReturnSelf ();
162
+ $ this ->select ->expects ($ this ->exactly (1 ))->method ('from ' )->willReturnSelf ();
163
+ $ this ->select ->expects ($ this ->exactly (1 ))->method ('columns ' )->willReturnSelf ();
164
+ $ this ->select ->expects ($ this ->exactly (5 ))->method ('joinInner ' )->willReturnSelf ();
165
+ $ this ->select ->expects ($ this ->exactly (3 ))->method ('joinLeft ' )->willReturnSelf ();
166
+ $ this ->select ->expects ($ this ->exactly (2 ))->method ('where ' )->willReturnSelf ();
167
+
168
+ $ this ->abstractAttribute ->expects ($ this ->any ())
169
+ ->method ('getBackendTable ' )
170
+ ->willReturn ('getBackendTable value ' );
171
+ $ this ->abstractAttribute ->expects ($ this ->any ())
172
+ ->method ('getAttributeId ' )
173
+ ->willReturn ('getAttributeId value ' );
174
+
175
+ $ this ->connectionMock ->expects ($ this ->once ())
176
+ ->method ('fetchAll ' )
177
+ ->willReturn ($ options );
178
+
179
+ $ this ->assertEquals (
180
+ $ options ,
181
+ $ this ->model ->getAttributeOptions ($ this ->abstractAttribute , 1 )
182
+ );
183
+ }
184
+
185
+ /**
186
+ * @param array $options
187
+ * @dataProvider testOptionsWithBackendModelDataProvider
188
+ */
189
+ public function testGetAttributeOptionsWithBackendModel (array $ options )
190
+ {
191
+ $ this ->scopeResolver ->expects ($ this ->any ())->method ('getScope ' )->willReturn ($ this ->scope );
192
+ $ this ->scope ->expects ($ this ->any ())->method ('getId ' )->willReturn (123 );
193
+
194
+ $ this ->select ->expects ($ this ->exactly (1 ))->method ('from ' )->willReturnSelf ();
195
+ $ this ->select ->expects ($ this ->exactly (0 ))->method ('columns ' )->willReturnSelf ();
196
+ $ this ->select ->expects ($ this ->exactly (5 ))->method ('joinInner ' )->willReturnSelf ();
197
+ $ this ->select ->expects ($ this ->exactly (1 ))->method ('joinLeft ' )->willReturnSelf ();
198
+ $ this ->select ->expects ($ this ->exactly (2 ))->method ('where ' )->willReturnSelf ();
164
199
200
+ $ source = $ this ->getMockBuilder (AbstractSource::class)
201
+ ->disableOriginalConstructor ()
202
+ ->setMethods (['getAllOptions ' ])
203
+ ->getMockForAbstractClass ();
204
+ $ source ->expects ($ this ->any ())
205
+ ->method ('getAllOptions ' )
206
+ ->willReturn ([
207
+ ['value ' => 13 , 'label ' => 'Option Value for index 13 ' ],
208
+ ['value ' => 14 , 'label ' => 'Option Value for index 14 ' ],
209
+ ['value ' => 15 , 'label ' => 'Option Value for index 15 ' ]
210
+ ]);
211
+
212
+ $ this ->abstractAttribute ->expects ($ this ->atLeastOnce ())
213
+ ->method ('getSource ' )
214
+ ->willReturn ($ source );
165
215
$ this ->abstractAttribute ->expects ($ this ->any ())
166
216
->method ('getBackendTable ' )
167
217
->willReturn ('getBackendTable value ' );
218
+ $ this ->abstractAttribute ->expects ($ this ->any ())
219
+ ->method ('getSourceModel ' )
220
+ ->willReturn ('getSourceModel value ' );
168
221
$ this ->abstractAttribute ->expects ($ this ->any ())
169
222
->method ('getAttributeId ' )
170
223
->willReturn ('getAttributeId value ' );
@@ -192,24 +245,61 @@ public function testOptionsDataProvider()
192
245
'product_id ' => 4 ,
193
246
'attribute_code ' => 'color ' ,
194
247
'value_index ' => '13 ' ,
195
- 'option_title ' => 'Black '
248
+ 'option_title ' => 'Black ' ,
196
249
],
197
250
[
198
251
'sku ' => 'Configurable1-White ' ,
199
252
'product_id ' => 4 ,
200
253
'attribute_code ' => 'color ' ,
201
254
'value_index ' => '14 ' ,
202
- 'option_title ' => 'White '
255
+ 'option_title ' => 'White ' ,
203
256
],
204
257
[
205
258
'sku ' => 'Configurable1-Red ' ,
206
259
'product_id ' => 4 ,
207
260
'attribute_code ' => 'color ' ,
208
261
'value_index ' => '15 ' ,
209
- 'option_title ' => 'Red '
210
- ]
211
- ]
212
- ]
262
+ 'option_title ' => 'Red ' ,
263
+ ],
264
+ ],
265
+ ],
266
+ ];
267
+ }
268
+
269
+ /**
270
+ * @return array
271
+ */
272
+ public function testOptionsWithBackendModelDataProvider ()
273
+ {
274
+ return [
275
+ [
276
+ [
277
+ [
278
+ 'sku ' => 'Configurable1-Black ' ,
279
+ 'product_id ' => 4 ,
280
+ 'attribute_code ' => 'color ' ,
281
+ 'value_index ' => '13 ' ,
282
+ 'option_title ' => 'Option Value for index 13 ' ,
283
+ 'default_title ' => 'Option Value for index 13 ' ,
284
+ ],
285
+ [
286
+ 'sku ' => 'Configurable1-White ' ,
287
+ 'product_id ' => 4 ,
288
+ 'attribute_code ' => 'color ' ,
289
+ 'value_index ' => '14 ' ,
290
+ 'option_title ' => 'Option Value for index 14 ' ,
291
+ 'default_title ' => 'Option Value for index 14 ' ,
292
+ ],
293
+ [
294
+ 'sku ' => 'Configurable1-Red ' ,
295
+ 'product_id ' => 4 ,
296
+ 'attribute_code ' => 'color ' ,
297
+ 'value_index ' => '15 ' ,
298
+ 'option_title ' => 'Option Value for index 15 ' ,
299
+ 'default_title ' => 'Option Value for index 15 ' ,
300
+ ],
301
+ ],
302
+ ],
213
303
];
214
304
}
215
305
}
0 commit comments