Skip to content

Commit 168fc90

Browse files
committed
ACP2E-977: Incorrect Dashboard YTD values
1 parent 4528625 commit 168fc90

File tree

1 file changed

+205
-0
lines changed
  • app/code/Magento/Backend/Test/Unit/Model/Dashboard/Chart

1 file changed

+205
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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\Pdo\Mysql;
15+
use Magento\Framework\DB\Helper;
16+
use Magento\Framework\DB\Select;
17+
use Magento\Framework\Event\ManagerInterface;
18+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
19+
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
20+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
21+
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
22+
use Magento\Backend\Model\Dashboard\Period;
23+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
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\TestCase;
30+
use Psr\Log\LoggerInterface;
31+
32+
class DateTest extends TestCase
33+
{
34+
/**
35+
* @var Date
36+
*/
37+
private $model;
38+
39+
/**
40+
* @var Collection
41+
*/
42+
protected $collection;
43+
44+
/**
45+
* @var CollectionFactory
46+
*/
47+
private $collectionFactoryMock;
48+
49+
/**
50+
* @var ObjectManager
51+
*/
52+
private $objectManagerHelper;
53+
54+
/**
55+
* @inheritDoc
56+
*/
57+
protected function setUp(): void
58+
{
59+
$this->entityFactoryMock = $this->getMockBuilder(EntityFactory::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
63+
->getMock();
64+
$this->fetchStrategyMock = $this->getMockBuilder(
65+
FetchStrategyInterface::class
66+
)->getMock();
67+
$this->managerMock = $this->getMockBuilder(ManagerInterface::class)
68+
->getMock();
69+
$snapshotClassName = Snapshot::class;
70+
$this->entitySnapshotMock = $this->getMockBuilder($snapshotClassName)
71+
->disableOriginalConstructor()
72+
->getMock();
73+
$this->helperMock = $this->getMockBuilder(Helper::class)
74+
->disableOriginalConstructor()
75+
->getMock();
76+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
77+
->getMock();
78+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
79+
->getMock();
80+
$this->timezoneMock = $this->getMockBuilder(TimezoneInterface::class)
81+
->getMock();
82+
$this->timezoneMock
83+
->expects($this->any())
84+
->method('getConfigTimezone')
85+
->willReturn('America/Chicago');
86+
87+
$this->configMock = $this->getMockBuilder(Config::class)
88+
->disableOriginalConstructor()
89+
->getMock();
90+
$this->orderFactoryMock = $this->getMockBuilder(OrderFactory::class)
91+
->onlyMethods(['create'])
92+
->disableOriginalConstructor()
93+
->getMock();
94+
$this->selectMock = $this->getMockBuilder(Select::class)
95+
->disableOriginalConstructor()
96+
->getMock();
97+
$this->selectMock
98+
->expects($this->any())
99+
->method('columns')
100+
->willReturnSelf();
101+
$this->selectMock
102+
->expects($this->any())
103+
->method('where')
104+
->willReturnSelf();
105+
$this->selectMock
106+
->expects($this->any())
107+
->method('order')
108+
->willReturnSelf();
109+
$this->selectMock
110+
->expects($this->any())
111+
->method('group')
112+
->willReturnSelf();
113+
$this->selectMock
114+
->expects($this->any())
115+
->method('getPart')
116+
->willReturn([]);
117+
$this->connectionMock = $this->getMockBuilder(Mysql::class)
118+
->onlyMethods(['select', 'getIfNullSql', 'getDateFormatSql', 'prepareSqlCondition', 'getCheckSql'])
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$this->connectionMock
122+
->expects($this->any())
123+
->method('select')
124+
->willReturn($this->selectMock);
125+
126+
$this->resourceMock = $this->getMockBuilder(AbstractDb::class)
127+
->disableOriginalConstructor()
128+
->getMock();
129+
$this->resourceMock
130+
->expects($this->once())
131+
->method('getConnection')
132+
->willReturn($this->connectionMock);
133+
134+
$this->collection = new Collection(
135+
$this->entityFactoryMock,
136+
$this->loggerMock,
137+
$this->fetchStrategyMock,
138+
$this->managerMock,
139+
$this->entitySnapshotMock,
140+
$this->helperMock,
141+
$this->scopeConfigMock,
142+
$this->storeManagerMock,
143+
$this->timezoneMock,
144+
$this->configMock,
145+
$this->orderFactoryMock,
146+
null,
147+
$this->resourceMock
148+
);
149+
150+
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
151+
->disableOriginalConstructor()
152+
->getMock();
153+
$this->collectionFactoryMock
154+
->expects($this->any())
155+
->method('create')
156+
->willReturn($this->collection);
157+
158+
$this->objectManagerHelper = new ObjectManager($this);
159+
160+
$this->model = $this->objectManagerHelper->getObject(
161+
Date::class,
162+
[
163+
'collectionFactory' => $this->collectionFactoryMock,
164+
'localeDate' => $this->timezoneMock
165+
]
166+
);
167+
}
168+
169+
/**
170+
* @param string $period
171+
* @param string $config
172+
* @param int $expectedYear
173+
*
174+
* @return void
175+
* @dataProvider getByPeriodDataProvider
176+
*/
177+
public function testGetByPeriod($period, $config, $expectedYear): void
178+
{
179+
$this->scopeConfigMock
180+
->expects($this->once())
181+
->method('getValue')
182+
->with(
183+
$config,
184+
ScopeInterface::SCOPE_STORE
185+
)
186+
->willReturn(1);
187+
$dates = $this->model->getByPeriod($period);
188+
$this->assertEquals($expectedYear, substr($dates[0],0,4));
189+
}
190+
191+
/**
192+
* @return array
193+
*/
194+
public function getByPeriodDataProvider(): array
195+
{
196+
$dateStart = new \DateTime();
197+
$expectedYear = $dateStart->format('Y');
198+
$expected2YTDYear = $expectedYear - 1;
199+
200+
return [
201+
[Period::PERIOD_1_YEAR, 'reports/dashboard/ytd_start', $expectedYear],
202+
[Period::PERIOD_2_YEARS, 'reports/dashboard/ytd_start', $expected2YTDYear]
203+
];
204+
}
205+
}

0 commit comments

Comments
 (0)