@@ -22,6 +22,16 @@ class TimezoneTest extends \PHPUnit\Framework\TestCase
2222 */
2323 private $ defaultTimeZone ;
2424
25+ /**
26+ * @var string
27+ */
28+ private $ scopeType ;
29+
30+ /**
31+ * @var string
32+ */
33+ private $ defaultTimezonePath ;
34+
2535 /**
2636 * @var ObjectManager
2737 */
@@ -49,6 +59,8 @@ protected function setUp()
4959 {
5060 $ this ->defaultTimeZone = date_default_timezone_get ();
5161 date_default_timezone_set ('UTC ' );
62+ $ this ->scopeType = 'store ' ;
63+ $ this ->defaultTimezonePath = 'default/timezone/path ' ;
5264
5365 $ this ->objectManager = new ObjectManager ($ this );
5466 $ this ->scopeResolver = $ this ->getMockBuilder (ScopeResolverInterface::class)->getMock ();
@@ -86,9 +98,10 @@ public function testDateIncludeTime($date, $locale, $includeTime, $expectedTimes
8698
8799 /**
88100 * DataProvider for testDateIncludeTime
101+ *
89102 * @return array
90103 */
91- public function dateIncludeTimeDataProvider ()
104+ public function dateIncludeTimeDataProvider (): array
92105 {
93106 return [
94107 'Parse d/m/y date without time ' => [
@@ -133,9 +146,10 @@ public function testConvertConfigTimeToUtc($date, $configuredTimezone, $expected
133146
134147 /**
135148 * Data provider for testConvertConfigTimeToUtc
149+ *
136150 * @return array
137151 */
138- public function getConvertConfigTimeToUtcFixtures ()
152+ public function getConvertConfigTimeToUtcFixtures (): array
139153 {
140154 return [
141155 'string ' => [
@@ -181,9 +195,10 @@ public function testDate()
181195
182196 /**
183197 * DataProvider for testDate
198+ *
184199 * @return array
185200 */
186- private function getDateFixtures ()
201+ private function getDateFixtures (): array
187202 {
188203 return [
189204 'now_datetime_utc ' => [
@@ -239,29 +254,71 @@ private function getTimezone()
239254 return new Timezone (
240255 $ this ->scopeResolver ,
241256 $ this ->localeResolver ,
242- $ this ->getMockBuilder (DateTime::class)-> getMock ( ),
257+ $ this ->createMock (DateTime::class),
243258 $ this ->scopeConfig ,
244- '' ,
245- ''
259+ $ this -> scopeType ,
260+ $ this -> defaultTimezonePath
246261 );
247262 }
248263
249264 /**
250265 * @param string $configuredTimezone
266+ * @param string|null $scope
251267 */
252- private function scopeConfigWillReturnConfiguredTimezone ($ configuredTimezone )
268+ private function scopeConfigWillReturnConfiguredTimezone (string $ configuredTimezone, string $ scope = null )
253269 {
254- $ this ->scopeConfig ->method ('getValue ' )->with ('' , '' , null )->willReturn ($ configuredTimezone );
270+ $ this ->scopeConfig ->expects ($ this ->atLeastOnce ())
271+ ->method ('getValue ' )
272+ ->with ($ this ->defaultTimezonePath , $ this ->scopeType , $ scope )
273+ ->willReturn ($ configuredTimezone );
255274 }
256275
257- public function testCheckIfScopeDateSetsTimeZone ()
276+ /**
277+ * @dataProvider scopeDateDataProvider
278+ * @param \DateTimeInterface|string|int $date
279+ * @param string $timezone
280+ * @param string $locale
281+ * @param string $expectedDate
282+ */
283+ public function testScopeDate ($ date , string $ timezone , string $ locale , string $ expectedDate )
258284 {
259- $ scopeDate = new \DateTime ('now ' , new \DateTimeZone ('America/Vancouver ' ));
260- $ this ->scopeConfig ->method ('getValue ' )->willReturn ('America/Vancouver ' );
285+ $ scopeCode = 'test ' ;
261286
262- $ this ->assertEquals (
263- $ scopeDate ->getTimezone (),
264- $ this ->getTimezone ()->scopeDate (0 , $ scopeDate ->getTimestamp ())->getTimezone ()
265- );
287+ $ this ->scopeConfigWillReturnConfiguredTimezone ($ timezone , $ scopeCode );
288+ $ this ->localeResolver ->method ('getLocale ' )
289+ ->willReturn ($ locale );
290+
291+ $ scopeDate = $ this ->getTimezone ()->scopeDate ($ scopeCode , $ date , true );
292+ $ this ->assertEquals ($ expectedDate , $ scopeDate ->format ('Y-m-d H:i:s ' ));
293+ $ this ->assertEquals ($ timezone , $ scopeDate ->getTimezone ()->getName ());
294+ }
295+
296+ /**
297+ * @return array
298+ */
299+ public function scopeDateDataProvider (): array
300+ {
301+ $ utcTz = new \DateTimeZone ('UTC ' );
302+
303+ return [
304+ ['2018-10-20 00:00:00 ' , 'UTC ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
305+ ['2018-10-20 00:00:00 ' , 'America/Los_Angeles ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
306+ ['2018-10-20 00:00:00 ' , 'Asia/Qatar ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
307+ ['10/20/18 00:00 ' , 'UTC ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
308+ ['10/20/18 00:00 ' , 'America/Los_Angeles ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
309+ ['10/20/18 00:00 ' , 'Asia/Qatar ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
310+ ['20/10/18 00:00 ' , 'UTC ' , 'fr_FR ' , '2018-10-20 00:00:00 ' ],
311+ ['20/10/18 00:00 ' , 'America/Los_Angeles ' , 'fr_FR ' , '2018-10-20 00:00:00 ' ],
312+ ['20/10/18 00:00 ' , 'Asia/Qatar ' , 'fr_FR ' , '2018-10-20 00:00:00 ' ],
313+ [1539993600 , 'UTC ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
314+ [1539993600 , 'America/Los_Angeles ' , 'en_US ' , '2018-10-19 17:00:00 ' ],
315+ [1539993600 , 'Asia/Qatar ' , 'en_US ' , '2018-10-20 03:00:00 ' ],
316+ [new \DateTime ('2018-10-20 ' , $ utcTz ), 'UTC ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
317+ [new \DateTime ('2018-10-20 ' , $ utcTz ), 'America/Los_Angeles ' , 'en_US ' , '2018-10-19 17:00:00 ' ],
318+ [new \DateTime ('2018-10-20 ' , $ utcTz ), 'Asia/Qatar ' , 'en_US ' , '2018-10-20 03:00:00 ' ],
319+ [new \DateTimeImmutable ('2018-10-20 ' , $ utcTz ), 'UTC ' , 'en_US ' , '2018-10-20 00:00:00 ' ],
320+ [new \DateTimeImmutable ('2018-10-20 ' , $ utcTz ), 'America/Los_Angeles ' , 'en_US ' , '2018-10-19 17:00:00 ' ],
321+ [new \DateTimeImmutable ('2018-10-20 ' , $ utcTz ), 'Asia/Qatar ' , 'en_US ' , '2018-10-20 03:00:00 ' ],
322+ ];
266323 }
267324}
0 commit comments