5
5
*/
6
6
namespace Magento \Framework \Stdlib \Test \Unit \DateTime ;
7
7
8
+ use DateTimeImmutable ;
9
+ use DateTimeInterface ;
10
+ use Exception ;
8
11
use Magento \Framework \Stdlib \DateTime \DateTime ;
9
12
use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
13
+ use PHPUnit \Framework \MockObject \MockObject ;
14
+ use PHPUnit \Framework \TestCase ;
10
15
11
16
/**
12
- * Magento\Framework\Stdlib\DateTimeTest test case
17
+ * Tests for @see DateTime
13
18
*/
14
- class DateTimeTest extends \ PHPUnit \ Framework \ TestCase
19
+ class DateTimeTest extends TestCase
15
20
{
16
21
/**
17
22
* @var string
18
23
*/
19
24
private $ testDate = '2015-04-02 21:03:00 ' ;
20
25
21
26
/**
22
- * @param int|string|\DateTimeInterface $input
27
+ * @param int|string|DateTimeInterface $input
28
+ * @throws Exception
29
+ *
23
30
* @dataProvider dateTimeInputDataProvider
24
31
*/
25
32
public function testGmtTimestamp ($ input )
26
33
{
27
- /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
34
+ /** @var TimezoneInterface|MockObject $timezone */
28
35
$ timezone = $ this ->getMockBuilder (TimezoneInterface::class)->getMock ();
29
36
$ timezone ->method ('date ' )->willReturn (new \DateTime ($ this ->testDate ));
30
37
@@ -33,12 +40,14 @@ public function testGmtTimestamp($input)
33
40
}
34
41
35
42
/**
36
- * @param int|string|\DateTimeInterface $input
43
+ * @param int|string|DateTimeInterface $input
44
+ * @throws Exception
45
+ *
37
46
* @dataProvider dateTimeInputDataProvider
38
47
*/
39
48
public function testTimestamp ($ input )
40
49
{
41
- /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
50
+ /** @var TimezoneInterface|MockObject $timezone */
42
51
$ timezone = $ this ->getMockBuilder (TimezoneInterface::class)->getMock ();
43
52
$ timezone ->method ('date ' )->willReturn (new \DateTime ($ this ->testDate ));
44
53
@@ -48,29 +57,50 @@ public function testTimestamp($input)
48
57
49
58
public function testGtmOffset ()
50
59
{
51
- /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
60
+ /** @var TimezoneInterface|MockObject $timezone */
52
61
$ timezone = $ this ->getMockBuilder (TimezoneInterface::class)->getMock ();
53
- // Asia/Tbilisi timezone have no DST
54
- $ timezone ->method ('getConfigTimezone ' )->willReturn ('Asia/Tbilisi ' );
62
+ $ timezone ->method ('getConfigTimezone ' )->willReturn ('Europe/Amsterdam ' );
55
63
56
- /** @var DateTime|\PHPUnit_Framework_MockObject_MockObject $dateTime */
64
+ /** @var DateTime|MockObject $dateTime */
57
65
$ dateTime = $ this ->getMockBuilder (DateTime::class)
58
66
->setConstructorArgs ([$ timezone ])
59
67
->setMethods (null )
60
68
->getMock ();
61
69
62
- $ this ->assertEquals (14400 , $ dateTime ->getGmtOffset ());
70
+ $ this ->assertEquals (
71
+ $ this ->getExpectedGtmOffset ($ timezone ->getConfigTimezone ()),
72
+ $ dateTime ->getGmtOffset ()
73
+ );
63
74
}
64
75
65
76
/**
77
+ * Returns expected offset according to Daylight Saving Time in timezone
78
+ *
79
+ * @param string $timezoneIdentifier
80
+ * @return int
81
+ */
82
+ private function getExpectedGtmOffset (string $ timezoneIdentifier ): int
83
+ {
84
+ $ timeZoneToReturn = date_default_timezone_get ();
85
+ date_default_timezone_set ($ timezoneIdentifier );
86
+ $ expectedOffset = (date ('I ' , time ()) + 1 ) * 3600 ;
87
+ date_default_timezone_set ($ timeZoneToReturn );
88
+
89
+ return (int ) $ expectedOffset ;
90
+ }
91
+
92
+ /**
93
+ * Data provider
94
+ *
66
95
* @return array
96
+ * @throws Exception
67
97
*/
68
98
public function dateTimeInputDataProvider ()
69
99
{
70
100
return [
71
101
'string ' => [$ this ->testDate ],
72
102
'int ' => [strtotime ($ this ->testDate )],
73
- \ DateTimeInterface::class => [new \ DateTimeImmutable ($ this ->testDate )],
103
+ DateTimeInterface::class => [new DateTimeImmutable ($ this ->testDate )],
74
104
];
75
105
}
76
106
}
0 commit comments