‪TYPO3CMS  ‪main
DateTimeConverterTest.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\Test;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
27 final class ‪DateTimeConverterTest extends FunctionalTestCase
28 {
29  protected bool ‪$initializeDatabase = false;
30 
31  #[Test]
33  {
34  $propertyMapper = $this->get(PropertyMapper::class);
35  $dateTime = $propertyMapper->convert(0, \DateTime::class);
36 
37  self::assertNull($dateTime);
38  $messages = $propertyMapper->getMessages();
39  self::assertTrue($messages->hasErrors());
40  self::assertSame(
41  'The date "%s" was not recognized (for format "%s").',
42  $messages->getFirstError()->getMessage()
43  );
44  }
45 
46  #[Test]
48  {
49  $propertyMapper = $this->get(PropertyMapper::class);
50 
51  $dateTime = $propertyMapper->convert('', \DateTime::class);
52 
53  self::assertNull($dateTime);
54  self::assertFalse($propertyMapper->getMessages()->hasErrors());
55  }
56 
57  #[Test]
58  public function ‪convertDefaultDateFormatString(): void
59  {
60  $propertyMapper = $this->get(PropertyMapper::class);
61 
62  $dateTime = $propertyMapper->convert('2019-12-07T19:07:02+00:00', \DateTime::class);
63 
64  self::assertInstanceOf(\DateTime::class, $dateTime);
65  self::assertSame(1575745622, $dateTime->getTimestamp());
66  }
67 
68  #[Test]
69  public function ‪convertCustomDateFormatString(): void
70  {
71  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
72  $propertyMapperConfiguration->setTypeConverterOption(
73  DateTimeConverter::class,
75  \DateTimeInterface::RFC7231
76  );
77 
78  $propertyMapper = $this->get(PropertyMapper::class);
79 
80  $dateTime = $propertyMapper->convert(
81  'Sat, 07 Dec 2019 19:15:45 GMT',
82  \DateTime::class,
83  $propertyMapperConfiguration
84  );
85 
86  self::assertInstanceOf(\DateTime::class, $dateTime);
87  self::assertSame(1575746145, $dateTime->getTimestamp());
88  }
89 
90  #[Test]
92  {
93  $this->expectException(Exception::class);
94  $this->expectExceptionCode(1297759968);
95  $this->expectExceptionMessage('Exception while property mapping at property path "": CONFIGURATION_DATE_FORMAT must be of type string, "array" given');
96 
97  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
98  $propertyMapperConfiguration->setTypeConverterOption(
99  DateTimeConverter::class,
101  []
102  );
103 
104  $propertyMapper = $this->get(PropertyMapper::class);
105 
106  $propertyMapper->convert(
107  'Sat, 07 Dec 2019 19:15:45 GMT',
108  \DateTime::class,
109  $propertyMapperConfiguration
110  );
111  }
112 
113  #[Test]
115  {
116  $propertyMapper = $this->get(PropertyMapper::class);
117 
118  $dateTime = $propertyMapper->convert(
119  [
120  'date' => '2019-12-07T19:07:02+00:00',
121  ],
122  \DateTime::class
123  );
124 
125  self::assertInstanceOf(\DateTime::class, $dateTime);
126  self::assertSame(1575745622, $dateTime->getTimestamp());
127  }
128 
129  #[Test]
131  {
132  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
133  $propertyMapperConfiguration->setTypeConverterOption(
134  DateTimeConverter::class,
136  'U'
137  );
138 
139  $propertyMapper = $this->get(PropertyMapper::class);
140 
141  $dateTime = $propertyMapper->convert(
142  [
143  'date' => 1575745622,
144  ],
145  \DateTime::class,
146  $propertyMapperConfiguration
147  );
148 
149  self::assertInstanceOf(\DateTime::class, $dateTime);
150  self::assertSame(1575745622, $dateTime->getTimestamp());
151  }
152 
153  #[Test]
155  {
156  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
157  $propertyMapperConfiguration->setTypeConverterOption(
158  DateTimeConverter::class,
160  'Y-m-d'
161  );
162 
163  $propertyMapper = $this->get(PropertyMapper::class);
164 
165  $dateTime = $propertyMapper->convert(
166  [
167  'day' => '12',
168  'month' => '12',
169  'year' => '2019',
170  ],
171  \DateTime::class,
172  $propertyMapperConfiguration
173  );
174 
175  self::assertInstanceOf(\DateTime::class, $dateTime);
176  self::assertSame('2019-12-12', $dateTime->format('Y-m-d'));
177  }
178 
179  #[Test]
181  {
182  $propertyMapper = $this->get(PropertyMapper::class);
183 
184  $dateTime = $propertyMapper->convert(
185  [
186  'day' => '12',
187  'month' => '12',
188  'year' => '2019',
189  'dateFormat' => 'Y-m-d',
190  ],
191  \DateTime::class
192  );
193 
194  self::assertInstanceOf(\DateTime::class, $dateTime);
195  self::assertSame('2019-12-12', $dateTime->format('Y-m-d'));
196  }
197 
198  #[Test]
200  {
201  $propertyMapper = $this->get(PropertyMapper::class);
202 
203  $dateTime = $propertyMapper->convert(
204  [
205  'day' => '12',
206  'month' => '12',
207  'year' => '2019',
208  'hour' => '15',
209  'minute' => '5',
210  'second' => '54',
211  'dateFormat' => 'Y-m-d',
212  ],
213  \DateTime::class
214  );
215 
216  self::assertInstanceOf(\DateTime::class, $dateTime);
217  self::assertSame('2019-12-12 15:05:54', $dateTime->format('Y-m-d H:i:s'));
218  self::assertSame(1576163154, $dateTime->getTimestamp());
219  }
220 
221  #[Test]
223  {
224  // Hint:
225  // The timezone parameter and the current timezone are ignored when the time parameter
226  // either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
227 
228  $propertyMapper = $this->get(PropertyMapper::class);
229 
230  $dateTime = $propertyMapper->convert(
231  [
232  'date' => '2019-12-07T19:07:02+00:00',
233  'timezone' => 'Pacific/Midway',
234  ],
235  \DateTime::class
236  );
237 
238  self::assertInstanceOf(\DateTime::class, $dateTime);
239  self::assertSame('2019-12-07T19:07:02+00:00', $dateTime->format(\DateTimeInterface::W3C));
240  self::assertSame(1575745622, $dateTime->getTimestamp());
241  }
242 
243  #[Test]
245  {
246  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
247  $propertyMapperConfiguration->setTypeConverterOption(
248  DateTimeConverter::class,
250  'Y-m-d H:i:s'
251  );
252 
253  $propertyMapper = $this->get(PropertyMapper::class);
254 
255  $dateTime = $propertyMapper->convert(
256  [
257  'date' => '2019-12-07 19:07:02',
258  'timezone' => 'Pacific/Midway',
259  ],
260  \DateTime::class,
261  $propertyMapperConfiguration
262  );
263 
264  self::assertInstanceOf(\DateTime::class, $dateTime);
265  self::assertSame('2019-12-07T19:07:02-11:00', $dateTime->format(\DateTimeInterface::W3C));
266  self::assertSame(1575785222, $dateTime->getTimestamp());
267  }
268 
269  #[Test]
271  {
272  $propertyMapper = $this->get(PropertyMapper::class);
273 
274  $dateTime = $propertyMapper->convert(
275  [
276  'day' => '0',
277  'month' => '1',
278  'year' => '1',
279  ],
280  \DateTime::class
281  );
282 
283  self::assertNull($dateTime);
284  $messages = $propertyMapper->getMessages();
285  self::assertTrue($messages->hasErrors());
286  self::assertSame(
287  'Could not convert the given date parts into a DateTime object because one or more parts were 0.',
288  $messages->getFirstError()->getMessage()
289  );
290  }
291 
292  #[Test]
294  {
295  $this->expectException(Exception::class);
296  $this->expectExceptionCode(1297759968);
297  $this->expectExceptionMessage('Exception while property mapping at property path "": Could not convert the given source into a DateTime object because it was not an array with a valid date as a string');
298 
299  $this->get(PropertyMapper::class)->convert([], \DateTime::class);
300  }
301 
302  #[Test]
304  {
305  $this->expectException(Exception::class);
306  $this->expectExceptionCode(1297759968);
307  $this->expectExceptionMessage('Exception while property mapping at property path "": The specified timezone "foo" is invalid.');
308 
309  $propertyMapperConfiguration = new ‪PropertyMappingConfiguration();
310  $propertyMapperConfiguration->setTypeConverterOption(
311  DateTimeConverter::class,
313  'Y-m-d H:i:s'
314  );
315 
316  $propertyMapper = $this->get(PropertyMapper::class);
317 
318  $propertyMapper->convert(
319  [
320  'date' => '2019-12-07 19:07:02',
321  'timezone' => 'foo',
322  ],
323  \DateTime::class,
324  $propertyMapperConfiguration
325  );
326  }
327 }
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertFromReturnsNullIfSourceIsAnEmptyString
‪convertFromReturnsNullIfSourceIsAnEmptyString()
Definition: DateTimeConverterTest.php:47
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertFromReturnsAnErrorWhenConvertingIntegersToDateTime
‪convertFromReturnsAnErrorWhenConvertingIntegersToDateTime()
Definition: DateTimeConverterTest.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertCustomDateFormatString
‪convertCustomDateFormatString()
Definition: DateTimeConverterTest.php:69
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertFromThrowsTypeConverterExceptionIfSourceIsAnInvalidArraySource
‪convertFromThrowsTypeConverterExceptionIfSourceIsAnInvalidArraySource()
Definition: DateTimeConverterTest.php:293
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
Definition: DateTimeConverter.php:64
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\$initializeDatabase
‪bool $initializeDatabase
Definition: DateTimeConverterTest.php:29
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertDefaultDateFormatString
‪convertDefaultDateFormatString()
Definition: DateTimeConverterTest.php:58
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\CONFIGURATION_DATE_FORMAT
‪const CONFIGURATION_DATE_FORMAT
Definition: DateTimeConverter.php:68
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertWithArraySourceWithDayMonthYearAndTimeZoneSet
‪convertWithArraySourceWithDayMonthYearAndTimeZoneSet()
Definition: DateTimeConverterTest.php:244
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter
Definition: ArrayConverterTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertWithArraySourceWithStringDate
‪convertWithArraySourceWithStringDate()
Definition: DateTimeConverterTest.php:114
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertWithArraySourceWithDayMonthYearAndTimeZoneSetWithDateThatIncludesTimezone
‪convertWithArraySourceWithDayMonthYearAndTimeZoneSetWithDateThatIncludesTimezone()
Definition: DateTimeConverterTest.php:222
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertWithArraySourceWithDayMonthYearHourMinuteAndSecondSet
‪convertWithArraySourceWithDayMonthYearHourMinuteAndSecondSet()
Definition: DateTimeConverterTest.php:199
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertFromReturnsErrorIfSourceIsAnArrayAndEitherDayMonthOrYearAreLowerThanOne
‪convertFromReturnsErrorIfSourceIsAnArrayAndEitherDayMonthOrYearAreLowerThanOne()
Definition: DateTimeConverterTest.php:270
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertFromThrowsTypeConverterExceptionIfGivenDateTimeZoneIsInvalid
‪convertFromThrowsTypeConverterExceptionIfGivenDateTimeZoneIsInvalid()
Definition: DateTimeConverterTest.php:303
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertThrowsInvalidPropertyMappingConfigurationExceptionIfDateFormatIsNotAString
‪convertThrowsInvalidPropertyMappingConfigurationExceptionIfDateFormatIsNotAString()
Definition: DateTimeConverterTest.php:91
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertWithArraySourceWithIntegerDate
‪convertWithArraySourceWithIntegerDate()
Definition: DateTimeConverterTest.php:130
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertWithArraySourceWithDayMonthAndYearSet
‪convertWithArraySourceWithDayMonthAndYearSet()
Definition: DateTimeConverterTest.php:154
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest
Definition: DateTimeConverterTest.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Property\TypeConverter\DateTimeConverterTest\convertWithArraySourceWithDayMonthYearAndDateFormatSet
‪convertWithArraySourceWithDayMonthYearAndDateFormatSet()
Definition: DateTimeConverterTest.php:180