|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Reports\Test\Unit\Block\Adminhtml\Sales\Coupons; |
| 9 | + |
| 10 | +/** |
| 11 | + * Test for class \Magento\Reports\Block\Adminhtml\Sales\Coupons\Grid |
| 12 | + */ |
| 13 | +class GridTest extends \PHPUnit\Framework\TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var \Magento\Reports\Block\Adminhtml\Sales\Coupons\Grid |
| 17 | + */ |
| 18 | + private $model; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + private $storeManagerMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Reports\Model\ResourceModel\Report\Collection\Factory|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $resourceFactoryMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * Set up mock objects for tested class |
| 32 | + * |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class) |
| 38 | + ->getMock(); |
| 39 | + $this->resourceFactoryMock = $this |
| 40 | + ->getMockBuilder(\Magento\Reports\Model\ResourceModel\Report\Collection\Factory::class) |
| 41 | + ->disableOriginalConstructor() |
| 42 | + ->getMock(); |
| 43 | + $aggregatedColumns = [1 => 'SUM(value)']; |
| 44 | + |
| 45 | + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 46 | + $this->model = $objectManager->getObject( |
| 47 | + \Magento\Reports\Block\Adminhtml\Sales\Coupons\Grid::class, |
| 48 | + [ |
| 49 | + '_storeManager' => $this->storeManagerMock, |
| 50 | + '_aggregatedColumns' => $aggregatedColumns, |
| 51 | + 'resourceFactory' => $this->resourceFactoryMock, |
| 52 | + ] |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @dataProvider getCountTotalsDataProvider |
| 58 | + * |
| 59 | + * @param string $reportType |
| 60 | + * @param int $priceRuleType |
| 61 | + * @param int $collectionSize |
| 62 | + * @param bool $expectedCountTotals |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function testGetCountTotals( |
| 66 | + string $reportType, |
| 67 | + int $priceRuleType, |
| 68 | + int $collectionSize, |
| 69 | + bool $expectedCountTotals |
| 70 | + ): void { |
| 71 | + $filterData = new \Magento\Framework\DataObject(); |
| 72 | + $filterData->setData('report_type', $reportType); |
| 73 | + $filterData->setData('period_type', 'day'); |
| 74 | + $filterData->setData('from', '2000-01-01'); |
| 75 | + $filterData->setData('to', '2000-01-30'); |
| 76 | + $filterData->setData('store_ids', '1'); |
| 77 | + $filterData->setData('price_rule_type', $priceRuleType); |
| 78 | + if ($priceRuleType) { |
| 79 | + $filterData->setData('rules_list', ['0,1']); |
| 80 | + } |
| 81 | + $filterData->setData('order_statuses', 'statuses'); |
| 82 | + $this->model->setFilterData($filterData); |
| 83 | + |
| 84 | + $resourceCollectionName = $this->model->getResourceCollectionName(); |
| 85 | + $collectionMock = $this->buildBaseCollectionMock($filterData, $resourceCollectionName, $collectionSize); |
| 86 | + |
| 87 | + $store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class) |
| 88 | + ->getMock(); |
| 89 | + $this->storeManagerMock->method('getStores') |
| 90 | + ->willReturn([1 => $store]); |
| 91 | + $this->resourceFactoryMock->expects($this->once()) |
| 92 | + ->method('create') |
| 93 | + ->willReturn($collectionMock); |
| 94 | + |
| 95 | + $this->assertEquals($expectedCountTotals, $this->model->getCountTotals()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function getCountTotalsDataProvider(): array |
| 102 | + { |
| 103 | + return [ |
| 104 | + ['created_at_shipment', 0, 0, false], |
| 105 | + ['created_at_shipment', 0, 1, true], |
| 106 | + ['updated_at_order', 0, 1, true], |
| 107 | + ['updated_at_order', 1, 1, true], |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param \Magento\Framework\DataObject $filterData |
| 113 | + * @param string $resourceCollectionName |
| 114 | + * @param int $collectionSize |
| 115 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 116 | + */ |
| 117 | + private function buildBaseCollectionMock( |
| 118 | + \Magento\Framework\DataObject $filterData, |
| 119 | + string $resourceCollectionName, |
| 120 | + int $collectionSize |
| 121 | + ): \PHPUnit_Framework_MockObject_MockObject { |
| 122 | + $collectionMock = $this->getMockBuilder($resourceCollectionName) |
| 123 | + ->disableOriginalConstructor() |
| 124 | + ->getMock(); |
| 125 | + $collectionMock->expects($this->once()) |
| 126 | + ->method('setPeriod') |
| 127 | + ->with($filterData->getData('period_type')) |
| 128 | + ->willReturnSelf(); |
| 129 | + $collectionMock->expects($this->once()) |
| 130 | + ->method('setDateRange') |
| 131 | + ->with($filterData->getData('from'), $filterData->getData('to')) |
| 132 | + ->willReturnSelf(); |
| 133 | + $collectionMock->expects($this->once()) |
| 134 | + ->method('addStoreFilter') |
| 135 | + ->with(\explode(',', $filterData->getData('store_ids'))) |
| 136 | + ->willReturnSelf(); |
| 137 | + $collectionMock->expects($this->once()) |
| 138 | + ->method('setAggregatedColumns') |
| 139 | + ->willReturnSelf(); |
| 140 | + $collectionMock->expects($this->once()) |
| 141 | + ->method('isTotals') |
| 142 | + ->with(true) |
| 143 | + ->willReturnSelf(); |
| 144 | + $collectionMock->expects($this->once()) |
| 145 | + ->method('addOrderStatusFilter') |
| 146 | + ->with($filterData->getData('order_statuses')) |
| 147 | + ->willReturnSelf(); |
| 148 | + |
| 149 | + if ($filterData->getData('price_rule_type')) { |
| 150 | + $collectionMock->expects($this->once()) |
| 151 | + ->method('addRuleFilter') |
| 152 | + ->with(\explode(',', $filterData->getData('rules_list')[0])) |
| 153 | + ->willReturnSelf(); |
| 154 | + } |
| 155 | + |
| 156 | + $collectionMock->expects($this->once()) |
| 157 | + ->method('load') |
| 158 | + ->willReturnSelf(); |
| 159 | + $collectionMock->expects($this->once()) |
| 160 | + ->method('getSize') |
| 161 | + ->willReturn($collectionSize); |
| 162 | + if ($collectionSize) { |
| 163 | + $itemMock = $this->getMockBuilder(\Magento\Reports\Model\Item::class) |
| 164 | + ->disableOriginalConstructor() |
| 165 | + ->getMock(); |
| 166 | + $collectionMock->expects($this->once()) |
| 167 | + ->method('getItems') |
| 168 | + ->willReturn([$itemMock]); |
| 169 | + } |
| 170 | + |
| 171 | + return $collectionMock; |
| 172 | + } |
| 173 | +} |
0 commit comments