Completed
Pull Request — master (#40)
by Chad
01:35 queued 30s
created

DateTimeZoneTest::fromOffsetInvalidDSTValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
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 View Code Duplication
    public function fromStringOutliers(string $abbreviation, string $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 basic behavior of getLongName().
114
     *
115
     * @test
116
     * @covers ::getLongName
117
     *
118
     * @return void
119
     */
120
    public function getLongName()
121
    {
122
        $timezone = new \DateTimeZone('HST');
123
        $this->assertSame('Pacific/Honolulu', DateTimeZone::getLongName($timezone));
124
    }
125
126
    /**
127
     * Verify basic behavior of getLongName().
128
     *
129
     * @test
130
     * @covers ::getLongName
131
     *
132
     * @return void
133
     */
134
    public function getLongNameWithLongName()
135
    {
136
        $timezone = new \DateTimeZone('Pacific/Honolulu');
137
        $this->assertSame('Pacific/Honolulu', DateTimeZone::getLongName($timezone));
138
    }
139
140
    /**
141
     * Verify behavior of getLongName() with outlier.
142
     *
143
     * @test
144
     * @covers ::getLongName
145
     *
146
     * @return void
147
     */
148
    public function getLongNameOutlier()
149
    {
150
        $timezone = new \DateTimeZone('UTC');
151
        $this->assertSame('UTC', DateTimeZone::getLongName($timezone));
152
    }
153
}
154