|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArpTest\DateTime; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\DateTime\DateTimeFactory; |
|
8
|
|
|
use Arp\DateTime\DateTimeFactoryInterface; |
|
9
|
|
|
use Arp\DateTime\DateTimeZoneFactoryInterface; |
|
10
|
|
|
use Arp\DateTime\Exception\DateTimeFactoryException; |
|
11
|
|
|
use Arp\DateTime\Exception\DateTimeZoneFactoryException; |
|
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @covers \Arp\DateTime\DateTimeFactory |
|
17
|
|
|
* |
|
18
|
|
|
* @author Alex Patterson <[email protected]> |
|
19
|
|
|
* @package ArpTest\DateTime |
|
20
|
|
|
*/ |
|
21
|
|
|
final class DateTimeFactoryTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var DateTimeZoneFactoryInterface&MockObject |
|
25
|
|
|
*/ |
|
26
|
|
|
private $dateTimeZoneFactory; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Prepare the test case dependencies |
|
30
|
|
|
*/ |
|
31
|
|
|
public function setUp(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->dateTimeZoneFactory = $this->createMock(DateTimeZoneFactoryInterface::class); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Ensure that the factory implements DateTimeFactoryInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testImplementsDateTimeFactoryInterface(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$factory = new DateTimeFactory(); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertInstanceOf(DateTimeFactoryInterface::class, $factory); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Assert that a DateTimeFactoryException is thrown if trying to create the class without a valid |
|
48
|
|
|
* $dateTimeClassName constructor argument |
|
49
|
|
|
* |
|
50
|
|
|
* @throws DateTimeFactoryException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testDateTimeFactoryExceptionIsThrownWhenProvidingInvalidDateTimeClassName(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$dateTimeClassName = \stdClass::class; |
|
55
|
|
|
|
|
56
|
|
|
$this->expectException(DateTimeFactoryException::class); |
|
57
|
|
|
$this->expectExceptionMessage( |
|
58
|
|
|
sprintf( |
|
59
|
|
|
'The \'dateTimeClassName\' parameter must be a class name that implements \'%s\'', |
|
60
|
|
|
\DateTimeInterface::class |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
new DateTimeFactory($this->dateTimeZoneFactory, $dateTimeClassName); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Ensure that calls to createDateTime() will return the valid configured \DateTime instance. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string|null $spec The date and time specification. |
|
71
|
|
|
* @param \DateTimeZone|string|null $timeZone The optional date time zone to test. |
|
72
|
|
|
* |
|
73
|
|
|
* @dataProvider getCreateDateTimeData |
|
74
|
|
|
* |
|
75
|
|
|
* @throws DateTimeFactoryException |
|
76
|
|
|
* @throws \Exception |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testCreateDateTime(?string $spec, $timeZone = null): void |
|
79
|
|
|
{ |
|
80
|
|
|
$factory = new DateTimeFactory(); |
|
81
|
|
|
|
|
82
|
|
|
$expectedDateTime = new \DateTime( |
|
83
|
|
|
$spec ?? 'now', |
|
84
|
|
|
is_string($timeZone) ? new \DateTimeZone($timeZone) : $timeZone |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
$dateTime = $factory->createDateTime($spec, $timeZone); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertDateTime($expectedDateTime, $dateTime); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array<array> |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getCreateDateTimeData(): array |
|
96
|
|
|
{ |
|
97
|
|
|
return [ |
|
98
|
|
|
[ |
|
99
|
|
|
null, |
|
100
|
|
|
], |
|
101
|
|
|
|
|
102
|
|
|
[ |
|
103
|
|
|
'now', |
|
104
|
|
|
], |
|
105
|
|
|
|
|
106
|
|
|
[ |
|
107
|
|
|
'now', |
|
108
|
|
|
'Europe/London', |
|
109
|
|
|
], |
|
110
|
|
|
|
|
111
|
|
|
[ |
|
112
|
|
|
'2019-05-14 12:33:00', |
|
113
|
|
|
], |
|
114
|
|
|
|
|
115
|
|
|
// [ |
|
116
|
|
|
// '2019-08-14 17:34:55', |
|
117
|
|
|
// 'UTC', |
|
118
|
|
|
// ], |
|
119
|
|
|
|
|
120
|
|
|
// [ |
|
121
|
|
|
// '2020-08-22 14:43:12', |
|
122
|
|
|
// null, |
|
123
|
|
|
// ], |
|
124
|
|
|
// |
|
125
|
|
|
// [ |
|
126
|
|
|
// '2020-08-22 14:44:37', |
|
127
|
|
|
// new \DateTimeZone('Europe/London'), |
|
128
|
|
|
// ], |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Ensure that if the DateTime cannot be created because the provided $spec is invalid, a new |
|
134
|
|
|
* DateTimeFactoryException will be thrown. |
|
135
|
|
|
* |
|
136
|
|
|
* @throws DateTimeFactoryException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function testCreateDateTimeWillThrowDateTimeFactoryExceptionForInvalidDateTimeSpec(): void |
|
139
|
|
|
{ |
|
140
|
|
|
$factory = new DateTimeFactory($this->dateTimeZoneFactory); |
|
141
|
|
|
|
|
142
|
|
|
$spec = 'foo'; // invalid argument |
|
143
|
|
|
|
|
144
|
|
|
$exceptionMessage = sprintf( |
|
145
|
|
|
'DateTime::__construct(): Failed to parse time string (%s) at position 0 (%s)', |
|
146
|
|
|
$spec, |
|
147
|
|
|
$spec[0] |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
$this->expectException(DateTimeFactoryException::class); |
|
151
|
|
|
$this->expectExceptionMessage( |
|
152
|
|
|
sprintf( |
|
153
|
|
|
'Failed to create a valid \DateTime instance using \'%s\': %s', |
|
154
|
|
|
$spec, |
|
155
|
|
|
$exceptionMessage |
|
156
|
|
|
) |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
$factory->createDateTime($spec); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Assert that a DateTimeFactoryException will be thrown when providing a invalid \DateTimeZone object to |
|
164
|
|
|
* createFromFormat() |
|
165
|
|
|
* |
|
166
|
|
|
* @throws DateTimeFactoryException |
|
167
|
|
|
*/ |
|
168
|
|
|
public function testCreateFromFormatWillCatchAndReThrowException(): void |
|
169
|
|
|
{ |
|
170
|
|
|
$factory = new DateTimeFactory($this->dateTimeZoneFactory); |
|
171
|
|
|
|
|
172
|
|
|
$spec = '2021-05-01 23:38:12'; |
|
173
|
|
|
$format = 'Y-m-d H:i:s'; |
|
174
|
|
|
$timeZone = 'UTC'; |
|
175
|
|
|
|
|
176
|
|
|
$exceptionMessage = 'This is a test exception'; |
|
177
|
|
|
$exceptionCode = 123; |
|
178
|
|
|
$exception = new DateTimeZoneFactoryException($exceptionMessage, $exceptionCode); |
|
179
|
|
|
|
|
180
|
|
|
$this->dateTimeZoneFactory->expects($this->once()) |
|
181
|
|
|
->method('createDateTimeZone') |
|
182
|
|
|
->with($timeZone) |
|
183
|
|
|
->willThrowException($exception); |
|
184
|
|
|
|
|
185
|
|
|
$this->expectException(DateTimeFactoryException::class); |
|
186
|
|
|
$this->expectExceptionCode($exceptionCode); |
|
187
|
|
|
$this->expectExceptionMessage( |
|
188
|
|
|
sprintf('Failed to create date time zone: %s', $exceptionMessage), |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
$factory->createFromFormat($format, $spec, $timeZone); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Assert that a DateTimeFactoryException will be thrown if providing an invalid $spec |
|
196
|
|
|
* argument to createFromFormat(). |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $spec |
|
199
|
|
|
* @param string $format |
|
200
|
|
|
* @param \DateTimeZone|string|null $timeZone |
|
201
|
|
|
* |
|
202
|
|
|
* @dataProvider getCreateFromFormatWillThrowDateTimeFactoryExceptionForInvalidDateTimeData |
|
203
|
|
|
* |
|
204
|
|
|
* @throws DateTimeFactoryException |
|
205
|
|
|
*/ |
|
206
|
|
|
public function testCreateFromFormatWillThrowDateTimeFactoryExceptionForInvalidDateTimeSpec( |
|
207
|
|
|
string $spec, |
|
208
|
|
|
string $format, |
|
209
|
|
|
$timeZone = null |
|
210
|
|
|
): void { |
|
211
|
|
|
$factory = new DateTimeFactory($this->dateTimeZoneFactory); |
|
212
|
|
|
|
|
213
|
|
|
$this->expectException(DateTimeFactoryException::class); |
|
214
|
|
|
$this->expectExceptionMessage( |
|
215
|
|
|
sprintf( |
|
216
|
|
|
'Failed to create a valid \DateTime instance using \'%s\' and format \'%s\'', |
|
217
|
|
|
$spec, |
|
218
|
|
|
$format |
|
219
|
|
|
) |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
$factory->createFromFormat($format, $spec, $timeZone); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @return array<array> |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getCreateFromFormatWillThrowDateTimeFactoryExceptionForInvalidDateTimeData(): array |
|
229
|
|
|
{ |
|
230
|
|
|
return [ |
|
231
|
|
|
[ |
|
232
|
|
|
'test', |
|
233
|
|
|
'Y-m-d', |
|
234
|
|
|
], |
|
235
|
|
|
]; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Assert that a DateTimeFactoryException will be thrown when providing a invalid \DateTimeZone object to |
|
240
|
|
|
* createDateTime() |
|
241
|
|
|
* |
|
242
|
|
|
* @throws DateTimeFactoryException |
|
243
|
|
|
*/ |
|
244
|
|
|
public function testCreateDateTimeWillThrowDateTimeFactoryExceptionForInvalidDateTimeZone(): void |
|
245
|
|
|
{ |
|
246
|
|
|
$factory = new DateTimeFactory($this->dateTimeZoneFactory); |
|
247
|
|
|
|
|
248
|
|
|
$spec = 'now'; |
|
249
|
|
|
$timeZone = 'UTC'; |
|
250
|
|
|
|
|
251
|
|
|
$exceptionMessage = 'This is a test exception'; |
|
252
|
|
|
$exceptionCode = 123; |
|
253
|
|
|
$exception = new DateTimeZoneFactoryException($exceptionMessage, $exceptionCode); |
|
254
|
|
|
|
|
255
|
|
|
$this->dateTimeZoneFactory->expects($this->once()) |
|
256
|
|
|
->method('createDateTimeZone') |
|
257
|
|
|
->with($timeZone) |
|
258
|
|
|
->willThrowException($exception); |
|
259
|
|
|
|
|
260
|
|
|
$this->expectException(DateTimeFactoryException::class); |
|
261
|
|
|
$this->expectExceptionCode($exceptionCode); |
|
262
|
|
|
$this->expectExceptionMessage( |
|
263
|
|
|
sprintf('Failed to create date time zone: %s', $exceptionMessage), |
|
264
|
|
|
); |
|
265
|
|
|
|
|
266
|
|
|
$factory->createDateTime($spec, $timeZone); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Ensure that a \DateTime instance can be created from the provided format |
|
271
|
|
|
* |
|
272
|
|
|
* @param string $spec |
|
273
|
|
|
* @param string $format |
|
274
|
|
|
* @param string|\DateTimeZone|null $timeZone |
|
275
|
|
|
* |
|
276
|
|
|
* @dataProvider getCreateFromFormatData |
|
277
|
|
|
* |
|
278
|
|
|
* @throws DateTimeFactoryException |
|
279
|
|
|
*/ |
|
280
|
|
|
public function testCreateFromFormat(string $spec, string $format, $timeZone = null): void |
|
281
|
|
|
{ |
|
282
|
|
|
$factory = new DateTimeFactory($this->dateTimeZoneFactory); |
|
283
|
|
|
|
|
284
|
|
|
$dateTimeZone = is_string($timeZone) ? new \DateTimeZone($timeZone) : $timeZone; |
|
285
|
|
|
|
|
286
|
|
|
/** @var \DateTimeInterface $expectedDateTime */ |
|
287
|
|
|
$expectedDateTime = \DateTime::createFromFormat( |
|
288
|
|
|
$format, |
|
289
|
|
|
$spec ?? 'now', |
|
290
|
|
|
$dateTimeZone |
|
291
|
|
|
); |
|
292
|
|
|
|
|
293
|
|
|
$this->assertDateTime($expectedDateTime, $factory->createFromFormat($spec, $format, $timeZone)); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @see https://www.php.net/manual/en/timezones.europe.php |
|
298
|
|
|
* |
|
299
|
|
|
* @return array<array> |
|
300
|
|
|
*/ |
|
301
|
|
|
public function getCreateFromFormatData(): array |
|
302
|
|
|
{ |
|
303
|
|
|
return [ |
|
304
|
|
|
[ |
|
305
|
|
|
'2019-04-01', |
|
306
|
|
|
'Y-m-d', |
|
307
|
|
|
], |
|
308
|
|
|
// [ |
|
309
|
|
|
// '1976/01/14', |
|
310
|
|
|
// 'Y/m/d', |
|
311
|
|
|
// ], |
|
312
|
|
|
// [ |
|
313
|
|
|
// '2019-08-14 17:34:55', |
|
314
|
|
|
// 'Y-m-d H:i:s', |
|
315
|
|
|
// 'UTC', |
|
316
|
|
|
// ], |
|
317
|
|
|
// [ |
|
318
|
|
|
// '2010-10-26 11:19:32', |
|
319
|
|
|
// 'Y-m-d H:i:s', |
|
320
|
|
|
// 'Europe/London', |
|
321
|
|
|
// ], |
|
322
|
|
|
]; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @param \DateTimeInterface $expectedDateTime |
|
327
|
|
|
* @param \DateTimeInterface $dateTime |
|
328
|
|
|
*/ |
|
329
|
|
|
private function assertDateTime(\DateTimeInterface $expectedDateTime, \DateTimeInterface $dateTime): void |
|
330
|
|
|
{ |
|
331
|
|
|
$this->assertSame($expectedDateTime->format('Y'), $dateTime->format('Y'), 'Years do not match'); |
|
332
|
|
|
$this->assertSame($expectedDateTime->format('M'), $dateTime->format('M'), 'Months do not match'); |
|
333
|
|
|
$this->assertSame($expectedDateTime->format('d'), $dateTime->format('d'), 'Days do not match'); |
|
334
|
|
|
$this->assertSame($expectedDateTime->format('H'), $dateTime->format('H'), 'Hours do not match'); |
|
335
|
|
|
$this->assertSame($expectedDateTime->format('i'), $dateTime->format('i'), 'Minuets do not match'); |
|
336
|
|
|
$this->assertSame($expectedDateTime->format('s'), $dateTime->format('s'), 'Seconds do not match'); |
|
337
|
|
|
$this->assertSame($expectedDateTime->format('f'), $dateTime->format('f'), 'Milliseconds do not match'); |
|
338
|
|
|
|
|
339
|
|
|
$this->assertSame( |
|
340
|
|
|
$expectedDateTime->getTimezone()->getName(), |
|
341
|
|
|
$dateTime->getTimezone()->getName(), |
|
342
|
|
|
'Timezones do not match' |
|
343
|
|
|
); |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|