1
1
<?php declare (strict_types=1 );
2
2
3
- namespace App \ Tests \ Model ;
3
+ namespace Darsyn \ DateTime \ Tests ;
4
4
5
5
use Darsyn \DateTime \DateTime ;
6
6
use Darsyn \DateTime \DateTimeInterface ;
@@ -11,42 +11,63 @@ class DateTimeTest extends Test
11
11
public function dataProviderValidConstructionValues (): array
12
12
{
13
13
return [
14
- ['2018-05-09T14:18:00+02:00 ' , 'America/Los_Angeles ' , '2018-05-09T14:18:00+02:00 ' ],
14
+ // Like most central African countries, Nairobi has never observed daylight-savings so this test *should*
15
+ // always be correct.
16
+ ['2018-05-09T14:18:00+02:00 ' , 'Africa/Nairobi ' , '2018-05-09T15:18:00+03:00 ' , 'Africa/Nairobi ' ],
15
17
// Bangkok does not have daylight savings time so this test *should* always be correct.
16
- ['2018-01-01T00:00:00 ' , 'Asia/Bangkok ' , '2018-01-01T00:00:00+07:00 ' ],
17
- ['2018-01-01T00:00:00-05:00 ' , 'Asia/Bangkok ' , '2018-01-01T00:00:00-05:00 ' ],
18
- ['2018-01-01T00:00:00-05:00 ' , null , '2018-01-01T00:00:00-05:00 ' ],
19
-
18
+ ['2018-01-01T00:00:00 ' , 'Asia/Bangkok ' , '2018-01-01T00:00:00+07:00 ' , 'Asia/Bangkok ' ],
19
+ ['2018-01-01T00:00:00-05:00 ' , 'Asia/Bangkok ' , '2018-01-01T12:00:00+07:00 ' , 'Asia/Bangkok ' ],
20
+ ['2018-01-01T00:00:00-05:00 ' , null , '2018-01-01T00:00:00-05:00 ' , '-05:00 ' ],
20
21
];
21
22
}
22
23
23
24
public function dataProviderValidFormatValues (): array
24
25
{
25
26
return [
26
- ['Y-m-d\TH:i:sP ' , '2018-05-09T14:18:00 +02:00 ' , 'America/Los_Angeles ' , '2018-05-09T14 :18:00+02 :00 ' ],
27
+ ['Y-m-d\TH:i:sP ' , '2018-05-09T14:18:59 +02:00 ' , 'America/Los_Angeles ' , '2018-05-09T05 :18:59-07 :00 ' , ' America/Los_Angeles ' ],
27
28
// Perth does not have daylight savings time so this test *should* always be correct.
28
- ['l, jS F, Y (g:ia) ' , 'Wednesday, 9th May, 2018 (3:34pm) ' , 'Australia/Perth ' , '2018-05-09T15:34:00+08:00 ' ],
29
+ ['l, jS F, Y (g:ia) ' , 'Wednesday, 9th May, 2018 (3:34pm) ' , 'Australia/Perth ' , '2018-05-09T15:34:00+08:00 ' , 'Australia/Perth ' ],
30
+ // We must specify the timezone in the string, else the timezone will be taken from the php.ini settings
31
+ // which would differ from environment to environment and fail the test.
32
+ ['l, jS F, Y (g:ia) P ' , 'Wednesday, 9th May, 2018 (3:34pm) -11:00 ' , null , '2018-05-09T15:34:00-11:00 ' , '-11:00 ' ],
33
+ ];
34
+ }
35
+
36
+ public function dataProviderValidTimestampValues (): array
37
+ {
38
+ return [
39
+ [1548075139 , 'Africa/Nairobi ' , '2019-01-21T15:52:19+03:00 ' , 'Africa/Nairobi ' ],
40
+ [1500000000 , 'Australia/Perth ' , '2017-07-14T10:40:00+08:00 ' , 'Australia/Perth ' ],
41
+ [1500000000 , 'Asia/Bangkok ' , '2017-07-14T09:40:00+07:00 ' , 'Asia/Bangkok ' ],
42
+ [1400000000 , null , null , null ],
29
43
];
30
44
}
31
45
32
46
/** @dataProvider dataProviderValidConstructionValues */
33
- public function testValidDates (string $ value , ?string $ timezone , string $ expected ): void
47
+ public function testValidDates (string $ value , ?string $ timezone , string $ expectedDate , string $ expectedTz ): void
34
48
{
35
49
$ timezone = is_string ($ timezone )
36
50
? new \DateTimeZone ($ timezone )
37
51
: null ;
38
52
$ datetime = new DateTime ($ value , $ timezone );
39
- Test::assertSame ($ expected , (string ) $ datetime );
53
+ Test::assertSame ($ expectedDate , (string ) $ datetime );
54
+ Test::assertSame ($ expectedTz , $ datetime ->getTimezone ()->getName ());
40
55
}
41
56
42
57
/** @dataProvider dataProviderValidFormatValues */
43
- public function testValidFormats (string $ format , string $ value , ?string $ timezone , string $ expected ): void
44
- {
58
+ public function testValidFormats (
59
+ string $ format ,
60
+ string $ value ,
61
+ ?string $ timezone ,
62
+ string $ expectedDate ,
63
+ string $ expectedTz
64
+ ): void {
45
65
$ timezone = is_string ($ timezone )
46
66
? new \DateTimeZone ($ timezone )
47
67
: null ;
48
68
$ datetime = DateTime::createFromFormat ($ format , $ value , $ timezone );
49
- Test::assertSame ($ expected , (string ) $ datetime );
69
+ Test::assertSame ($ expectedDate , (string ) $ datetime );
70
+ Test::assertSame ($ expectedTz , $ datetime ->getTimezone ()->getName ());
50
71
}
51
72
52
73
public function testNoConstructorArgumentsIndicateCurrentDate (): void
@@ -75,4 +96,25 @@ public function testDateTimeSerialisesIntoJsonNicelyUnlikeThePhpVersionWhichDoes
75
96
// Ensure that it's in standardized UTC format.
76
97
Test::assertSame (json_encode ($ datetime ->format (DateTimeInterface::RFC3339 )), json_encode ($ datetime ));
77
98
}
99
+
100
+ /** @dataProvider dataProviderValidTimestampValues */
101
+ public function testDateIsCorrectWhenTimezoneSuppliedWithTimestamp (
102
+ int $ timestamp ,
103
+ ?string $ timezone ,
104
+ ?string $ expectedDate ,
105
+ ?string $ expectedTz
106
+ ): void {
107
+ $ timezone = \is_string ($ timezone )
108
+ ? new \DateTimeZone ($ timezone )
109
+ : null ;
110
+ $ datetime = DateTime::createFromTimestamp ($ timestamp , $ timezone );
111
+ Test::assertSame ($ timestamp , $ datetime ->getTimestamp ());
112
+ // A timestamp cannot contain any timezone information, if no timezone was set then the timezone defined in
113
+ // php.ini is used, and as it differs depending on the environment we cannot test for it (or the resulting
114
+ // date string which is dependant on the timezone).
115
+ if ($ timezone !== null ) {
116
+ Test::assertSame ($ expectedDate , (string ) $ datetime );
117
+ Test::assertSame ($ expectedTz , $ datetime ->getTimezone ()->getName ());
118
+ }
119
+ }
78
120
}
0 commit comments