-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathTimeZoneTest.php
More file actions
101 lines (86 loc) · 2.77 KB
/
TimeZoneTest.php
File metadata and controls
101 lines (86 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace Brick\DateTime\Tests;
use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\TimeZone;
use Brick\DateTime\TimeZoneOffset;
use Brick\DateTime\TimeZoneRegion;
use DateTimeZone;
use PHPUnit\Framework\Attributes\DataProvider;
use const PHP_VERSION_ID;
/**
* Unit tests for class TimeZone.
*/
class TimeZoneTest extends AbstractTestCase
{
/**
* @param string $text The text to parse.
* @param string $class The expected class name.
* @param string $id The expected id.
*/
#[DataProvider('providerParse')]
public function testParse(string $text, string $class, string $id): void
{
$timeZone = TimeZone::parse($text);
self::assertInstanceOf($class, $timeZone);
self::assertSame($id, $timeZone->getId());
}
public static function providerParse(): iterable
{
yield from [
['Z', TimeZoneOffset::class, 'Z'],
['z', TimeZoneOffset::class, 'Z'],
['+01:00', TimeZoneOffset::class, '+01:00'],
['-02:30', TimeZoneOffset::class, '-02:30'],
['Europe/London', TimeZoneRegion::class, 'Europe/London'],
['America/Los_Angeles', TimeZoneRegion::class, 'America/Los_Angeles'],
];
if (PHP_VERSION_ID >= 80107) {
yield ['-02:30:30', TimeZoneOffset::class, '-02:30:30'];
}
}
#[DataProvider('providerParseInvalidStringThrowsException')]
public function testParseInvalidStringThrowsException(string $text): void
{
$this->expectException(DateTimeParseException::class);
TimeZone::parse($text);
}
public static function providerParseInvalidStringThrowsException(): array
{
return [
[''],
['+'],
['-'],
];
}
public function testUtc(): void
{
$this->assertTimeZoneOffsetIs(0, TimeZoneOffset::utc());
}
public function testIsEqualTo(): void
{
self::assertTrue(TimeZoneOffset::utc()->isEqualTo(TimeZoneOffset::ofTotalSeconds(0)));
self::assertFalse(TimeZoneOffset::utc()->isEqualTo(TimeZoneOffset::ofTotalSeconds(3600)));
}
/**
* @param string $tz The time-zone name.
*/
#[DataProvider('providerFromNativeDateTimeZone')]
public function testFromNativeDateTimeZone(string $tz): void
{
$dateTimeZone = new DateTimeZone($tz);
self::assertSame($tz, TimeZone::fromNativeDateTimeZone($dateTimeZone)->getId());
}
public static function providerFromNativeDateTimeZone(): iterable
{
yield from [
['Z'],
['+01:00'],
['Europe/London'],
['America/Los_Angeles'],
];
if (PHP_VERSION_ID >= 80107) {
yield ['-02:30:30'];
}
}
}