11
11
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
12
12
use Magento \Framework \View \Element \UiComponent \ContextInterface ;
13
13
use Magento \Framework \View \Element \UiComponent \DataProvider \DataProviderInterface ;
14
+ use Magento \Framework \View \Element \UiComponent \Processor ;
15
+ use PHPUnit \Framework \TestCase ;
14
16
15
17
/**
16
- * Class ColumnTest
18
+ * Testing for generic UI column classes & for custom ones such as Websites
17
19
*/
18
- class ColumnTest extends \ PHPUnit \ Framework \ TestCase
20
+ class ColumnTest extends TestCase
19
21
{
20
22
/**
21
23
* @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -27,6 +29,23 @@ class ColumnTest extends \PHPUnit\Framework\TestCase
27
29
*/
28
30
protected $ objectManager ;
29
31
32
+ /**
33
+ * @var UiComponentFactory
34
+ */
35
+ protected $ uiComponentFactoryMock ;
36
+
37
+ protected $ dataProviderMock ;
38
+
39
+ /**
40
+ * @var string
41
+ */
42
+ protected $ columnClass = Column::class;
43
+
44
+ /**
45
+ * @var string
46
+ */
47
+ protected $ columnName = Column::NAME ;
48
+
30
49
/**
31
50
* Set up
32
51
*/
@@ -43,6 +62,8 @@ protected function setUp()
43
62
true ,
44
63
[]
45
64
);
65
+
66
+ $ this ->uiComponentFactoryMock = $ this ->createMock (UiComponentFactory::class);
46
67
}
47
68
48
69
/**
@@ -54,7 +75,7 @@ public function testGetComponentName()
54
75
{
55
76
$ this ->contextMock ->expects ($ this ->never ())->method ('getProcessor ' );
56
77
$ column = $ this ->objectManager ->getObject (
57
- \ Magento \ Ui \ Component \ Listing \ Columns \Column::class ,
78
+ $ this -> columnClass ,
58
79
[
59
80
'context ' => $ this ->contextMock ,
60
81
'data ' => [
@@ -68,7 +89,7 @@ public function testGetComponentName()
68
89
]
69
90
);
70
91
71
- $ this ->assertEquals ($ column ->getComponentName (), Column:: NAME . '.testType ' );
92
+ $ this ->assertEquals ($ column ->getComponentName (), $ this -> columnName . '.testType ' );
72
93
}
73
94
74
95
/**
@@ -80,7 +101,7 @@ public function testPrepareItems()
80
101
{
81
102
$ testItems = ['item1 ' ,'item2 ' , 'item3 ' ];
82
103
$ column = $ this ->objectManager ->getObject (
83
- \ Magento \ Ui \ Component \ Listing \ Columns \Column::class ,
104
+ $ this -> columnClass ,
84
105
['context ' => $ this ->contextMock ]
85
106
);
86
107
@@ -90,57 +111,70 @@ public function testPrepareItems()
90
111
/**
91
112
* Run test prepare method
92
113
*
114
+ * @param null $dataProviderMock
93
115
* @return void
94
116
*/
95
117
public function testPrepare ()
96
118
{
97
- $ processor = $ this ->getMockBuilder (\Magento \Framework \View \Element \UiComponent \Processor::class)
98
- ->disableOriginalConstructor ()
99
- ->getMock ();
100
- $ this ->contextMock ->expects ($ this ->atLeastOnce ())->method ('getProcessor ' )->willReturn ($ processor );
101
119
$ data = [
102
120
'name ' => 'test_name ' ,
103
121
'js_config ' => ['extends ' => 'test_config_extends ' ],
104
122
'config ' => ['dataType ' => 'test_type ' , 'sortable ' => true ]
105
123
];
106
124
107
- /** @var UiComponentFactory|\PHPUnit_Framework_MockObject_MockObject $uiComponentFactoryMock */
108
- $ uiComponentFactoryMock = $ this ->createMock (\Magento \Framework \View \Element \UiComponentFactory::class);
125
+ /** @var Column $column */
126
+ $ column = $ this ->objectManager ->getObject (
127
+ $ this ->columnClass ,
128
+ [
129
+ 'context ' => $ this ->contextMock ,
130
+ 'uiComponentFactory ' => $ this ->uiComponentFactoryMock ,
131
+ 'data ' => $ data
132
+ ]
133
+ );
109
134
110
- /** @var UiComponentInterface|\PHPUnit_Framework_MockObject_MockObject $wrappedComponentMock */
135
+ /** @var UiComponentInterface|PHPUnit\Framework\MockObject\MockObject $wrappedComponentMock */
111
136
$ wrappedComponentMock = $ this ->getMockForAbstractClass (
112
137
\Magento \Framework \View \Element \UiComponentInterface::class,
113
138
[],
114
139
'' ,
115
140
false
116
141
);
117
- /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */
118
- $ dataProviderMock = $ this ->getMockForAbstractClass (
119
- \Magento \Framework \View \Element \UiComponent \DataProvider \DataProviderInterface::class,
120
- [],
121
- '' ,
122
- false
123
- );
142
+
143
+ if ($ this ->dataProviderMock === null ) {
144
+ $ this ->dataProviderMock = $ this ->getMockForAbstractClass (
145
+ DataProviderInterface::class,
146
+ [],
147
+ '' ,
148
+ false
149
+ );
150
+
151
+ $ this ->dataProviderMock ->expects ($ this ->once ())
152
+ ->method ('addOrder ' )
153
+ ->with ('test_name ' , 'ASC ' );
154
+ }
155
+
156
+ $ processor = $ this ->getMockBuilder (Processor::class)
157
+ ->disableOriginalConstructor ()
158
+ ->getMock ();
124
159
160
+ $ this ->contextMock ->expects ($ this ->atLeastOnce ())
161
+ ->method ('getProcessor ' )
162
+ ->willReturn ($ processor );
125
163
$ this ->contextMock ->expects ($ this ->atLeastOnce ())
126
164
->method ('getNamespace ' )
127
165
->willReturn ('test_namespace ' );
128
166
$ this ->contextMock ->expects ($ this ->atLeastOnce ())
129
167
->method ('getDataProvider ' )
130
- ->willReturn ($ dataProviderMock );
168
+ ->willReturn ($ this -> dataProviderMock );
131
169
$ this ->contextMock ->expects ($ this ->atLeastOnce ())
132
170
->method ('getRequestParam ' )
133
171
->with ('sorting ' )
134
172
->willReturn (['field ' => 'test_name ' , 'direction ' => 'asc ' ]);
135
173
$ this ->contextMock ->expects ($ this ->atLeastOnce ())
136
174
->method ('addComponentDefinition ' )
137
- ->with (Column::NAME . '.test_type ' , ['extends ' => 'test_config_extends ' ]);
138
-
139
- $ dataProviderMock ->expects ($ this ->once ())
140
- ->method ('addOrder ' )
141
- ->with ('test_name ' , 'ASC ' );
175
+ ->with ($ this ->columnName . '.test_type ' , ['extends ' => 'test_config_extends ' ]);
142
176
143
- $ uiComponentFactoryMock ->expects ($ this ->once ())
177
+ $ this -> uiComponentFactoryMock ->expects ($ this ->once ())
144
178
->method ('create ' )
145
179
->with ('test_name ' , 'test_type ' , array_merge (['context ' => $ this ->contextMock ], $ data ))
146
180
->willReturn ($ wrappedComponentMock );
@@ -151,16 +185,71 @@ public function testPrepare()
151
185
$ wrappedComponentMock ->expects ($ this ->once ())
152
186
->method ('prepare ' );
153
187
154
- /** @var Column $column */
188
+ $ column ->prepare ();
189
+ }
190
+
191
+ /**
192
+ * Run a test on sorting function
193
+ *
194
+ * @param array $config
195
+ * @param string $direction
196
+ * @param int $numOfProviderCalls
197
+ * @throws \ReflectionException
198
+ *
199
+ * @dataProvider sortingDataProvider
200
+ */
201
+ public function testSorting (array $ config , string $ direction , int $ numOfProviderCalls )
202
+ {
203
+ $ data = [
204
+ 'name ' => 'test_name ' ,
205
+ 'config ' => $ config
206
+ ];
207
+
208
+ $ this ->dataProviderMock = $ this ->getMockForAbstractClass (
209
+ DataProviderInterface::class,
210
+ [],
211
+ '' ,
212
+ false
213
+ );
214
+
215
+ $ this ->dataProviderMock ->expects ($ this ->exactly ($ numOfProviderCalls ))
216
+ ->method ('addOrder ' )
217
+ ->with ('test_name ' , $ direction );
218
+
219
+ $ this ->contextMock ->expects ($ this ->atLeastOnce ())
220
+ ->method ('getRequestParam ' )
221
+ ->with ('sorting ' )
222
+ ->willReturn (['field ' => 'test_name ' , 'direction ' => $ direction ]);
223
+
224
+ $ this ->contextMock ->expects ($ this ->exactly ($ numOfProviderCalls ))
225
+ ->method ('getDataProvider ' )
226
+ ->willReturn ($ this ->dataProviderMock );
227
+
155
228
$ column = $ this ->objectManager ->getObject (
156
- \ Magento \ Ui \ Component \ Listing \ Columns \Column::class ,
229
+ $ this -> columnClass ,
157
230
[
158
231
'context ' => $ this ->contextMock ,
159
- 'uiComponentFactory ' => $ uiComponentFactoryMock ,
232
+ 'uiComponentFactory ' => $ this -> uiComponentFactoryMock ,
160
233
'data ' => $ data
161
234
]
162
235
);
163
236
164
- $ column ->prepare ();
237
+ // get access to the method
238
+ $ method = new \ReflectionMethod (
239
+ Column::class,
240
+ 'applySorting '
241
+ );
242
+ $ method ->setAccessible (true );
243
+
244
+ $ method ->invokeArgs ($ column , []);
245
+ }
246
+
247
+ public function sortingDataProvider ()
248
+ {
249
+ return [
250
+ [['dataType ' => 'test_type ' , 'sortable ' => true ], 'ASC ' , 1 ],
251
+ [['dataType ' => 'test_type ' , 'sortable ' => false ], 'ASC ' , 0 ],
252
+ [['dataType ' => 'test_type ' , 'sortable ' => true ], 'foobar ' , 0 ]
253
+ ];
165
254
}
166
255
}
0 commit comments