Skip to content

Commit 28d48a1

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

File tree

1 file changed

+133
-59
lines changed
  • app/code/Magento/Backend/Test/Unit/Model/Dashboard/Chart

1 file changed

+133
-59
lines changed

app/code/Magento/Backend/Test/Unit/Model/Dashboard/Chart/DateTest.php

Lines changed: 133 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
1313
use Magento\Framework\Data\Collection\EntityFactory;
14+
use Magento\Framework\DB\Adapter\AdapterInterface;
1415
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1516
use Magento\Framework\DB\Helper;
1617
use Magento\Framework\DB\Select;
@@ -26,6 +27,7 @@
2627
use Magento\Sales\Model\ResourceModel\Report\OrderFactory;
2728
use Magento\Store\Model\ScopeInterface;
2829
use Magento\Store\Model\StoreManagerInterface;
30+
use PHPUnit\Framework\MockObject\MockObject;
2931
use PHPUnit\Framework\TestCase;
3032
use Psr\Log\LoggerInterface;
3133

@@ -41,6 +43,76 @@ class DateTest extends TestCase
4143
*/
4244
protected $collection;
4345

46+
/**
47+
* @var EntityFactory|MockObject
48+
*/
49+
protected $entityFactoryMock;
50+
51+
/**
52+
* @var LoggerInterface|MockObject
53+
*/
54+
protected $loggerMock;
55+
56+
/**
57+
* @var FetchStrategyInterface|MockObject
58+
*/
59+
protected $fetchStrategyMock;
60+
61+
/**
62+
* @var ManagerInterface|MockObject
63+
*/
64+
protected $managerMock;
65+
66+
/**
67+
* @var \Magento\Sales\Model\ResourceModel\EntitySnapshot|MockObject
68+
*/
69+
protected $entitySnapshotMock;
70+
71+
/**
72+
* @var Helper|MockObject
73+
*/
74+
protected $helperMock;
75+
76+
/**
77+
* @var ScopeConfigInterface|MockObject
78+
*/
79+
protected $scopeConfigMock;
80+
81+
/**
82+
* @var StoreManagerInterface|MockObject
83+
*/
84+
protected $storeManagerMock;
85+
86+
/**
87+
* @var TimezoneInterface|MockObject
88+
*/
89+
protected $timezoneMock;
90+
91+
/**
92+
* @var Config|MockObject
93+
*/
94+
protected $configMock;
95+
96+
/**
97+
* @var OrderFactory|MockObject
98+
*/
99+
protected $orderFactoryMock;
100+
101+
/**
102+
* @var AdapterInterface|MockObject
103+
*/
104+
protected $connectionMock;
105+
106+
/**
107+
* @var Select|MockObject
108+
*/
109+
protected $selectMock;
110+
111+
/**
112+
* @var AbstractDb|MockObject
113+
*/
114+
protected $resourceMock;
115+
44116
/**
45117
* @var CollectionFactory
46118
*/
@@ -55,6 +127,66 @@ class DateTest extends TestCase
55127
* @inheritDoc
56128
*/
57129
protected function setUp(): void
130+
{
131+
$this->collection = $this->getCollectionObject();
132+
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
133+
->disableOriginalConstructor()
134+
->getMock();
135+
$this->collectionFactoryMock
136+
->expects($this->any())
137+
->method('create')
138+
->willReturn($this->collection);
139+
$this->objectManagerHelper = new ObjectManager($this);
140+
$this->model = $this->objectManagerHelper->getObject(
141+
Date::class,
142+
[
143+
'collectionFactory' => $this->collectionFactoryMock,
144+
'localeDate' => $this->timezoneMock
145+
]
146+
);
147+
}
148+
149+
/**
150+
* @param string $period
151+
* @param string $config
152+
* @param int $expectedYear
153+
*
154+
* @return void
155+
* @dataProvider getByPeriodDataProvider
156+
*/
157+
public function testGetByPeriod($period, $config, $expectedYear): void
158+
{
159+
$this->scopeConfigMock
160+
->expects($this->once())
161+
->method('getValue')
162+
->with(
163+
$config,
164+
ScopeInterface::SCOPE_STORE
165+
)
166+
->willReturn(1);
167+
$dates = $this->model->getByPeriod($period);
168+
$this->assertEquals($expectedYear, substr($dates[0], 0, 4));
169+
}
170+
171+
/**
172+
* @return array
173+
*/
174+
public function getByPeriodDataProvider(): array
175+
{
176+
$dateStart = new \DateTime();
177+
$expectedYear = $dateStart->format('Y');
178+
$expected2YTDYear = $expectedYear - 1;
179+
180+
return [
181+
[Period::PERIOD_1_YEAR, 'reports/dashboard/ytd_start', $expectedYear],
182+
[Period::PERIOD_2_YEARS, 'reports/dashboard/ytd_start', $expected2YTDYear]
183+
];
184+
}
185+
186+
/**
187+
* @return Collection
188+
*/
189+
private function getCollectionObject()
58190
{
59191
$this->entityFactoryMock = $this->getMockBuilder(EntityFactory::class)
60192
->disableOriginalConstructor()
@@ -83,7 +215,6 @@ protected function setUp(): void
83215
->expects($this->any())
84216
->method('getConfigTimezone')
85217
->willReturn('America/Chicago');
86-
87218
$this->configMock = $this->getMockBuilder(Config::class)
88219
->disableOriginalConstructor()
89220
->getMock();
@@ -122,16 +253,14 @@ protected function setUp(): void
122253
->expects($this->any())
123254
->method('select')
124255
->willReturn($this->selectMock);
125-
126256
$this->resourceMock = $this->getMockBuilder(AbstractDb::class)
127257
->disableOriginalConstructor()
128258
->getMock();
129259
$this->resourceMock
130260
->expects($this->once())
131261
->method('getConnection')
132262
->willReturn($this->connectionMock);
133-
134-
$this->collection = new Collection(
263+
return new Collection(
135264
$this->entityFactoryMock,
136265
$this->loggerMock,
137266
$this->fetchStrategyMock,
@@ -146,60 +275,5 @@ protected function setUp(): void
146275
null,
147276
$this->resourceMock
148277
);
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-
];
204278
}
205279
}

0 commit comments

Comments
 (0)