DateTimeZoneFactoryTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 35
c 2
b 0
f 0
dl 0
loc 119
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDateTimeZoneFactoryExceptionIsThrownWhenProvidingInvalidDateTimeZoneClassName() 0 13 1
A testCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalid() 0 18 2
A getCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalidData() 0 8 1
A testImplementsDateTimeZoneFactoryInterface() 0 5 1
A testCreateDateTimeZone() 0 7 1
A getCreateDateTimeZoneData() 0 23 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
final class DateTimeZoneFactoryTest extends TestCase
16
{
17
    /**
18
     * Assert the factory implement DateTimeZoneFactoryInterface
19
     */
20
    public function testImplementsDateTimeZoneFactoryInterface(): void
21
    {
22
        $factory = new DateTimeZoneFactory();
23
24
        $this->assertInstanceOf(DateTimeZoneFactoryInterface::class, $factory);
25
    }
26
27
    /**
28
     * Assert that a DateTimeZoneFactoryException is thrown if trying to create the class without a valid
29
     * $dateTimeZoneClassName constructor argument
30
     *
31
     * @throws DateTimeZoneFactoryException
32
     */
33
    public function testDateTimeZoneFactoryExceptionIsThrownWhenProvidingInvalidDateTimeZoneClassName(): void
34
    {
35
        $dateTimeZoneClassName = \stdClass::class;
36
37
        $this->expectException(DateTimeZoneFactoryException::class);
38
        $this->expectExceptionMessage(
39
            sprintf(
40
                'The \'dateTimeZoneClassName\' parameter must be a class name that implements \'%s\'',
41
                \DateTimeZone::class
42
            )
43
        );
44
45
        new DateTimeZoneFactory($dateTimeZoneClassName);
46
    }
47
48
    /**
49
     * Assert that if providing an invalid $spec to createDateTimeZone() a DateTimeZoneFactoryException is thrown
50
     *
51
     * @dataProvider getCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalidData
52
     *
53
     * @throws DateTimeZoneFactoryException
54
     */
55
    public function testCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalid(string $spec): void
56
    {
57
        $factory = new DateTimeZoneFactory();
58
59
        $exceptionCode = 123;
60
        try {
61
            new \DateTimeZone($spec);
62
        } catch (\Exception $e) {
63
            $exceptionCode = $e->getCode();
64
        }
65
66
        $this->expectException(DateTimeZoneFactoryException::class);
67
        $this->expectExceptionCode($exceptionCode);
68
        $this->expectExceptionMessage(
69
            sprintf('Failed to create a valid \DateTimeZone instance using \'%s\'', $spec)
70
        );
71
72
        $factory->createDateTimeZone($spec);
73
    }
74
75
    /**
76
     * @return array<int, mixed>
77
     */
78
    public function getCreateDateTimeZoneWillThrowDateTimeZoneFactoryExceptionIfSpecIsInvalidData(): array
79
    {
80
        return [
81
            [
82
                'foo',
83
            ],
84
            [
85
                '123',
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * Ensure a \DateTimeZone instance is returned according to the provided $spec
92
     *
93
     * @dataProvider getCreateDateTimeZoneData
94
     *
95
     * @throws DateTimeZoneFactoryException
96
     */
97
    public function testCreateDateTimeZone(string $spec): void
98
    {
99
        $factory = new DateTimeZoneFactory();
100
101
        $dateTimeZone = $factory->createDateTimeZone($spec);
102
103
        $this->assertSame($spec, $dateTimeZone->getName());
104
    }
105
106
    /**
107
     * @see https://www.php.net/manual/en/timezones.europe.php
108
     *
109
     * @return array<int, mixed>
110
     */
111
    public function getCreateDateTimeZoneData(): array
112
    {
113
        return [
114
            [
115
                'UTC',
116
            ],
117
            [
118
                'Europe/London',
119
            ],
120
            [
121
                'Europe/Amsterdam',
122
            ],
123
            [
124
                'Europe/Rome',
125
            ],
126
            [
127
                'Atlantic/Bermuda',
128
            ],
129
            [
130
                'Atlantic/Azores',
131
            ],
132
            [
133
                'Antarctica/DumontDUrville',
134
            ],
135
        ];
136
    }
137
}
138