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