Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectionTest.php
Go to the documentation of this file.
1 <?php
7 
8 class CollectionTest extends \PHPUnit\Framework\TestCase
9 {
13  private $_collection;
14 
15  protected function setUp()
16  {
18  \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection::class
19  );
20  $this->_collection->setPeriod('day')->setDateRange(null, null)->addStoreFilter([1]);
21  }
22 
27  public function testGetItems()
28  {
29  $expectedResult = [1 => 2];
30  $actualResult = [];
32  foreach ($this->_collection->getItems() as $reportItem) {
33  $actualResult[$reportItem->getData('product_id')] = $reportItem->getData('qty_ordered');
34  }
35  $this->assertEquals($expectedResult, $actualResult);
36  }
37 
46  public function testTableSelection($period, $expectedTable, $dateFrom, $dateTo)
47  {
48  $dbTableName = $this->_collection->getTable($expectedTable);
49  $this->_collection->setPeriod($period);
50  $this->_collection->setDateRange($dateFrom, $dateTo);
51  $this->_collection->load();
52  $from = $this->_collection->getSelect()->getPart('from');
53 
54  $this->assertArrayHasKey($dbTableName, $from);
55 
56  $this->assertArrayHasKey('tableName', $from[$dbTableName]);
57  $actualTable = $from[$dbTableName]['tableName'];
58 
59  $this->assertEquals($dbTableName, $actualTable);
60  }
61 
67  public function tableForPeriodDataProvider()
68  {
69  $dateNow = date('Y-m-d', time());
70  $dateYearAgo = date('Y-m-d', strtotime($dateNow . ' -1 year'));
71  return [
72  [
73  'period' => 'year',
74  'table' => 'sales_bestsellers_aggregated_yearly',
75  'date_from' => null,
76  'date_to' => null,
77  ],
78  [
79  'period' => 'month',
80  'table' => 'sales_bestsellers_aggregated_monthly',
81  'date_from' => null,
82  'date_to' => null
83  ],
84  [
85  'period' => 'day',
86  'table' => 'sales_bestsellers_aggregated_daily',
87  'date_from' => null,
88  'date_to' => null
89  ],
90  [
91  'period' => 'undefinedPeriod',
92  'table' => 'sales_bestsellers_aggregated_daily',
93  'date_from' => null,
94  'date_to' => null
95  ],
96  [
97  'period' => null,
98  'table' => 'sales_bestsellers_aggregated_daily',
99  'date_from' => $dateYearAgo,
100  'date_to' => $dateNow
101  ],
102  [
103  'period' => null,
104  'table' => 'sales_bestsellers_aggregated_daily',
105  'date_from' => $dateNow,
106  'date_to' => $dateNow
107  ],
108  [
109  'period' => null,
110  'table' => 'sales_bestsellers_aggregated_daily',
111  'date_from' => $dateYearAgo,
112  'date_to' => $dateYearAgo
113  ],
114  [
115  'period' => null,
116  'table' => 'sales_bestsellers_aggregated_yearly',
117  'date_from' => null,
118  'date_to' => null
119  ],
120  ];
121  }
122 }
$this _collection
Definition: coupons.php:7
testTableSelection($period, $expectedTable, $dateFrom, $dateTo)