1717use Magento \Framework \View \Element \UiComponent \DataProvider \DataProviderInterface ;
1818use Magento \Framework \View \Element \UiComponent \ContextInterface ;
1919
20+ /**
21+ * Class FilterTest
22+ *
23+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24+ */
2025class FilterTest extends \PHPUnit_Framework_TestCase
2126{
2227 /**
@@ -62,6 +67,11 @@ class FilterTest extends \PHPUnit_Framework_TestCase
6267 */
6368 private $ uiComponentMock ;
6469
70+ /**
71+ * \PHPUnit_Framework_MockObject_MockObject
72+ */
73+ private $ contextMock ;
74+
6575 /**
6676 * Set up
6777 */
@@ -74,7 +84,7 @@ protected function setUp()
7484 $ this ->dataProviderMock = $ this ->getMock (DataProviderInterface::class);
7585 $ this ->uiComponentMock = $ this ->getMock (UiComponentInterface::class);
7686 $ this ->abstractDbMock = $ this ->getMock (AbstractDb::class, [], [], '' , false );
77- $ contextMock = $ this ->getMock (ContextInterface::class);
87+ $ this -> contextMock = $ this ->getMock (ContextInterface::class);
7888 $ this ->searchResultMock = $ this ->getMock (SearchResultInterface::class);
7989 $ uiComponentMockTwo = $ this ->getMock (UiComponentInterface::class);
8090 $ this ->filter = $ this ->objectManager ->getObject (
@@ -96,18 +106,7 @@ protected function setUp()
96106 ->willReturn ([]);
97107 $ this ->uiComponentMock ->expects ($ this ->any ())
98108 ->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 );
111110 }
112111
113112 /**
@@ -144,6 +143,18 @@ public function applySelectionOnTargetProviderDataProvider()
144143 */
145144 public function testApplySelectionOnTargetProviderException ()
146145 {
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+
147158 $ filterMock = $ this ->getMock (ApiFilter::class, [], [], '' , false );
148159 $ this ->filterBuilderMock ->expects ($ this ->any ())
149160 ->method ('setConditionType ' )
@@ -170,7 +181,7 @@ public function testApplySelectionOnTargetProviderException()
170181 }
171182
172183 /**
173- * Run test for getCollection method
184+ * Run test for getCollection method with SearchResultInterface
174185 *
175186 * @param int[]|bool $selectedIds
176187 * @param int[]|bool $excludedIds
@@ -196,6 +207,44 @@ public function testGetCollection($selectedIds, $excludedIds, $filterExpected, $
196207 $ this ->assertEquals ($ this ->abstractDbMock , $ this ->filter ->getCollection ($ this ->abstractDbMock ));
197208 }
198209
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+
199248 /**
200249 * This tests the method prepareComponent()
201250 */
@@ -221,6 +270,9 @@ public function testGetComponent()
221270 */
222271 public function testGetComponentRefererUrlIsNotNull ()
223272 {
273+ $ this ->contextMock ->expects ($ this ->any ())
274+ ->method ('getDataProvider ' )
275+ ->willReturn ($ this ->dataProviderMock );
224276 $ returnArray = [
225277 'referer_url ' => 'referer_url '
226278 ];
@@ -235,6 +287,9 @@ public function testGetComponentRefererUrlIsNotNull()
235287 */
236288 public function testGetComponentRefererUrlIsNull ()
237289 {
290+ $ this ->contextMock ->expects ($ this ->any ())
291+ ->method ('getDataProvider ' )
292+ ->willReturn ($ this ->dataProviderMock );
238293 $ this ->assertNull ($ this ->filter ->getComponentRefererUrl ());
239294 }
240295
@@ -248,6 +303,17 @@ public function testGetComponentRefererUrlIsNull()
248303 */
249304 private function setUpApplySelection ($ selectedIds , $ excludedIds , $ filterExpected , $ conditionExpected )
250305 {
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 ])]);
251317 $ filterMock = $ this ->getMock (ApiFilter::class, [], [], '' , false );
252318 $ this ->requestMock ->expects ($ this ->at (0 ))
253319 ->method ('getParam ' )
@@ -260,6 +326,7 @@ private function setUpApplySelection($selectedIds, $excludedIds, $filterExpected
260326 $ this ->dataProviderMock ->expects ($ this ->exactly ($ filterExpected ))
261327 ->method ('addFilter ' )
262328 ->with ($ filterMock );
329+
263330 $ this ->filterBuilderMock ->expects ($ this ->exactly ($ filterExpected ))
264331 ->method ('setConditionType ' )
265332 ->with ($ conditionExpected )
0 commit comments