6
6
namespace Magento \Eav \Test \Unit \Model \Entity \Attribute \Source ;
7
7
8
8
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
9
+ use Magento \Eav \Model \Entity \Attribute \Source \AbstractSource ;
10
+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
11
+ use Magento \Store \Model \StoreManagerInterface ;
12
+ use Magento \Store \Api \Data \StoreInterface ;
13
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Option \Collection as AttributeOptionCollection ;
9
14
10
15
/**
11
16
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -28,6 +33,31 @@ class TableTest extends \PHPUnit_Framework_TestCase
28
33
*/
29
34
private $ attrOptionFactory ;
30
35
36
+ /**
37
+ * @var AbstractSource | \PHPUnit_Framework_MockObject_MockObject
38
+ */
39
+ private $ sourceMock ;
40
+
41
+ /**
42
+ * @var AbstractAttribute | \PHPUnit_Framework_MockObject_MockObject
43
+ */
44
+ private $ abstractAttributeMock ;
45
+
46
+ /**
47
+ * @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
48
+ */
49
+ private $ storeManagerMock ;
50
+
51
+ /**
52
+ * @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
53
+ */
54
+ private $ storeMock ;
55
+
56
+ /**
57
+ * @var AttributeOptionCollection|\PHPUnit_Framework_MockObject_MockObject
58
+ */
59
+ private $ attributeOptionCollectionMock ;
60
+
31
61
protected function setUp ()
32
62
{
33
63
$ objectManager = new ObjectManager ($ this );
@@ -48,20 +78,49 @@ protected function setUp()
48
78
false
49
79
);
50
80
81
+ $ this ->attributeOptionCollectionMock = $ this ->getMockBuilder (AttributeOptionCollection::class)
82
+ ->setMethods (['toOptionArray ' ])
83
+ ->disableOriginalConstructor ()
84
+ ->getMock ();
85
+
51
86
$ this ->attrOptionFactory = $ this ->getMockBuilder (
52
87
\Magento \Eav \Model \ResourceModel \Entity \Attribute \OptionFactory::class
53
88
)
54
89
->setMethods (['create ' ])
55
90
->disableOriginalConstructor ()
56
91
->getMockForAbstractClass ();
57
92
93
+ $ this ->sourceMock = $ this ->getMockBuilder (AbstractSource::class)
94
+ ->disableOriginalConstructor ()
95
+ ->getMockForAbstractClass ();
96
+
97
+ $ this ->abstractAttributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
98
+ ->setMethods (
99
+ [
100
+ 'getFrontend ' , 'getAttributeCode ' , '__wakeup ' , 'getStoreId ' ,
101
+ 'getId ' , 'getIsRequired ' , 'getEntity ' , 'getBackend '
102
+ ]
103
+ )
104
+ ->disableOriginalConstructor ()
105
+ ->getMockForAbstractClass ();
106
+
58
107
$ this ->model = $ objectManager ->getObject (
59
108
\Magento \Eav \Model \Entity \Attribute \Source \Table::class,
60
109
[
61
110
'attrOptionCollectionFactory ' => $ this ->collectionFactory ,
62
111
'attrOptionFactory ' => $ this ->attrOptionFactory
63
112
]
64
113
);
114
+ $ this ->model ->setAttribute ($ this ->abstractAttributeMock );
115
+
116
+ $ this ->storeManagerMock = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
117
+ $ this ->storeMock = $ this ->getMockForAbstractClass (StoreInterface::class);
118
+
119
+ $ objectManager ->setBackwardCompatibleProperty (
120
+ $ this ->model ,
121
+ 'storeManager ' ,
122
+ $ this ->storeManagerMock
123
+ );
65
124
}
66
125
67
126
public function testGetFlatColumns ()
@@ -74,25 +133,8 @@ public function testGetFlatColumns()
74
133
false
75
134
);
76
135
77
- $ abstractAttributeMock = $ this ->getMock (
78
- \Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class,
79
- ['getFrontend ' , 'getAttributeCode ' , '__wakeup ' ],
80
- [],
81
- '' ,
82
- false
83
- );
84
-
85
- $ abstractAttributeMock ->expects (
86
- $ this ->any ()
87
- )->method (
88
- 'getFrontend '
89
- )->will (
90
- $ this ->returnValue ($ abstractFrontendMock )
91
- );
92
-
93
- $ abstractAttributeMock ->expects ($ this ->any ())->method ('getAttributeCode ' )->will ($ this ->returnValue ('code ' ));
94
-
95
- $ this ->model ->setAttribute ($ abstractAttributeMock );
136
+ $ this ->abstractAttributeMock ->expects ($ this ->any ())->method ('getFrontend ' )->willReturn (($ abstractFrontendMock ));
137
+ $ this ->abstractAttributeMock ->expects ($ this ->any ())->method ('getAttributeCode ' )->willReturn ('code ' );
96
138
97
139
$ flatColumns = $ this ->model ->getFlatColumns ();
98
140
@@ -121,25 +163,16 @@ public function testGetSpecificOptions($optionIds, $withEmpty)
121
163
$ storeId = 5 ;
122
164
$ options = [['label ' => 'The label ' , 'value ' => 'A value ' ]];
123
165
124
- $ attribute = $ this ->getMock (
125
- \Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class,
126
- ['getId ' , 'getStoreId ' , 'getIsRequired ' , '__wakeup ' ],
127
- [],
128
- '' ,
129
- false
130
- );
131
- $ attribute ->expects ($ this ->once ())
166
+ $ this ->abstractAttributeMock ->expects ($ this ->once ())
132
167
->method ('getId ' )
133
168
->willReturn ($ attributeId );
134
- $ attribute ->expects ($ this ->once ())
169
+ $ this -> abstractAttributeMock ->expects ($ this ->once ())
135
170
->method ('getStoreId ' )
136
171
->willReturn ($ storeId );
137
- $ attribute ->expects ($ this ->any ())
172
+ $ this -> abstractAttributeMock ->expects ($ this ->any ())
138
173
->method ('getIsRequired ' )
139
174
->willReturn (false );
140
175
141
- $ this ->model ->setAttribute ($ attribute );
142
-
143
176
$ this ->collectionFactory ->expects ($ this ->once ())
144
177
->method ('create ' )
145
178
->willReturnSelf ();
@@ -191,22 +224,14 @@ public function testGetOptionText($optionsIds, $value, $options, $expectedResult
191
224
{
192
225
$ attributeId = 1 ;
193
226
$ storeId = 5 ;
194
- $ attribute = $ this ->getMock (
195
- \Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class,
196
- ['getId ' , 'getStoreId ' , '__wakeup ' ],
197
- [],
198
- '' ,
199
- false
200
- );
201
- $ attribute ->expects ($ this ->once ())
227
+
228
+ $ this ->abstractAttributeMock ->expects ($ this ->once ())
202
229
->method ('getId ' )
203
230
->willReturn ($ attributeId );
204
- $ attribute ->expects ($ this ->once ())
231
+ $ this -> abstractAttributeMock ->expects ($ this ->once ())
205
232
->method ('getStoreId ' )
206
233
->willReturn ($ storeId );
207
234
208
- $ this ->model ->setAttribute ($ attribute );
209
-
210
235
$ this ->collectionFactory ->expects ($ this ->once ())
211
236
->method ('create ' )
212
237
->willReturnSelf ();
@@ -257,16 +282,13 @@ public function testAddValueSortToCollection()
257
282
->setMethods ([ 'getSelect ' , 'getStoreId ' ])
258
283
->disableOriginalConstructor ()
259
284
->getMockForAbstractClass ();
260
- $ attribute = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class)
261
- ->setMethods (['getAttributeCode ' , 'getEntity ' , 'getBackend ' , 'getId ' ])
262
- ->disableOriginalConstructor ()
263
- ->getMockForAbstractClass ();
264
- $ attribute ->expects ($ this ->any ())->method ('getAttributeCode ' )->willReturn ($ attributeCode );
285
+
286
+ $ this ->abstractAttributeMock ->expects ($ this ->any ())->method ('getAttributeCode ' )->willReturn ($ attributeCode );
265
287
$ entity = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \AbstractEntity::class)
266
288
->setMethods (['getLinkField ' ])
267
289
->disableOriginalConstructor ()
268
290
->getMockForAbstractClass ();
269
- $ attribute ->expects ($ this ->once ())->method ('getEntity ' )->willReturn ($ entity );
291
+ $ this -> abstractAttributeMock ->expects ($ this ->once ())->method ('getEntity ' )->willReturn ($ entity );
270
292
$ entity ->expects ($ this ->once ())->method ('getLinkField ' )->willReturn ('entity_id ' );
271
293
$ select = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
272
294
->setMethods (['joinLeft ' , 'getConnection ' , 'order ' ])
@@ -278,9 +300,9 @@ public function testAddValueSortToCollection()
278
300
->setMethods (['getTable ' ])
279
301
->disableOriginalConstructor ()
280
302
->getMockForAbstractClass ();
281
- $ attribute ->expects ($ this ->any ())->method ('getBackend ' )->willReturn ($ backend );
303
+ $ this -> abstractAttributeMock ->expects ($ this ->any ())->method ('getBackend ' )->willReturn ($ backend );
282
304
$ backend ->expects ($ this ->any ())->method ('getTable ' )->willReturn ('table_name ' );
283
- $ attribute ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
305
+ $ this -> abstractAttributeMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
284
306
$ collection ->expects ($ this ->once ())->method ('getStoreId ' )->willReturn (1 );
285
307
$ connection = $ this ->getMockBuilder (\Magento \Framework \DB \Adapter \AdapterInterface::class)
286
308
->disableOriginalConstructor ()
@@ -294,11 +316,99 @@ public function testAddValueSortToCollection()
294
316
->disableOriginalConstructor ()
295
317
->getMock ();
296
318
$ this ->attrOptionFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ attrOption );
297
- $ attrOption ->expects ($ this ->once ())->method ('addOptionValueToCollection ' )->with ($ collection , $ attribute , $ expr )
319
+ $ attrOption ->expects ($ this ->once ())->method ('addOptionValueToCollection ' )
320
+ ->with ($ collection , $ this ->abstractAttributeMock , $ expr )
298
321
->willReturnSelf ();
299
322
$ select ->expects ($ this ->once ())->method ('order ' )->with ("{$ attributeCode } {$ dir }" );
300
323
301
- $ this ->model ->setAttribute ($ attribute );
302
324
$ this ->assertEquals ($ this ->model , $ this ->model ->addValueSortToCollection ($ collection , $ dir ));
303
325
}
326
+
327
+ /**
328
+ * @param bool $withEmpty
329
+ * @param bool $defaultValues
330
+ * @param array $options
331
+ * @param array $optionsDefault
332
+ * @param array $expectedResult
333
+ * @dataProvider getAllOptionsDataProvider
334
+ */
335
+ public function testGetAllOptions (
336
+ $ withEmpty ,
337
+ $ defaultValues ,
338
+ array $ options ,
339
+ array $ optionsDefault ,
340
+ array $ expectedResult
341
+ ) {
342
+ $ storeId = '1 ' ;
343
+ $ attributeId = '42 ' ;
344
+
345
+ $ this ->abstractAttributeMock ->expects ($ this ->once ())->method ('getStoreId ' )->willReturn (null );
346
+
347
+ $ this ->storeManagerMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ this ->storeMock );
348
+ $ this ->storeMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ storeId );
349
+
350
+ $ this ->abstractAttributeMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ attributeId );
351
+
352
+ $ this ->collectionFactory ->expects ($ this ->once ())
353
+ ->method ('create ' )
354
+ ->willReturnSelf ();
355
+ $ this ->collectionFactory ->expects ($ this ->once ())
356
+ ->method ('setPositionOrder ' )
357
+ ->willReturnSelf ();
358
+ $ this ->collectionFactory ->expects ($ this ->once ())
359
+ ->method ('setAttributeFilter ' )
360
+ ->with ($ attributeId )
361
+ ->willReturnSelf ();
362
+ $ this ->collectionFactory ->expects ($ this ->once ())
363
+ ->method ('setStoreFilter ' )
364
+ ->with ($ storeId )
365
+ ->willReturnSelf ();
366
+ $ this ->collectionFactory ->expects ($ this ->once ())
367
+ ->method ('load ' )
368
+ ->willReturn ($ this ->attributeOptionCollectionMock );
369
+ $ this ->attributeOptionCollectionMock ->expects ($ this ->any ())
370
+ ->method ('toOptionArray ' )
371
+ ->willReturnMap (
372
+ [
373
+ ['value ' , $ options ],
374
+ ['default_value ' , $ optionsDefault ]
375
+ ]
376
+ );
377
+
378
+ $ this ->assertEquals ($ expectedResult , $ this ->model ->getAllOptions ($ withEmpty , $ defaultValues ));
379
+ }
380
+
381
+ /**
382
+ * @return array
383
+ */
384
+ public function getAllOptionsDataProvider ()
385
+ {
386
+ return [
387
+ [
388
+ false ,
389
+ false ,
390
+ [['value ' => '16 ' , 'label ' => 'black ' ], ['value ' => '17 ' , 'label ' => 'white ' ]],
391
+ [['value ' => '16 ' , 'label ' => 'blck ' ], ['value ' => '17 ' , 'label ' => 'wht ' ]],
392
+ [['value ' => '16 ' , 'label ' => 'black ' ], ['value ' => '17 ' , 'label ' => 'white ' ]]
393
+ ],
394
+ [
395
+ false ,
396
+ true ,
397
+ [['value ' => '16 ' , 'label ' => 'black ' ], ['value ' => '17 ' , 'label ' => 'white ' ]],
398
+ [['value ' => '16 ' , 'label ' => 'blck ' ], ['value ' => '17 ' , 'label ' => 'wht ' ]],
399
+ [['value ' => '16 ' , 'label ' => 'blck ' ], ['value ' => '17 ' , 'label ' => 'wht ' ]]
400
+ ],
401
+ [
402
+ true ,
403
+ false ,
404
+ [['value ' => '16 ' , 'label ' => 'black ' ], ['value ' => '17 ' , 'label ' => 'white ' ]],
405
+ [['value ' => '16 ' , 'label ' => 'blck ' ], ['value ' => '17 ' , 'label ' => 'wht ' ]],
406
+ [
407
+ ['label ' => ' ' , 'value ' => '' ],
408
+ ['value ' => '16 ' , 'label ' => 'black ' ],
409
+ ['value ' => '17 ' , 'label ' => 'white ' ]
410
+ ]
411
+ ]
412
+ ];
413
+ }
304
414
}
0 commit comments