9
9
10
10
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
11
11
use Magento \Catalog \Ui \Component \ColumnFactory ;
12
+ use Magento \Eav \Model \Entity \Attribute \Source \AbstractSource ;
12
13
use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
13
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
14
use Magento \Framework \View \Element \UiComponent \ContextInterface ;
15
15
use Magento \Framework \View \Element \UiComponentFactory ;
16
16
use Magento \Ui \Component \Filters \FilterModifier ;
@@ -28,11 +28,6 @@ class ColumnFactoryTest extends TestCase
28
28
*/
29
29
private $ columnFactory ;
30
30
31
- /**
32
- * @var ObjectManager
33
- */
34
- private $ objectManager ;
35
-
36
31
/**
37
32
* @var Attribute|MockObject
38
33
*/
@@ -63,32 +58,15 @@ class ColumnFactoryTest extends TestCase
63
58
*/
64
59
protected function setUp (): void
65
60
{
66
- $ this ->objectManager = new ObjectManager ($ this );
67
-
68
- $ this ->attribute = $ this ->createPartialMock (
69
- Attribute::class,
70
- [
71
- 'getAttributeCode ' ,
72
- 'getIsFilterableInGrid ' ,
73
- 'getFrontendInput ' ,
74
- 'getDefaultFrontendLabel ' ,
75
- 'getIsVisibleInGrid ' ,
76
- ]
77
- );
78
- $ this ->context = $ this ->getMockForAbstractClass (ContextInterface::class);
61
+ $ this ->attribute = $ this ->createMock (Attribute::class);
62
+ $ this ->context = $ this ->createMock (ContextInterface::class);
79
63
$ this ->uiComponentFactory = $ this ->createMock (UiComponentFactory::class);
80
- $ this ->column = $ this ->getMockForAbstractClass (ColumnInterface::class);
64
+ $ this ->column = $ this ->createMock (ColumnInterface::class);
81
65
$ this ->uiComponentFactory ->method ('create ' )
82
66
->willReturn ($ this ->column );
83
- $ this ->timezone = $ this ->getMockForAbstractClass (TimezoneInterface::class);
67
+ $ this ->timezone = $ this ->createMock (TimezoneInterface::class);
84
68
85
- $ this ->columnFactory = $ this ->objectManager ->getObject (
86
- ColumnFactory::class,
87
- [
88
- 'componentFactory ' => $ this ->uiComponentFactory ,
89
- 'timezone ' => $ this ->timezone ,
90
- ]
91
- );
69
+ $ this ->columnFactory = new ColumnFactory ($ this ->uiComponentFactory , $ this ->timezone );
92
70
}
93
71
94
72
/**
@@ -268,4 +246,73 @@ public function createDateColumnDataProvider(): array
268
246
],
269
247
];
270
248
}
249
+
250
+ public function testCreateAttributeWithSource (): void
251
+ {
252
+ $ this ->context ->method ('getRequestParam ' )
253
+ ->with (FilterModifier::FILTER_MODIFIER , [])
254
+ ->willReturn ([]);
255
+ $ attributeCode = 'color ' ;
256
+ $ this ->attribute ->expects ($ this ->atLeastOnce ())
257
+ ->method ('getAttributeCode ' )
258
+ ->willReturn ($ attributeCode );
259
+ $ label = 'Color ' ;
260
+ $ this ->attribute ->expects ($ this ->atLeastOnce ())
261
+ ->method ('getDefaultFrontendLabel ' )
262
+ ->willReturn ($ label );
263
+ $ this ->attribute ->expects ($ this ->atLeastOnce ())
264
+ ->method ('getFrontendInput ' )
265
+ ->willReturn ('select ' );
266
+ $ this ->attribute ->expects ($ this ->atLeastOnce ())
267
+ ->method ('getIsVisibleInGrid ' )
268
+ ->willReturn (true );
269
+ $ this ->attribute ->expects ($ this ->atLeastOnce ())
270
+ ->method ('getIsFilterableInGrid ' )
271
+ ->willReturn (true );
272
+ $ this ->attribute ->expects ($ this ->atLeastOnce ())
273
+ ->method ('usesSource ' )
274
+ ->willReturn (true );
275
+ $ source = $ this ->createMock (AbstractSource::class);
276
+ $ this ->attribute ->expects ($ this ->atLeastOnce ())
277
+ ->method ('getSource ' )
278
+ ->willReturn ($ source );
279
+ $ options = [
280
+ ['label ' => '' ],
281
+ ['label ' => 'admin1 ' ],
282
+ ['label ' => 'admin2 ' ],
283
+ ];
284
+ $ source ->expects ($ this ->atLeastOnce ())
285
+ ->method ('getAllOptions ' )
286
+ ->with (true , true )
287
+ ->willReturn ($ options );
288
+
289
+ $ expectedConfig = [
290
+ 'label ' => __ ($ label ),
291
+ 'dataType ' => 'select ' ,
292
+ 'add_field ' => true ,
293
+ 'visible ' => true ,
294
+ 'filter ' => 'select ' ,
295
+ 'component ' => 'Magento_Ui/js/grid/columns/select ' ,
296
+ 'options ' => array_map (
297
+ function (array $ option ) {
298
+ $ option ['__disableTmpl ' ] = true ;
299
+ return $ option ;
300
+ },
301
+ $ options
302
+ ),
303
+ ];
304
+ $ expectedArguments = [
305
+ 'data ' => ['config ' => $ expectedConfig ],
306
+ 'context ' => $ this ->context ,
307
+ ];
308
+ $ this ->uiComponentFactory ->expects ($ this ->once ())
309
+ ->method ('create ' )
310
+ ->with ($ attributeCode , 'column ' , $ expectedArguments )
311
+ ->willReturn ($ this ->column );
312
+
313
+ $ this ->assertEquals (
314
+ $ this ->column ,
315
+ $ this ->columnFactory ->create ($ this ->attribute , $ this ->context )
316
+ );
317
+ }
271
318
}
0 commit comments