Completed
Push — master ( 42a2ad...714685 )
by Chad
12s queued 10s
created

DateTimeZoneTest::fromOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace SubjectivePHPTest\Util;
3
4
use SubjectivePHP\Util\DateTimeZone;
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * @coversDefaultClass \SubjectivePHP\Util\DateTimeZone
9
 * @covers ::<private>
10
 */
11
final class DateTimeZoneTest extends TestCase
12
{
13
    /**
14
     * Verify basic behavior of fromString()
15
     *
16
     * @test
17
     * @covers ::fromString
18
     *
19
     * @return void
20
     */
21
    public function fromString()
22
    {
23
        $timezone = DateTimeZone::fromString('Pacific/Honolulu');
24
        $this->assertSame('Pacific/Honolulu', $timezone->getName());
25
        $this->assertSame(-36000, $timezone->getOffset(new \DateTime('now', $timezone)));
26
    }
27
28
    /**
29
     * Verify behavior of fromString() with default timezone.
30
     *
31
     * @test
32
     * @covers ::fromString
33
     *
34
     * @return void
35
     */
36
    public function fromStringDefaultTimeZone()
37
    {
38
        $timezone = new \DateTimeZone('Pacific/Honolulu');
39
        $this->assertSame($timezone, DateTimeZone::fromString('Invalid', $timezone));
40
    }
41
42
    /**
43
     * Verify fromString() defaults to UTC on error.
44
     *
45
     * @test
46
     * @covers ::fromString
47
     *
48
     * @return void
49
     */
50
    public function fromStringWithInvalidAbbreviation()
51
    {
52
        $this->assertNull(DateTimeZone::fromString('NOT VALID'));
53
    }
54
55
    /**
56
     * Verify fromString() correctly converts edge case timezones.
57
     *
58
     * @param string $abbreviation The abbreviation to tests.
59
     * @param string $expected     The expected result from the fromString() call.
60
     *
61
     * @test
62
     * @covers ::fromString
63
     * @dataProvider getOutliers
64
     *
65
     * @return void
66
     */
67
    public function fromStringOutliers($abbreviation, $expected)
68
    {
69
        if (timezone_name_from_abbr($abbreviation) !== false) {
70
            $this->markTestSkipped(
71
                "The timezone abbreviation '{$abbreviation}' is not considered an outlier on this system"
72
            );
73
            return;
74
        }
75
76
        $this->assertSame($expected, DateTimeZone::fromString($abbreviation)->getName());
77
    }
78
79
    /**
80
     * Dataprovider for outlier testing
81
     *
82
     * @return array
83
     */
84
    public function getOutliers()
85
    {
86
        return [
87
            ['WIB', 'Asia/Jakarta'],
88
            ['FET', 'Europe/Helsinki'],
89
            ['AEST', 'Australia/Tasmania'],
90
            ['AWST', 'Australia/West'],
91
            ['WITA', 'Asia/Makassar'],
92
            ['AEDT', 'Australia/Sydney'],
93
            ['ACDT', 'Australia/Adelaide'],
94
        ];
95
    }
96
97
    /**
98
     * Verify basic behavior of fromOffset()
99
     *
100
     * @test
101
     * @covers ::fromOffset
102
     * @uses \SubjectivePHP\Util\DateTimeZone::fromString
103
     *
104
     * @return void
105
     */
106
    public function fromOffset()
107
    {
108
        $timezone = DateTimeZone::fromOffset(-36000, false);
109
        $this->assertSame('Pacific/Honolulu', $timezone->getName());
110
    }
111
112
    /**
113
     * Verify behavior of fromOffset() when $gmtOffset is not an integer.
114
     *
115
     * @test
116
     * @covers ::fromOffset
117
     * @expectedException \InvalidArgumentException
118
     * @expectedExceptionMessage $gmtOffset must be an integer
119
     *
120
     * @return void
121
     */
122
    public function fromOffsetInvalidOffsetValue()
123
    {
124
        DateTimeZone::fromOffset(false, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
    }
126
127
    /**
128
     * Verify behavior of fromOffset() when $isDaylightSavings is not a boolean.
129
     *
130
     * @test
131
     * @covers ::fromOffset
132
     * @expectedException \InvalidArgumentException
133
     * @expectedExceptionMessage $isDaylightSavings must be a boolean
134
     *
135
     * @return void
136
     */
137
    public function fromOffsetInvalidDSTValue()
138
    {
139
        DateTimeZone::fromOffset(-36000, 0);
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
140
    }
141
142
    /**
143
     * Verify basic behavior of getLongName().
144
     *
145
     * @test
146
     * @covers ::getLongName
147
     *
148
     * @return void
149
     */
150
    public function getLongName()
151
    {
152
        $timezone = new \DateTimeZone('HST');
153
        $this->assertSame('Pacific/Honolulu', DateTimeZone::getLongName($timezone));
154
    }
155
156
    /**
157
     * Verify basic behavior of getLongName().
158
     *
159
     * @test
160
     * @covers ::getLongName
161
     *
162
     * @return void
163
     */
164
    public function getLongNameWithLongName()
165
    {
166
        $timezone = new \DateTimeZone('Pacific/Honolulu');
167
        $this->assertSame('Pacific/Honolulu', DateTimeZone::getLongName($timezone));
168
    }
169
170
    /**
171
     * Verify behavior of getLongName() with outlier.
172
     *
173
     * @test
174
     * @covers ::getLongName
175
     *
176
     * @return void
177
     */
178
    public function getLongNameOutlier()
179
    {
180
        $timezone = new \DateTimeZone('UTC');
181
        $this->assertSame('UTC', DateTimeZone::getLongName($timezone));
182
    }
183
}
184