Passed
Pull Request — master (#11)
by Alex
02:52
created

DateTimeZoneFactoryTest::testCreateDateTimeZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DateTime;
6
7
use Arp\DateTime\DateTimeZoneFactory;
8
use Arp\DateTime\DateTimeZoneFactoryInterface;
9
use Arp\DateTime\Exception\DateTimeZoneFactoryException;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @covers  \Arp\DateTime\DateTimeZoneFactory
14
 *
15
 * @author  Alex Patterson <[email protected]>
16
 * @package ArpTest\DateTime
17
 */
18
final class DateTimeZoneFactoryTest extends TestCase
19
{
20
    /**
21
     * Assert the factory implement DateTimeZoneFactoryInterface
22
     */
23
    public function testImplementsDateTimeZoneFactoryInterface(): void
24
    {
25
        $factory = new DateTimeZoneFactory();
26
27
        $this->assertInstanceOf(DateTimeZoneFactoryInterface::class, $factory);
28
    }
29
30
    /**
31
     * Assert that a DateTimeZoneFactoryException is thrown if trying to create the class without a valid
32
     * $dateTimeZoneClassName constructor argument
33
     *
34
     * @throws DateTimeZoneFactoryException
35
     */
36
    public function testDateTimeZoneFactoryExceptionIsThrownWhenProvidingInvalidDateTimeZoneClassName(): void
37
    {
38
        $dateTimeZoneClassName = \stdClass::class;
39
40
        $this->expectException(DateTimeZoneFactoryException::class);
41
        $this->expectExceptionMessage(
42
            sprintf(
43
                'The \'dateTimeZoneClassName\' parameter must be a class name that implements \'%s\'',
44
                \DateTimeZone::class
45
            )
46
        );
47
48
        new DateTimeZoneFactory($dateTimeZoneClassName);
49
    }
50
51
    /**
52
     * Assert that if providing an invalid $spec to createDateTimeZone() a DateTimeZoneFactoryException is thrown
53
     *
54
     * @param string $spec
55
     *
56
     * @dataProvider getCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalidData
57
     *
58
     * @throws DateTimeZoneFactoryException
59
     */
60
    public function testCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalid(string $spec): void
61
    {
62
        $factory = new DateTimeZoneFactory();
63
64
        $exceptionMessage = '';
65
        $exceptionCode = 123;
66
        try {
67
            new \DateTimeZone($spec);
68
        } catch (\Exception $e) {
69
            $exceptionMessage = $e->getMessage();
70
            $exceptionCode = $e->getCode();
71
        }
72
73
        $this->expectException(DateTimeZoneFactoryException::class);
74
        $this->expectExceptionCode($exceptionCode);
75
        $this->expectExceptionMessage(
76
            sprintf(
77
                'Failed to create a valid \DateTimeZone instance using \'%s\': %s',
78
                $spec,
79
                $exceptionMessage
80
            )
81
        );
82
83
        $factory->createDateTimeZone($spec);
84
    }
85
86
    /**
87
     * @return array<int, mixed>
88
     */
89
    public function getCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalidData(): array
90
    {
91
        return [
92
            [
93
                'foo',
94
            ],
95
            [
96
                '123',
97
            ],
98
        ];
99
    }
100
101
    /**
102
     * Ensure a \DateTimeZone instance is returned according to the provided $spec
103
     *
104
     * @param string $spec
105
     *
106
     * @dataProvider getCreateDateTimeZoneData
107
     *
108
     * @throws DateTimeZoneFactoryException
109
     */
110
    public function testCreateDateTimeZone(string $spec): void
111
    {
112
        $factory = new DateTimeZoneFactory();
113
114
        $dateTimeZone = $factory->createDateTimeZone($spec);
115
116
        $this->assertSame($spec, $dateTimeZone->getName());
117
    }
118
119
    /**
120
     * @see https://www.php.net/manual/en/timezones.europe.php
121
     *
122
     * @return array<int, mixed>
123
     */
124
    public function getCreateDateTimeZoneData(): array
125
    {
126
        return [
127
            [
128
                'UTC',
129
            ],
130
            [
131
                'Europe/London',
132
            ],
133
            [
134
                'Europe/Amsterdam',
135
            ],
136
            [
137
                'Europe/Rome',
138
            ],
139
            [
140
                'Atlantic/Bermuda',
141
            ],
142
            [
143
                'Atlantic/Azores',
144
            ],
145
            [
146
                'Antarctica/DumontDUrville',
147
            ],
148
        ];
149
    }
150
}
151