|
| 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\Backend\Test\Unit\Model\Dashboard\Chart; |
| 9 | + |
| 10 | +use Magento\Backend\Model\Dashboard\Chart\Date; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; |
| 13 | +use Magento\Framework\Data\Collection\EntityFactory; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 15 | +use Magento\Framework\DB\Adapter\Pdo\Mysql; |
| 16 | +use Magento\Framework\DB\Helper; |
| 17 | +use Magento\Framework\DB\Select; |
| 18 | +use Magento\Framework\Event\ManagerInterface; |
| 19 | +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; |
| 20 | +use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot; |
| 21 | +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 22 | +use Magento\Reports\Model\ResourceModel\Order\CollectionFactory; |
| 23 | +use Magento\Backend\Model\Dashboard\Period; |
| 24 | +use Magento\Reports\Model\ResourceModel\Order\Collection; |
| 25 | +use Magento\Sales\Model\Order\Config; |
| 26 | +use Magento\Sales\Model\ResourceModel\Report\OrderFactory; |
| 27 | +use Magento\Store\Model\ScopeInterface; |
| 28 | +use Magento\Store\Model\StoreManagerInterface; |
| 29 | +use PHPUnit\Framework\MockObject\MockObject; |
| 30 | +use PHPUnit\Framework\TestCase; |
| 31 | +use Psr\Log\LoggerInterface; |
| 32 | + |
| 33 | +/** |
| 34 | + * @SuppressWarnings(PHPMD.TooManyFields) |
| 35 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 36 | + */ |
| 37 | +class DateTest extends TestCase |
| 38 | +{ |
| 39 | + /** |
| 40 | + * @var Date |
| 41 | + */ |
| 42 | + private $model; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Collection |
| 46 | + */ |
| 47 | + private $collection; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var EntityFactory|MockObject |
| 51 | + */ |
| 52 | + private $entityFactoryMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var LoggerInterface|MockObject |
| 56 | + */ |
| 57 | + private $loggerMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var FetchStrategyInterface|MockObject |
| 61 | + */ |
| 62 | + private $fetchStrategyMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var ManagerInterface|MockObject |
| 66 | + */ |
| 67 | + private $managerMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var \Magento\Sales\Model\ResourceModel\EntitySnapshot|MockObject |
| 71 | + */ |
| 72 | + private $entitySnapshotMock; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var Helper|MockObject |
| 76 | + */ |
| 77 | + private $helperMock; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var ScopeConfigInterface|MockObject |
| 81 | + */ |
| 82 | + private $scopeConfigMock; |
| 83 | + |
| 84 | + /** |
| 85 | + * @var StoreManagerInterface|MockObject |
| 86 | + */ |
| 87 | + private $storeManagerMock; |
| 88 | + |
| 89 | + /** |
| 90 | + * @var TimezoneInterface|MockObject |
| 91 | + */ |
| 92 | + private $timezoneMock; |
| 93 | + |
| 94 | + /** |
| 95 | + * @var Config|MockObject |
| 96 | + */ |
| 97 | + private $configMock; |
| 98 | + |
| 99 | + /** |
| 100 | + * @var OrderFactory|MockObject |
| 101 | + */ |
| 102 | + private $orderFactoryMock; |
| 103 | + |
| 104 | + /** |
| 105 | + * @var AdapterInterface|MockObject |
| 106 | + */ |
| 107 | + private $connectionMock; |
| 108 | + |
| 109 | + /** |
| 110 | + * @var Select|MockObject |
| 111 | + */ |
| 112 | + private $selectMock; |
| 113 | + |
| 114 | + /** |
| 115 | + * @var AbstractDb|MockObject |
| 116 | + */ |
| 117 | + private $resourceMock; |
| 118 | + |
| 119 | + /** |
| 120 | + * @var CollectionFactory |
| 121 | + */ |
| 122 | + private $collectionFactoryMock; |
| 123 | + |
| 124 | + /** |
| 125 | + * @inheritDoc |
| 126 | + */ |
| 127 | + protected function setUp(): void |
| 128 | + { |
| 129 | + $this->collection = $this->getCollectionObject(); |
| 130 | + $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class) |
| 131 | + ->disableOriginalConstructor() |
| 132 | + ->getMock(); |
| 133 | + $this->collectionFactoryMock |
| 134 | + ->expects($this->any()) |
| 135 | + ->method('create') |
| 136 | + ->willReturn($this->collection); |
| 137 | + $this->model = new Date($this->collectionFactoryMock, $this->timezoneMock); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param string $period |
| 142 | + * @param string $config |
| 143 | + * @param int $expectedYear |
| 144 | + * |
| 145 | + * @return void |
| 146 | + * @dataProvider getByPeriodDataProvider |
| 147 | + */ |
| 148 | + public function testGetByPeriod($period, $config, $expectedYear): void |
| 149 | + { |
| 150 | + $this->scopeConfigMock |
| 151 | + ->expects($this->once()) |
| 152 | + ->method('getValue') |
| 153 | + ->with( |
| 154 | + $config, |
| 155 | + ScopeInterface::SCOPE_STORE |
| 156 | + ) |
| 157 | + ->willReturn(1); |
| 158 | + $dates = $this->model->getByPeriod($period); |
| 159 | + $this->assertEquals($expectedYear, substr($dates[0], 0, 4)); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @return array |
| 164 | + */ |
| 165 | + public function getByPeriodDataProvider(): array |
| 166 | + { |
| 167 | + $dateStart = new \DateTime(); |
| 168 | + $expectedYear = $dateStart->format('Y'); |
| 169 | + $expected2YTDYear = $expectedYear - 1; |
| 170 | + |
| 171 | + return [ |
| 172 | + [Period::PERIOD_1_YEAR, 'reports/dashboard/ytd_start', $expectedYear], |
| 173 | + [Period::PERIOD_2_YEARS, 'reports/dashboard/ytd_start', $expected2YTDYear] |
| 174 | + ]; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @return Collection |
| 179 | + */ |
| 180 | + private function getCollectionObject() |
| 181 | + { |
| 182 | + $this->entityFactoryMock = $this->getMockBuilder(EntityFactory::class) |
| 183 | + ->disableOriginalConstructor() |
| 184 | + ->getMock(); |
| 185 | + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) |
| 186 | + ->getMock(); |
| 187 | + $this->fetchStrategyMock = $this->getMockBuilder( |
| 188 | + FetchStrategyInterface::class |
| 189 | + )->getMock(); |
| 190 | + $this->managerMock = $this->getMockBuilder(ManagerInterface::class) |
| 191 | + ->getMock(); |
| 192 | + $snapshotClassName = Snapshot::class; |
| 193 | + $this->entitySnapshotMock = $this->getMockBuilder($snapshotClassName) |
| 194 | + ->disableOriginalConstructor() |
| 195 | + ->getMock(); |
| 196 | + $this->helperMock = $this->getMockBuilder(Helper::class) |
| 197 | + ->disableOriginalConstructor() |
| 198 | + ->getMock(); |
| 199 | + $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) |
| 200 | + ->getMock(); |
| 201 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 202 | + ->getMock(); |
| 203 | + $this->timezoneMock = $this->getMockBuilder(TimezoneInterface::class) |
| 204 | + ->getMock(); |
| 205 | + $this->timezoneMock |
| 206 | + ->expects($this->any()) |
| 207 | + ->method('getConfigTimezone') |
| 208 | + ->willReturn('America/Chicago'); |
| 209 | + $this->configMock = $this->getMockBuilder(Config::class) |
| 210 | + ->disableOriginalConstructor() |
| 211 | + ->getMock(); |
| 212 | + $this->orderFactoryMock = $this->getMockBuilder(OrderFactory::class) |
| 213 | + ->onlyMethods(['create']) |
| 214 | + ->disableOriginalConstructor() |
| 215 | + ->getMock(); |
| 216 | + $this->selectMock = $this->getMockBuilder(Select::class) |
| 217 | + ->disableOriginalConstructor() |
| 218 | + ->getMock(); |
| 219 | + $this->selectMock |
| 220 | + ->expects($this->any()) |
| 221 | + ->method('columns') |
| 222 | + ->willReturnSelf(); |
| 223 | + $this->selectMock |
| 224 | + ->expects($this->any()) |
| 225 | + ->method('where') |
| 226 | + ->willReturnSelf(); |
| 227 | + $this->selectMock |
| 228 | + ->expects($this->any()) |
| 229 | + ->method('order') |
| 230 | + ->willReturnSelf(); |
| 231 | + $this->selectMock |
| 232 | + ->expects($this->any()) |
| 233 | + ->method('group') |
| 234 | + ->willReturnSelf(); |
| 235 | + $this->selectMock |
| 236 | + ->expects($this->any()) |
| 237 | + ->method('getPart') |
| 238 | + ->willReturn([]); |
| 239 | + $this->connectionMock = $this->getMockBuilder(Mysql::class) |
| 240 | + ->onlyMethods(['select', 'getIfNullSql', 'getDateFormatSql', 'prepareSqlCondition', 'getCheckSql']) |
| 241 | + ->disableOriginalConstructor() |
| 242 | + ->getMock(); |
| 243 | + $this->connectionMock |
| 244 | + ->expects($this->any()) |
| 245 | + ->method('select') |
| 246 | + ->willReturn($this->selectMock); |
| 247 | + $this->resourceMock = $this->getMockBuilder(AbstractDb::class) |
| 248 | + ->disableOriginalConstructor() |
| 249 | + ->getMock(); |
| 250 | + $this->resourceMock |
| 251 | + ->expects($this->once()) |
| 252 | + ->method('getConnection') |
| 253 | + ->willReturn($this->connectionMock); |
| 254 | + return new Collection( |
| 255 | + $this->entityFactoryMock, |
| 256 | + $this->loggerMock, |
| 257 | + $this->fetchStrategyMock, |
| 258 | + $this->managerMock, |
| 259 | + $this->entitySnapshotMock, |
| 260 | + $this->helperMock, |
| 261 | + $this->scopeConfigMock, |
| 262 | + $this->storeManagerMock, |
| 263 | + $this->timezoneMock, |
| 264 | + $this->configMock, |
| 265 | + $this->orderFactoryMock, |
| 266 | + null, |
| 267 | + $this->resourceMock |
| 268 | + ); |
| 269 | + } |
| 270 | +} |
0 commit comments