21
21
use Magento \Sales \Model \ResourceModel \Report \Bestsellers ;
22
22
use Magento \Store \Api \Data \StoreInterface ;
23
23
use Magento \Store \Model \StoreManagerInterface ;
24
+ use PHPUnit \Framework \Attributes \DataProvider ;
25
+ use PHPUnit \Framework \MockObject \Exception ;
24
26
use PHPUnit \Framework \MockObject \MockObject ;
25
27
use PHPUnit \Framework \TestCase ;
26
28
use Psr \Log \LoggerInterface ;
@@ -95,6 +97,24 @@ class BestsellersTest extends TestCase
95
97
*/
96
98
protected string $ connectionName = 'connection_name ' ;
97
99
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
+
98
118
/**
99
119
* @inheritDoc
100
120
*/
@@ -307,16 +327,18 @@ public function testAggregatePerStoreCalculationNoInterval(): void
307
327
}
308
328
309
329
/**
330
+ * @param \DateTime|null $from
331
+ * @param \DateTime|null $to
332
+ * @param array $randomDates
310
333
* @return void
311
- * @throws \ Exception
334
+ * @throws Exception
312
335
*/
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 {
320
342
$ periodExpr = 'DATE(DATE_ADD(`source_table`.`created_at`, INTERVAL -25200 SECOND)) ' ;
321
343
$ select = $ this ->getMockBuilder (Select::class)
322
344
->disableOriginalConstructor ()
@@ -336,7 +358,11 @@ public function testAggregateWithMultipleOrderDates(): void
336
358
$ connection ->expects ($ this ->any ())->method ('select ' )->willReturn ($ select );
337
359
$ query = $ this ->createMock (\Zend_Db_Statement_Interface::class);
338
360
$ 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 );
340
366
$ resource = $ this ->createMock (ResourceConnection::class);
341
367
$ resource ->expects ($ this ->any ())
342
368
->method ('getConnection ' )
0 commit comments