forked from symfony/symfony
/
DateTimeToLocalizedStringTransformerTest.php
383 lines (298 loc) · 15.3 KB
/
DateTimeToLocalizedStringTransformerTest.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer;
use Symfony\Component\Form\Tests\Extension\Core\DataTransformer\Traits\DateTimeEqualsTrait;
use Symfony\Component\Intl\Util\IntlTestHelper;
class DateTimeToLocalizedStringTransformerTest extends TestCase
{
use DateTimeEqualsTrait;
protected $dateTime;
protected $dateTimeWithoutSeconds;
protected function setUp()
{
parent::setUp();
// Normalize intl. configuration settings.
if (\extension_loaded('intl')) {
$this->iniSet('intl.use_exceptions', 0);
$this->iniSet('intl.error_level', 0);
}
// Since we test against "de_AT", we need the full implementation
IntlTestHelper::requireFullIntl($this, '57.1');
\Locale::setDefault('de_AT');
$this->dateTime = new \DateTime('2010-02-03 04:05:06 UTC');
$this->dateTimeWithoutSeconds = new \DateTime('2010-02-03 04:05:00 UTC');
}
protected function tearDown()
{
$this->dateTime = null;
$this->dateTimeWithoutSeconds = null;
}
public function dataProvider()
{
return [
[\IntlDateFormatter::SHORT, null, null, '03.02.10, 04:05', '2010-02-03 04:05:00 UTC'],
[\IntlDateFormatter::MEDIUM, null, null, '03.02.2010, 04:05', '2010-02-03 04:05:00 UTC'],
[\IntlDateFormatter::LONG, null, null, '3. Februar 2010 um 04:05', '2010-02-03 04:05:00 UTC'],
[\IntlDateFormatter::FULL, null, null, 'Mittwoch, 3. Februar 2010 um 04:05', '2010-02-03 04:05:00 UTC'],
[\IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, null, '03.02.10', '2010-02-03 00:00:00 UTC'],
[\IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE, null, '03.02.2010', '2010-02-03 00:00:00 UTC'],
[\IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, '3. Februar 2010', '2010-02-03 00:00:00 UTC'],
[\IntlDateFormatter::FULL, \IntlDateFormatter::NONE, null, 'Mittwoch, 3. Februar 2010', '2010-02-03 00:00:00 UTC'],
[null, \IntlDateFormatter::SHORT, null, '03.02.2010, 04:05', '2010-02-03 04:05:00 UTC'],
[null, \IntlDateFormatter::MEDIUM, null, '03.02.2010, 04:05:06', '2010-02-03 04:05:06 UTC'],
[null, \IntlDateFormatter::LONG, null, '03.02.2010, 04:05:06 UTC', '2010-02-03 04:05:06 UTC'],
[null, \IntlDateFormatter::LONG, null, '03.02.2010, 04:05:06 UTC', '2010-02-03 04:05:06 GMT'],
// see below for extra test case for time format FULL
[\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT, null, '04:05', '1970-01-01 04:05:00 UTC'],
[\IntlDateFormatter::NONE, \IntlDateFormatter::MEDIUM, null, '04:05:06', '1970-01-01 04:05:06 UTC'],
[\IntlDateFormatter::NONE, \IntlDateFormatter::LONG, null, '04:05:06 UTC', '1970-01-01 04:05:06 GMT'],
[\IntlDateFormatter::NONE, \IntlDateFormatter::LONG, null, '04:05:06 UTC', '1970-01-01 04:05:06 UTC'],
[null, null, 'yyyy-MM-dd HH:mm:00', '2010-02-03 04:05:00', '2010-02-03 04:05:00 UTC'],
[null, null, 'yyyy-MM-dd HH:mm', '2010-02-03 04:05', '2010-02-03 04:05:00 UTC'],
[null, null, 'yyyy-MM-dd HH', '2010-02-03 04', '2010-02-03 04:00:00 UTC'],
[null, null, 'yyyy-MM-dd', '2010-02-03', '2010-02-03 00:00:00 UTC'],
[null, null, 'yyyy-MM', '2010-02', '2010-02-01 00:00:00 UTC'],
[null, null, 'yyyy', '2010', '2010-01-01 00:00:00 UTC'],
[null, null, 'dd-MM-yyyy', '03-02-2010', '2010-02-03 00:00:00 UTC'],
[null, null, 'HH:mm:ss', '04:05:06', '1970-01-01 04:05:06 UTC'],
[null, null, 'HH:mm:00', '04:05:00', '1970-01-01 04:05:00 UTC'],
[null, null, 'HH:mm', '04:05', '1970-01-01 04:05:00 UTC'],
[null, null, 'HH', '04', '1970-01-01 04:00:00 UTC'],
];
}
/**
* @dataProvider dataProvider
*/
public function testTransform($dateFormat, $timeFormat, $pattern, $output, $input)
{
IntlTestHelper::requireFullIntl($this, '59.1');
\Locale::setDefault('de_AT');
$transformer = new DateTimeToLocalizedStringTransformer(
'UTC',
'UTC',
$dateFormat,
$timeFormat,
\IntlDateFormatter::GREGORIAN,
$pattern
);
$input = new \DateTime($input);
$this->assertEquals($output, $transformer->transform($input));
}
public function testTransformFullTime()
{
IntlTestHelper::requireFullIntl($this, '59.1');
\Locale::setDefault('de_AT');
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter::FULL);
$this->assertEquals('03.02.2010, 04:05:06 Koordinierte Weltzeit', $transformer->transform($this->dateTime));
}
public function testTransformToDifferentLocale()
{
\Locale::setDefault('en_US');
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
$this->assertEquals('Feb 3, 2010, 4:05 AM', $transformer->transform($this->dateTime));
}
public function testTransformEmpty()
{
$transformer = new DateTimeToLocalizedStringTransformer();
$this->assertSame('', $transformer->transform(null));
}
public function testTransformWithDifferentTimezones()
{
$transformer = new DateTimeToLocalizedStringTransformer('America/New_York', 'Asia/Hong_Kong');
$input = new \DateTime('2010-02-03 04:05:06 America/New_York');
$dateTime = clone $input;
$dateTime->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
$this->assertEquals($dateTime->format('d.m.Y, H:i'), $transformer->transform($input));
}
public function testReverseTransformWithNoConstructorParameters()
{
$tz = date_default_timezone_get();
date_default_timezone_set('Europe/Rome');
$transformer = new DateTimeToLocalizedStringTransformer();
$dateTime = new \DateTime('2010-02-03 04:05');
$this->assertDateTimeEquals(
$dateTime->format('c'),
$transformer->reverseTransform('03.02.2010, 04:05')->format('c')
);
date_default_timezone_set($tz);
}
public function testTransformWithDifferentPatterns()
{
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'MM*yyyy*dd HH|mm|ss');
$this->assertEquals('02*2010*03 04|05|06', $transformer->transform($this->dateTime));
}
public function testTransformDateTimeImmutableTimezones()
{
$transformer = new DateTimeToLocalizedStringTransformer('America/New_York', 'Asia/Hong_Kong');
$input = new \DateTimeImmutable('2010-02-03 04:05:06 America/New_York');
$dateTime = clone $input;
$dateTime = $dateTime->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
$this->assertEquals($dateTime->format('d.m.Y, H:i'), $transformer->transform($input));
}
public function testTransformRequiresValidDateTime()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->transform('2010-01-01');
}
public function testTransformWrapsIntlErrors()
{
$this->markTestIncomplete('Checking for intl errors needs to be reimplemented');
$transformer = new DateTimeToLocalizedStringTransformer();
// HOW TO REPRODUCE?
//$this->expectException('Symfony\Component\Form\Extension\Core\DataTransformer\TransformationFailedException');
//$transformer->transform(1.5);
}
/**
* @dataProvider dataProvider
*/
public function testReverseTransform($dateFormat, $timeFormat, $pattern, $input, $output)
{
$transformer = new DateTimeToLocalizedStringTransformer(
'UTC',
'UTC',
$dateFormat,
$timeFormat,
\IntlDateFormatter::GREGORIAN,
$pattern
);
$output = new \DateTime($output);
$this->assertDateTimeEquals($output, $transformer->reverseTransform($input));
}
public function testReverseTransformFullTime()
{
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter::FULL);
$this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010, 04:05:06 GMT+00:00'));
}
public function testReverseTransformFromDifferentLocale()
{
\Locale::setDefault('en_US');
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('Feb 3, 2010, 04:05 AM'));
}
public function testReverseTransformWithDifferentTimezones()
{
$transformer = new DateTimeToLocalizedStringTransformer('America/New_York', 'Asia/Hong_Kong');
$dateTime = new \DateTime('2010-02-03 04:05:00 Asia/Hong_Kong');
$dateTime->setTimezone(new \DateTimeZone('America/New_York'));
$this->assertDateTimeEquals($dateTime, $transformer->reverseTransform('03.02.2010, 04:05'));
}
public function testReverseTransformOnlyDateWithDifferentTimezones()
{
$transformer = new DateTimeToLocalizedStringTransformer('Europe/Berlin', 'Pacific/Tahiti', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
$dateTime = new \DateTime('2017-01-10 11:00', new \DateTimeZone('Europe/Berlin'));
$this->assertDateTimeEquals($dateTime, $transformer->reverseTransform('2017-01-10'));
}
public function testReverseTransformWithDifferentPatterns()
{
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'MM*yyyy*dd HH|mm|ss');
$this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('02*2010*03 04|05|06'));
}
public function testReverseTransformDateOnlyWithDstIssue()
{
$transformer = new DateTimeToLocalizedStringTransformer('Europe/Rome', 'Europe/Rome', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, 'dd/MM/yyyy');
$this->assertDateTimeEquals(
new \DateTime('1978-05-28', new \DateTimeZone('Europe/Rome')),
$transformer->reverseTransform('28/05/1978')
);
}
public function testReverseTransformDateOnlyWithDstIssueAndEscapedText()
{
$transformer = new DateTimeToLocalizedStringTransformer('Europe/Rome', 'Europe/Rome', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, \IntlDateFormatter::GREGORIAN, "'day': dd 'month': MM 'year': yyyy");
$this->assertDateTimeEquals(
new \DateTime('1978-05-28', new \DateTimeZone('Europe/Rome')),
$transformer->reverseTransform('day: 28 month: 05 year: 1978')
);
}
public function testReverseTransformEmpty()
{
$transformer = new DateTimeToLocalizedStringTransformer();
$this->assertNull($transformer->reverseTransform(''));
}
public function testReverseTransformRequiresString()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform(12345);
}
public function testReverseTransformWrapsIntlErrors()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
}
public function testValidateDateFormatOption()
{
$this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
new DateTimeToLocalizedStringTransformer(null, null, 'foobar');
}
public function testValidateTimeFormatOption()
{
$this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
new DateTimeToLocalizedStringTransformer(null, null, null, 'foobar');
}
public function testReverseTransformWithNonExistingDate()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter::SHORT);
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('31.04.10 04:05'));
}
public function testReverseTransformOutOfTimestampRange()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
$transformer->reverseTransform('1789-07-14');
}
public function testReverseTransformFiveDigitYears()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
$transformer->reverseTransform('20107-03-21');
}
public function testReverseTransformFiveDigitYearsWithTimestamp()
{
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss');
$transformer->reverseTransform('20107-03-21 12:34:56');
}
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}
$this->iniSet('intl.error_level', E_WARNING);
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
}
public function testReverseTransformWrapsIntlErrorsWithExceptions()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}
$this->iniSet('intl.use_exceptions', 1);
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
}
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}
$this->iniSet('intl.use_exceptions', 1);
$this->iniSet('intl.error_level', E_WARNING);
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
$transformer = new DateTimeToLocalizedStringTransformer();
$transformer->reverseTransform('12345');
}
}