17
17
use Magento \Framework \View \Element \UiComponent \DataProvider \DataProviderInterface ;
18
18
use Magento \Framework \View \Element \UiComponent \ContextInterface ;
19
19
20
+ /**
21
+ * Class FilterTest
22
+ *
23
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
+ */
20
25
class FilterTest extends \PHPUnit_Framework_TestCase
21
26
{
22
27
/**
@@ -62,6 +67,11 @@ class FilterTest extends \PHPUnit_Framework_TestCase
62
67
*/
63
68
private $ uiComponentMock ;
64
69
70
+ /**
71
+ * \PHPUnit_Framework_MockObject_MockObject
72
+ */
73
+ private $ contextMock ;
74
+
65
75
/**
66
76
* Set up
67
77
*/
@@ -74,7 +84,7 @@ protected function setUp()
74
84
$ this ->dataProviderMock = $ this ->getMock (DataProviderInterface::class);
75
85
$ this ->uiComponentMock = $ this ->getMock (UiComponentInterface::class);
76
86
$ this ->abstractDbMock = $ this ->getMock (AbstractDb::class, [], [], '' , false );
77
- $ contextMock = $ this ->getMock (ContextInterface::class);
87
+ $ this -> contextMock = $ this ->getMock (ContextInterface::class);
78
88
$ this ->searchResultMock = $ this ->getMock (SearchResultInterface::class);
79
89
$ uiComponentMockTwo = $ this ->getMock (UiComponentInterface::class);
80
90
$ this ->filter = $ this ->objectManager ->getObject (
@@ -96,18 +106,7 @@ protected function setUp()
96
106
->willReturn ([]);
97
107
$ this ->uiComponentMock ->expects ($ this ->any ())
98
108
->method ('getContext ' )
99
- ->willReturn ($ contextMock );
100
- $ contextMock ->expects ($ this ->any ())
101
- ->method ('getDataProvider ' )
102
- ->willReturn ($ this ->dataProviderMock );
103
- $ this ->dataProviderMock ->expects ($ this ->any ())
104
- ->method ('setLimit ' );
105
- $ this ->dataProviderMock ->expects ($ this ->any ())
106
- ->method ('searchResultMock ' )
107
- ->willReturn ($ this ->searchResultMock );
108
- $ this ->searchResultMock ->expects ($ this ->any ())
109
- ->method ('getAllIds ' )
110
- ->willReturn ([]);
109
+ ->willReturn ($ this ->contextMock );
111
110
}
112
111
113
112
/**
@@ -144,6 +143,18 @@ public function applySelectionOnTargetProviderDataProvider()
144
143
*/
145
144
public function testApplySelectionOnTargetProviderException ()
146
145
{
146
+ $ this ->contextMock ->expects ($ this ->any ())
147
+ ->method ('getDataProvider ' )
148
+ ->willReturn ($ this ->dataProviderMock );
149
+ $ this ->dataProviderMock ->expects ($ this ->any ())
150
+ ->method ('setLimit ' );
151
+ $ this ->dataProviderMock ->expects ($ this ->any ())
152
+ ->method ('getSearchResult ' )
153
+ ->willReturn ($ this ->searchResultMock );
154
+ $ this ->searchResultMock ->expects ($ this ->any ())
155
+ ->method ('getItems ' )
156
+ ->willReturn ([]);
157
+
147
158
$ filterMock = $ this ->getMock (ApiFilter::class, [], [], '' , false );
148
159
$ this ->filterBuilderMock ->expects ($ this ->any ())
149
160
->method ('setConditionType ' )
@@ -170,7 +181,7 @@ public function testApplySelectionOnTargetProviderException()
170
181
}
171
182
172
183
/**
173
- * Run test for getCollection method
184
+ * Run test for getCollection method with SearchResultInterface
174
185
*
175
186
* @param int[]|bool $selectedIds
176
187
* @param int[]|bool $excludedIds
@@ -196,6 +207,44 @@ public function testGetCollection($selectedIds, $excludedIds, $filterExpected, $
196
207
$ this ->assertEquals ($ this ->abstractDbMock , $ this ->filter ->getCollection ($ this ->abstractDbMock ));
197
208
}
198
209
210
+ /**
211
+ * Run test for getCollection method with collection
212
+ *
213
+ * @param int[]|bool $selectedIds
214
+ * @param int[]|bool $excludedIds
215
+ * @param int $filterExpected
216
+ * @param string $conditionExpected
217
+ * @dataProvider applySelectionOnTargetProviderDataProvider
218
+ */
219
+ public function testGetCollectionWithCollection ($ selectedIds , $ excludedIds , $ filterExpected , $ conditionExpected )
220
+ {
221
+ $ this ->dataProviderMock = $ this ->getMock (
222
+ \Magento \Ui \DataProvider \AbstractDataProvider::class,
223
+ [],
224
+ [],
225
+ '' ,
226
+ false
227
+ );
228
+ $ this ->contextMock ->expects ($ this ->any ())
229
+ ->method ('getDataProvider ' )
230
+ ->willReturn ($ this ->dataProviderMock );
231
+ $ this ->dataProviderMock ->expects ($ this ->any ())
232
+ ->method ('getAllIds ' )
233
+ ->willReturn ([1 , 2 , 3 ]);
234
+
235
+ $ this ->setUpApplySelection ($ selectedIds , $ excludedIds , $ filterExpected , $ conditionExpected );
236
+
237
+ $ this ->requestMock ->expects ($ this ->any ())
238
+ ->method ('getParam ' )
239
+ ->willReturnMap ([
240
+ ['namespace ' , null , '' ],
241
+ [Filter::SELECTED_PARAM , null , $ selectedIds ],
242
+ [Filter::EXCLUDED_PARAM , null , $ excludedIds ],
243
+ ]);
244
+
245
+ $ this ->assertEquals ($ this ->abstractDbMock , $ this ->filter ->getCollection ($ this ->abstractDbMock ));
246
+ }
247
+
199
248
/**
200
249
* This tests the method prepareComponent()
201
250
*/
@@ -221,6 +270,9 @@ public function testGetComponent()
221
270
*/
222
271
public function testGetComponentRefererUrlIsNotNull ()
223
272
{
273
+ $ this ->contextMock ->expects ($ this ->any ())
274
+ ->method ('getDataProvider ' )
275
+ ->willReturn ($ this ->dataProviderMock );
224
276
$ returnArray = [
225
277
'referer_url ' => 'referer_url '
226
278
];
@@ -235,6 +287,9 @@ public function testGetComponentRefererUrlIsNotNull()
235
287
*/
236
288
public function testGetComponentRefererUrlIsNull ()
237
289
{
290
+ $ this ->contextMock ->expects ($ this ->any ())
291
+ ->method ('getDataProvider ' )
292
+ ->willReturn ($ this ->dataProviderMock );
238
293
$ this ->assertNull ($ this ->filter ->getComponentRefererUrl ());
239
294
}
240
295
@@ -248,6 +303,17 @@ public function testGetComponentRefererUrlIsNull()
248
303
*/
249
304
private function setUpApplySelection ($ selectedIds , $ excludedIds , $ filterExpected , $ conditionExpected )
250
305
{
306
+ $ this ->contextMock ->expects ($ this ->any ())
307
+ ->method ('getDataProvider ' )
308
+ ->willReturn ($ this ->dataProviderMock );
309
+ $ this ->dataProviderMock ->expects ($ this ->any ())
310
+ ->method ('setLimit ' );
311
+ $ this ->dataProviderMock ->expects ($ this ->any ())
312
+ ->method ('getSearchResult ' )
313
+ ->willReturn ($ this ->searchResultMock );
314
+ $ this ->searchResultMock ->expects ($ this ->any ())
315
+ ->method ('getItems ' )
316
+ ->willReturn ([new \Magento \Framework \DataObject (['id ' => 1 ])]);
251
317
$ filterMock = $ this ->getMock (ApiFilter::class, [], [], '' , false );
252
318
$ this ->requestMock ->expects ($ this ->at (0 ))
253
319
->method ('getParam ' )
@@ -260,6 +326,7 @@ private function setUpApplySelection($selectedIds, $excludedIds, $filterExpected
260
326
$ this ->dataProviderMock ->expects ($ this ->exactly ($ filterExpected ))
261
327
->method ('addFilter ' )
262
328
->with ($ filterMock );
329
+
263
330
$ this ->filterBuilderMock ->expects ($ this ->exactly ($ filterExpected ))
264
331
->method ('setConditionType ' )
265
332
->with ($ conditionExpected )
0 commit comments