1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubjectivePHPTest\DateTime\Utilities; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SubjectivePHP\DateTime\Utilities\DateTimeZoneUtil; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @coversDefaultClass \SubjectivePHP\DateTime\Utilities\DateTimeZoneUtil |
12
|
|
|
* @covers ::<private> |
13
|
|
|
*/ |
14
|
|
|
final class DateTimeZoneUtilTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Verify basic behavior of fromString() |
18
|
|
|
* |
19
|
|
|
* @test |
20
|
|
|
* @covers ::fromString |
21
|
|
|
*/ |
22
|
|
|
public function fromString() |
23
|
|
|
{ |
24
|
|
|
$timezone = DateTimeZoneUtil::fromString(DateTimeZoneUtil::TIMEZONE_PACIFIC_HONOLULU); |
25
|
|
|
$this->assertSame(DateTimeZoneUtil::TIMEZONE_PACIFIC_HONOLULU, $timezone->getName()); |
26
|
|
|
$this->assertSame(-36000, $timezone->getOffset(new DateTime('now', $timezone))); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Verify behavior of fromString() with default timezone. |
31
|
|
|
* |
32
|
|
|
* @test |
33
|
|
|
* @covers ::fromString |
34
|
|
|
*/ |
35
|
|
|
public function fromStringDefaultTimeZone() |
36
|
|
|
{ |
37
|
|
|
$timezone = new DateTimeZone(DateTimeZoneUtil::TIMEZONE_PACIFIC_HONOLULU); |
38
|
|
|
$this->assertSame($timezone, DateTimeZoneUtil::fromString('Invalid', $timezone)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Verify fromString() defaults to UTC on error. |
43
|
|
|
* |
44
|
|
|
* @test |
45
|
|
|
* @covers ::fromString |
46
|
|
|
*/ |
47
|
|
|
public function fromStringWithInvalidAbbreviation() |
48
|
|
|
{ |
49
|
|
|
$this->assertNull(DateTimeZoneUtil::fromString('NOT VALID')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Verify fromString() correctly converts edge case timezones. |
54
|
|
|
* |
55
|
|
|
* @param string $abbreviation The abbreviation to tests. |
56
|
|
|
* @param string $expected The expected result from the fromString() call. |
57
|
|
|
* |
58
|
|
|
* @test |
59
|
|
|
* @covers ::fromString |
60
|
|
|
* @dataProvider getOutliers |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function fromStringOutliers(string $abbreviation, string $expected) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if (timezone_name_from_abbr($abbreviation) !== false) { |
65
|
|
|
$this->markTestSkipped( |
66
|
|
|
"The timezone abbreviation '{$abbreviation}' is not considered an outlier on this system" |
67
|
|
|
); |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->assertSame($expected, DateTimeZoneUtil::fromString($abbreviation)->getName()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Dataprovider for outlier testing |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getOutliers() : array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
['WIB', DateTimeZoneUtil::TIMEZONE_ASIA_JAKARTA], |
83
|
|
|
['FET', DateTimeZoneUtil::TIMEZONE_EUROPE_HELSINKI], |
84
|
|
|
['AEST', DateTimeZoneUtil::TIMEZONE_AUSTRALIA_TASMANIA], |
85
|
|
|
['AWST', DateTimeZoneUtil::TIMEZONE_AUSTRALIA_WEST], |
86
|
|
|
['WITA', DateTimeZoneUtil::TIMEZONE_ASIA_MAKASSAR], |
87
|
|
|
['AEDT', DateTimeZoneUtil::TIMEZONE_AUSTRALIA_SYDNEY], |
88
|
|
|
['ACDT', DateTimeZoneUtil::TIMEZONE_AUSTRALIA_ADELAIDE], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Verify basic behavior of fromOffset() |
94
|
|
|
* |
95
|
|
|
* @test |
96
|
|
|
* @covers ::fromOffset |
97
|
|
|
*/ |
98
|
|
|
public function fromOffset() |
99
|
|
|
{ |
100
|
|
|
$timezone = DateTimeZoneUtil::fromOffset(-36000, false); |
101
|
|
|
$this->assertSame(DateTimeZoneUtil::TIMEZONE_PACIFIC_HONOLULU, $timezone->getName()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Verify basic behavior of getLongName(). |
106
|
|
|
* |
107
|
|
|
* @test |
108
|
|
|
* @covers ::getLongName |
109
|
|
|
*/ |
110
|
|
|
public function getLongName() |
111
|
|
|
{ |
112
|
|
|
$timezone = new DateTimeZone('HST'); |
113
|
|
|
$this->assertSame(DateTimeZoneUtil::TIMEZONE_PACIFIC_HONOLULU, DateTimeZoneUtil::getLongName($timezone)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Verify basic behavior of getLongName(). |
118
|
|
|
* |
119
|
|
|
* @test |
120
|
|
|
* @covers ::getLongName |
121
|
|
|
*/ |
122
|
|
|
public function getLongNameWithLongName() |
123
|
|
|
{ |
124
|
|
|
$timezone = new DateTimeZone(DateTimeZoneUtil::TIMEZONE_PACIFIC_HONOLULU); |
125
|
|
|
$this->assertSame(DateTimeZoneUtil::TIMEZONE_PACIFIC_HONOLULU, DateTimeZoneUtil::getLongName($timezone)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Verify behavior of getLongName() with outlier. |
130
|
|
|
* |
131
|
|
|
* @test |
132
|
|
|
* @covers ::getLongName |
133
|
|
|
*/ |
134
|
|
|
public function getLongNameOutlier() |
135
|
|
|
{ |
136
|
|
|
$timezone = new DateTimeZone(DateTimeZoneUtil::TIMEZONE_UTC); |
137
|
|
|
$this->assertSame(DateTimeZoneUtil::TIMEZONE_UTC, DateTimeZoneUtil::getLongName($timezone)); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.