11
11
use Magento \Framework \App \Config \ScopeConfigInterface ;
12
12
use Magento \Framework \Data \Collection \Db \FetchStrategyInterface ;
13
13
use Magento \Framework \Data \Collection \EntityFactory ;
14
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
14
15
use Magento \Framework \DB \Adapter \Pdo \Mysql ;
15
16
use Magento \Framework \DB \Helper ;
16
17
use Magento \Framework \DB \Select ;
26
27
use Magento \Sales \Model \ResourceModel \Report \OrderFactory ;
27
28
use Magento \Store \Model \ScopeInterface ;
28
29
use Magento \Store \Model \StoreManagerInterface ;
30
+ use PHPUnit \Framework \MockObject \MockObject ;
29
31
use PHPUnit \Framework \TestCase ;
30
32
use Psr \Log \LoggerInterface ;
31
33
@@ -41,6 +43,76 @@ class DateTest extends TestCase
41
43
*/
42
44
protected $ collection ;
43
45
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
+
44
116
/**
45
117
* @var CollectionFactory
46
118
*/
@@ -55,6 +127,66 @@ class DateTest extends TestCase
55
127
* @inheritDoc
56
128
*/
57
129
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 ()
58
190
{
59
191
$ this ->entityFactoryMock = $ this ->getMockBuilder (EntityFactory::class)
60
192
->disableOriginalConstructor ()
@@ -83,7 +215,6 @@ protected function setUp(): void
83
215
->expects ($ this ->any ())
84
216
->method ('getConfigTimezone ' )
85
217
->willReturn ('America/Chicago ' );
86
-
87
218
$ this ->configMock = $ this ->getMockBuilder (Config::class)
88
219
->disableOriginalConstructor ()
89
220
->getMock ();
@@ -122,16 +253,14 @@ protected function setUp(): void
122
253
->expects ($ this ->any ())
123
254
->method ('select ' )
124
255
->willReturn ($ this ->selectMock );
125
-
126
256
$ this ->resourceMock = $ this ->getMockBuilder (AbstractDb::class)
127
257
->disableOriginalConstructor ()
128
258
->getMock ();
129
259
$ this ->resourceMock
130
260
->expects ($ this ->once ())
131
261
->method ('getConnection ' )
132
262
->willReturn ($ this ->connectionMock );
133
-
134
- $ this ->collection = new Collection (
263
+ return new Collection (
135
264
$ this ->entityFactoryMock ,
136
265
$ this ->loggerMock ,
137
266
$ this ->fetchStrategyMock ,
@@ -146,60 +275,5 @@ protected function setUp(): void
146
275
null ,
147
276
$ this ->resourceMock
148
277
);
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
278
}
205
279
}
0 commit comments