2121use Magento \Sales \Model \ResourceModel \Report \Bestsellers ;
2222use Magento \Store \Api \Data \StoreInterface ;
2323use Magento \Store \Model \StoreManagerInterface ;
24+ use PHPUnit \Framework \Attributes \DataProvider ;
25+ use PHPUnit \Framework \MockObject \Exception ;
2426use PHPUnit \Framework \MockObject \MockObject ;
2527use PHPUnit \Framework \TestCase ;
2628use Psr \Log \LoggerInterface ;
@@ -95,6 +97,24 @@ class BestsellersTest extends TestCase
9597 */
9698 protected string $ connectionName = 'connection_name ' ;
9799
100+ /**
101+ * Data provider for testAggregateWithMultipleOrderDatesAndNoDates
102+ *
103+ * @return array
104+ */
105+ public static function datesDataProvider (): array
106+ {
107+ $ randomDates = [];
108+ for ($ i = 0 ; $ i < 10000 ; $ i ++) {
109+ $ randomDates [] = date ('Y-m-d ' , rand (0 , time ()));
110+ }
111+ return [
112+ 'from-to interval ' => [new \DateTime ('yesterday ' ), new \DateTime (), $ randomDates ],
113+ 'from interval ' => [new \DateTime ('yesterday ' ), null , $ randomDates ],
114+ 'from interval no dates ' => [new \DateTime ('yesterday ' ), null , []]
115+ ];
116+ }
117+
98118 /**
99119 * @inheritDoc
100120 */
@@ -307,16 +327,18 @@ public function testAggregatePerStoreCalculationNoInterval(): void
307327 }
308328
309329 /**
330+ * @param \DateTime|null $from
331+ * @param \DateTime|null $to
332+ * @param array $randomDates
310333 * @return void
311- * @throws \ Exception
334+ * @throws Exception
312335 */
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- }
336+ #[DataProvider('datesDataProvider ' )]
337+ public function testAggregateWithMultipleOrderDatesAndNoDates (
338+ ?\DateTime $ from ,
339+ ?\DateTime $ to ,
340+ array $ randomDates
341+ ): void {
320342 $ periodExpr = 'DATE(DATE_ADD(`source_table`.`created_at`, INTERVAL -25200 SECOND)) ' ;
321343 $ select = $ this ->getMockBuilder (Select::class)
322344 ->disableOriginalConstructor ()
@@ -336,7 +358,11 @@ public function testAggregateWithMultipleOrderDates(): void
336358 $ connection ->expects ($ this ->any ())->method ('select ' )->willReturn ($ select );
337359 $ query = $ this ->createMock (\Zend_Db_Statement_Interface::class);
338360 $ query ->expects ($ this ->once ())->method ('fetchAll ' )->willReturn ($ randomDates );
339- $ connection ->expects ($ this ->exactly (4 ))->method ('query ' )->willReturn ($ query );
361+ $ calls = 3 ;
362+ if ($ from && $ to ) {
363+ $ calls = 4 ;
364+ }
365+ $ connection ->expects ($ this ->exactly ($ calls ))->method ('query ' )->willReturn ($ query );
340366 $ resource = $ this ->createMock (ResourceConnection::class);
341367 $ resource ->expects ($ this ->any ())
342368 ->method ('getConnection ' )
0 commit comments