1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\DateTime; |
6
|
|
|
|
7
|
|
|
use Arp\DateTime\DateTimeFactoryInterface; |
8
|
|
|
use Arp\DateTime\DateTimeImmutableFactory; |
9
|
|
|
use Arp\DateTime\Exception\DateTimeFactoryException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers \Arp\DateTime\DateTimeImmutableFactory |
14
|
|
|
*/ |
15
|
|
|
final class DateTimeImmutableFactoryTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Assert the class implements DateTimeFactoryInterface |
19
|
|
|
*/ |
20
|
|
|
public function testImplementsDateTimeFactoryInterface(): void |
21
|
|
|
{ |
22
|
|
|
$factory = new DateTimeImmutableFactory(); |
23
|
|
|
|
24
|
|
|
$this->assertInstanceOf(DateTimeFactoryInterface::class, $factory); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Assert that an exception will be thrown if the provided $dateTimeClassName option is a class that does not |
29
|
|
|
* implement \DateTimeInterface |
30
|
|
|
*/ |
31
|
|
|
public function testCreateDateTimeWillThrowDateTimeFactoryExceptionIfDateTimeClassNameIsNotDateTimeImmutable(): void |
32
|
|
|
{ |
33
|
|
|
$this->expectException(DateTimeFactoryException::class); |
34
|
|
|
$this->expectExceptionMessage( |
35
|
|
|
sprintf( |
36
|
|
|
'The \'dateTimeClassName\' parameter must be a class name that implements \'%s\'', |
37
|
|
|
\DateTimeImmutable::class |
38
|
|
|
) |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
new DateTimeImmutableFactory(\DateTime::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Assert that calls to createDateTime() will return a DateTimeImmutable instance, matching the |
46
|
|
|
* provided $spec and $timeZone |
47
|
|
|
* |
48
|
|
|
* @dataProvider getCreatDateTimeWillReturnDateTimeImmutableData |
49
|
|
|
* |
50
|
|
|
* @throws DateTimeFactoryException |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
|
|
public function testCreatDateTimeWillReturnDateTimeImmutable( |
54
|
|
|
?string $spec, |
55
|
|
|
string|\DateTimeZone|null $timeZone = null |
56
|
|
|
): void { |
57
|
|
|
$factory = new DateTimeImmutableFactory(); |
58
|
|
|
|
59
|
|
|
$immutableDateTime = $factory->createDateTime($spec, $timeZone); |
60
|
|
|
|
61
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $immutableDateTime); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array<mixed> |
66
|
|
|
*/ |
67
|
|
|
public function getCreatDateTimeWillReturnDateTimeImmutableData(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
[ |
71
|
|
|
null, |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
'now', |
75
|
|
|
'UTC', |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
'2021-05-01', |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @dataProvider getCreatFromFormatWillReturnDateTimeImmutableData |
85
|
|
|
* |
86
|
|
|
* @throws DateTimeFactoryException |
87
|
|
|
*/ |
88
|
|
|
public function testCreatFromFormatWillReturnDateTimeImmutable( |
89
|
|
|
string $format, |
90
|
|
|
string $spec, |
91
|
|
|
string|\DateTimeZone|null $timeZone = null |
92
|
|
|
): void { |
93
|
|
|
$factory = new DateTimeImmutableFactory(); |
94
|
|
|
|
95
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $factory->createFromFormat($format, $spec, $timeZone)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array<mixed> |
100
|
|
|
*/ |
101
|
|
|
public function getCreatFromFormatWillReturnDateTimeImmutableData(): array |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
[ |
105
|
|
|
'Y/m/d H:i:s', |
106
|
|
|
'2021/05/03 14:36:47', |
107
|
|
|
], |
108
|
|
|
[ |
109
|
|
|
'Y-d-m H:i:s', |
110
|
|
|
'1999-01-12 11:06:01', |
111
|
|
|
'UTC', |
112
|
|
|
], |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|