@@ -139,7 +139,8 @@ public function testAggregatePerStoreCalculationWithInterval(): void
139139 ->willReturn ($ periodExpr );
140140 $ connection ->expects ($ this ->any ())->method ('select ' )->willReturn ($ select );
141141 $ query = $ this ->createMock (\Zend_Db_Statement_Interface::class);
142- $ connection ->expects ($ this ->exactly (5 ))->method ('query ' )->willReturn ($ query );
142+ $ query ->expects ($ this ->once ())->method ('fetchAll ' )->willReturn (['date1 ' , 'date2 ' ]);
143+ $ connection ->expects ($ this ->exactly (4 ))->method ('query ' )->willReturn ($ query );
143144 $ resource = $ this ->createMock (ResourceConnection::class);
144145 $ resource ->expects ($ this ->any ())
145146 ->method ('getConnection ' )
@@ -219,7 +220,7 @@ public function testClearByDateRange()
219220 $ this ->flagFactory ->expects ($ this ->atLeastOnce ())->method ('create ' )->willReturn ($ flag );
220221
221222 $ query = $ this ->createMock (\Zend_Db_Statement_Interface::class);
222- $ query ->method ('fetchColumn ' )->willReturnOnConsecutiveCalls ( 'date1 ' , 'date2 ' , false );
223+ $ query ->method ('fetchAll ' )->willReturn ([ 'date1 ' , 'date2 ' ] );
223224 $ connection ->expects ($ this ->atLeastOnce ())->method ('query ' )->willReturn ($ query );
224225 $ connection ->expects ($ this ->atLeastOnce ())->method ('getDatePartSql ' )->willReturn ($ periodExpr );
225226
@@ -304,4 +305,75 @@ public function testAggregatePerStoreCalculationNoInterval(): void
304305
305306 $ this ->report ->aggregate ();
306307 }
308+
309+ /**
310+ * @return void
311+ * @throws \Exception
312+ */
313+ public function testAggregateWithMultipleOrderDates (): void
314+ {
315+ $ from = new \DateTime ('yesterday ' );
316+ $ to = null ;
317+ for ($ i = 0 ; $ i < 10000 ; $ i ++) {
318+ $ randomDates [] = date ('Y-m-d ' , rand (0 , time ()));
319+ }
320+ $ periodExpr = 'DATE(DATE_ADD(`source_table`.`created_at`, INTERVAL -25200 SECOND)) ' ;
321+ $ select = $ this ->getMockBuilder (Select::class)
322+ ->disableOriginalConstructor ()
323+ ->getMock ();
324+ $ select ->expects ($ this ->exactly (2 ))->method ('group ' );
325+ $ select ->expects ($ this ->exactly (5 ))->method ('from ' )->willReturn ($ select );
326+ $ select ->expects ($ this ->exactly (3 ))->method ('distinct ' )->willReturn ($ select );
327+ $ select ->expects ($ this ->once ())->method ('joinInner ' )->willReturn ($ select );
328+ $ select ->expects ($ this ->once ())->method ('joinLeft ' )->willReturn ($ select );
329+ $ select ->expects ($ this ->any ())->method ('where ' )->willReturn ($ select );
330+ $ select ->expects ($ this ->once ())->method ('useStraightJoin ' );
331+ $ select ->expects ($ this ->exactly (2 ))->method ('insertFromSelect ' );
332+ $ connection = $ this ->createMock (AdapterInterface::class);
333+ $ connection ->expects ($ this ->exactly (4 ))
334+ ->method ('getDatePartSql ' )
335+ ->willReturn ($ periodExpr );
336+ $ connection ->expects ($ this ->any ())->method ('select ' )->willReturn ($ select );
337+ $ query = $ this ->createMock (\Zend_Db_Statement_Interface::class);
338+ $ query ->expects ($ this ->once ())->method ('fetchAll ' )->willReturn ($ randomDates );
339+ $ connection ->expects ($ this ->exactly (4 ))->method ('query ' )->willReturn ($ query );
340+ $ resource = $ this ->createMock (ResourceConnection::class);
341+ $ resource ->expects ($ this ->any ())
342+ ->method ('getConnection ' )
343+ ->with ($ this ->connectionName )
344+ ->willReturn ($ connection );
345+ $ this ->context ->expects ($ this ->any ())->method ('getResources ' )->willReturn ($ resource );
346+
347+ $ store = $ this ->createMock (StoreInterface::class);
348+ $ store ->expects ($ this ->once ())->method ('getId ' )->willReturn (1 );
349+ $ this ->storeManager ->expects ($ this ->once ())->method ('getStores ' )->with (true )->willReturn ([$ store ]);
350+
351+ $ this ->helper ->expects ($ this ->exactly (3 ))->method ('getBestsellersReportUpdateRatingPos ' );
352+
353+ $ flag = $ this ->createMock (Flag::class);
354+ $ flag ->expects ($ this ->once ())->method ('setReportFlagCode ' )->willReturn ($ flag );
355+ $ flag ->expects ($ this ->once ())->method ('unsetData ' )->willReturn ($ flag );
356+ $ flag ->expects ($ this ->once ())->method ('loadSelf ' );
357+ $ this ->flagFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ flag );
358+
359+ $ date = $ this ->createMock (\DateTime::class);
360+ $ date ->expects ($ this ->exactly (4 ))->method ('format ' )->with ('e ' );
361+ $ this ->time ->expects ($ this ->exactly (4 ))->method ('scopeDate ' )->willReturn ($ date );
362+
363+ $ this ->report = new Bestsellers (
364+ $ this ->context ,
365+ $ this ->logger ,
366+ $ this ->time ,
367+ $ this ->flagFactory ,
368+ $ this ->validator ,
369+ $ this ->date ,
370+ $ this ->product ,
371+ $ this ->helper ,
372+ $ this ->connectionName ,
373+ [],
374+ $ this ->storeManager
375+ );
376+
377+ $ this ->report ->aggregate ($ from , $ to );
378+ }
307379}
0 commit comments