‪TYPO3CMS  ‪main
DateFormatterTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪DateFormatterTest extends UnitTestCase
27 {
28  public static function ‪formatDateProvider(): \Generator
29  {
30  yield 'regular formatting - no locale' => [
31  '2023.02.02 AD at 13:05:00 UTC',
32  "yyyy.MM.dd G 'at' HH:mm:ss zzz",
33  ];
34  yield 'full - no locale' => [
35  'Thursday, February 2, 2023 at 1:05:00 PM Coordinated Universal Time',
36  'FULL',
37  ];
38  yield 'full - locale C' => [
39  'Thursday, February 2, 2023 at 1:05:00 PM Coordinated Universal Time',
40  'FULL',
41  new ‪Locale('C'),
42  ];
43  yield 'long - no locale' => [
44  'February 2, 2023 at 1:05:00 PM UTC',
45  'LONG',
46  ];
47  yield 'long - locale C' => [
48  'February 2, 2023 at 1:05:00 PM UTC',
49  'LONG',
50  new ‪Locale('C'),
51  ];
52  yield 'medium - no locale' => [
53  'Feb 2, 2023, 1:05:00 PM',
54  'MEDIUM',
55  ];
56  yield 'medium - locale C' => [
57  'Feb 2, 2023, 1:05:00 PM',
58  'MEDIUM',
59  new ‪Locale('C'),
60  ];
61  yield 'medium with int - no locale' => [
62  'Feb 2, 2023, 1:05:00 PM',
63  \IntlDateFormatter::MEDIUM,
64  ];
65  yield 'medium with int - locale C' => [
66  'Feb 2, 2023, 1:05:00 PM',
67  \IntlDateFormatter::MEDIUM,
68  new ‪Locale('C'),
69  ];
70  yield 'medium with int as string - no locale' => [
71  'Feb 2, 2023, 1:05:00 PM',
72  (string)\IntlDateFormatter::MEDIUM,
73  ];
74  yield 'medium with int as string - locale C' => [
75  'Feb 2, 2023, 1:05:00 PM',
76  (string)\IntlDateFormatter::MEDIUM,
77  new ‪Locale('C'),
78  ];
79  yield 'short - no locale' => [
80  '2/2/23, 1:05 PM',
81  'SHORT',
82  ];
83  yield 'short - locale C' => [
84  '2/2/23, 1:05 PM',
85  'SHORT',
86  new ‪Locale('C'),
87  ];
88  yield 'short in lowercase - no locale' => [
89  '2/2/23, 1:05 PM',
90  'short',
91  ];
92  yield 'short in lowercase - locale C' => [
93  '2/2/23, 1:05 PM',
94  'short',
95  new ‪Locale('C'),
96  ];
97  yield 'regular formatting - en-US locale' => [
98  '2023.02.02 AD at 13:05:00 UTC',
99  "yyyy.MM.dd G 'at' HH:mm:ss zzz",
100  'en-US',
101  ];
102  yield 'full - en-US locale' => [
103  'Thursday, February 2, 2023 at 1:05:00 PM Coordinated Universal Time',
104  'FULL',
105  'en-US',
106  ];
107  yield 'long - en-US locale' => [
108  'February 2, 2023 at 1:05:00 PM UTC',
109  'LONG',
110  'en-US',
111  ];
112  yield 'medium - en-US locale' => [
113  'Feb 2, 2023, 1:05:00 PM',
114  'MEDIUM',
115  'en-US',
116  ];
117  yield 'short - en-US locale' => [
118  '2/2/23, 1:05 PM',
119  'SHORT',
120  'en-US',
121  ];
122  yield 'regular formatting - german locale' => [
123  '2023.02.02 n. Chr. um 13:05:00 UTC',
124  "yyyy.MM.dd G 'um' HH:mm:ss zzz",
125  'de-DE',
126  ];
127  yield 'full - german locale' => [
128  'Donnerstag, 2. Februar 2023 um 13:05:00 Koordinierte Weltzeit',
129  'FULL',
130  'de-DE',
131  ];
132  yield 'long - german locale' => [
133  '2. Februar 2023 um 13:05:00 UTC',
134  'LONG',
135  'de-DE',
136  ];
137  yield 'medium - german locale' => [
138  '02.02.2023, 13:05:00',
139  'MEDIUM',
140  'de-DE',
141  ];
142  yield 'short - german locale' => [
143  '02.02.23, 13:05',
144  'SHORT',
145  'de-DE',
146  ];
147  yield 'custom date only - german locale' => [
148  '02. Februar 2023',
149  'dd. MMMM yyyy',
150  'de-DE',
151  ];
152  yield 'custom time only - german locale' => [
153  '13:05:00',
154  'HH:mm:ss',
155  new ‪Locale('de'),
156  ];
157  }
158 
159  #[DataProvider('formatDateProvider')]
160  #[Test]
161  public function ‪formatFormatsCorrectly(string $expected, mixed $format, string|‪Locale|null $locale = 'C'): void
162  {
163  $input = new \DateTimeImmutable('2023-02-02 13:05:00');
164  $subject = new ‪DateFormatter();
165  self::assertEquals($expected, $subject->format($input, $format, $locale));
166  }
167 }
‪TYPO3\CMS\Core\Tests\Unit\Localization\DateFormatterTest\formatDateProvider
‪static formatDateProvider()
Definition: DateFormatterTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Localization\DateFormatterTest\formatFormatsCorrectly
‪formatFormatsCorrectly(string $expected, mixed $format, string|Locale|null $locale='C')
Definition: DateFormatterTest.php:161
‪TYPO3\CMS\Core\Localization\DateFormatter
Definition: DateFormatter.php:27
‪TYPO3\CMS\Core\Tests\Unit\Localization\DateFormatterTest
Definition: DateFormatterTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Localization
Definition: DateFormatterTest.php:18
‪TYPO3\CMS\Core\Localization\Locale
Definition: Locale.php:30