TYPO3 CMS  TYPO3_8-7
DateTimeConverterTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the Extbase framework. *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License as published by the *
9  * Free Software Foundation, either version 3 of the License, or (at your *
10  * option) any later version. *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
23 
26 
30 class DateTimeConverterTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
31 {
35  protected $converter;
36 
37  protected function setUp()
38  {
39  $this->converter = new \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter();
40  }
41 
45  public function checkMetadata()
46  {
47  $this->assertEquals(['string', 'integer', 'array'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
48  $this->assertEquals('DateTime', $this->converter->getSupportedTargetType(), 'Target type does not match');
49  $this->assertEquals(10, $this->converter->getPriority(), 'Priority does not match');
50  }
51 
58  {
59  $this->assertFalse($this->converter->canConvertFrom('Foo', 'SomeOtherType'));
60  }
61 
66  {
67  $this->assertTrue($this->converter->canConvertFrom('Foo', 'DateTime'));
68  }
69 
74  {
75  $this->assertTrue($this->converter->canConvertFrom('', 'DateTime'));
76  }
77 
82  {
83  $error = $this->converter->convertFrom('1980-12-13', 'DateTime');
84  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $error);
85  }
86 
91  {
92  $expectedResult = '1980-12-13T20:15:07+01:23';
93  $date = $this->converter->convertFrom($expectedResult, 'DateTime');
94  $actualResult = $date->format('Y-m-d\\TH:i:sP');
95  $this->assertSame($expectedResult, $actualResult);
96  }
97 
102  {
103  $expectedResult = '1980-12-13T20:15:07+01:23';
104  $mockMappingConfiguration = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
105  $mockMappingConfiguration
106  ->expects($this->atLeastOnce())
107  ->method('getConfigurationValue')
108  ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
109  ->will($this->returnValue(null));
110 
111  $date = $this->converter->convertFrom($expectedResult, 'DateTime', [], $mockMappingConfiguration);
112  $actualResult = $date->format(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT);
113  $this->assertSame($expectedResult, $actualResult);
114  }
115 
120  {
121  $date = $this->converter->convertFrom('', 'DateTime', [], null);
122  $this->assertNull($date);
123  }
124 
130  {
131  return [
132  ['1308174051', '', false],
133  ['13-12-1980', 'd.m.Y', false],
134  ['1308174051', 'Y-m-d', false],
135  ['12:13', 'H:i', true],
136  ['13.12.1980', 'd.m.Y', true],
137  ['2005-08-15T15:52:01+00:00', null, true],
138  ['2005-08-15T15:52:01+00:00', \DateTime::ATOM, true],
139  ['1308174051', 'U', true],
140  ];
141  }
142 
150  public function convertFromStringTests($source, $dateFormat, $isValid)
151  {
152  if ($dateFormat !== null) {
153  $mockMappingConfiguration = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
154  $mockMappingConfiguration
155  ->expects($this->atLeastOnce())
156  ->method('getConfigurationValue')
157  ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
158  ->will($this->returnValue($dateFormat));
159  } else {
160  $mockMappingConfiguration = null;
161  }
162  $date = $this->converter->convertFrom($source, 'DateTime', [], $mockMappingConfiguration);
163  if ($isValid !== true) {
164  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $date);
165  return;
166  }
167  $this->assertInstanceOf('DateTime', $date);
168 
169  if ($dateFormat === null) {
171  }
172  $this->assertSame($source, $date->format($dateFormat));
173  }
174 
181  {
182  return [
183  ['1308174051'],
184  [1308174051],
185  ];
186  }
187 
194  {
195  $date = $this->converter->convertFrom($source, 'DateTime', [], null);
196  $this->assertInstanceOf('DateTime', $date);
197  $this->assertSame(strval($source), $date->format('U'));
198  }
199 
208  {
209  $date = $this->converter->convertFrom(['date' => $source], 'DateTime', [], null);
210  $this->assertInstanceOf('DateTime', $date);
211  $this->assertSame(strval($source), $date->format('U'));
212  }
213 
218  {
219  $this->assertTrue($this->converter->canConvertFrom([], 'DateTime'));
220  }
221 
226  {
227  $error = $this->converter->convertFrom(['date' => '1980-12-13'], 'DateTime');
228  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $error);
229  }
230 
235  {
236  $this->expectException(TypeConverterException::class);
237  $this->expectExceptionCode(1308003914);
238  $this->converter->convertFrom(['hour' => '12', 'minute' => '30'], 'DateTime');
239  }
240 
245  {
246  $expectedResult = '1980-12-13T20:15:07+01:23';
247  $date = $this->converter->convertFrom(['date' => $expectedResult], 'DateTime');
248  $actualResult = $date->format('Y-m-d\\TH:i:sP');
249  $this->assertSame($expectedResult, $actualResult);
250  }
251 
257  {
258  return [
259  [['day' => '13.0', 'month' => '10', 'year' => '2010']],
260  [['day' => '13', 'month' => '10.0', 'year' => '2010']],
261  [['day' => '13', 'month' => '10', 'year' => '2010.0']],
262  [['day' => '-13', 'month' => '10', 'year' => '2010']],
263  [['day' => '13', 'month' => '-10', 'year' => '2010']],
264  [['day' => '13', 'month' => '10', 'year' => '-2010']],
265  ];
266  }
267 
273  {
274  $this->expectException(TypeConverterException::class);
275  $this->expectExceptionCode(1308003914);
276  $this->converter->convertFrom($source, 'DateTime');
277  }
278 
283  {
284  $source = ['day' => '13', 'month' => '10', 'year' => '2010'];
285  $mappingConfiguration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
286  $mappingConfiguration->setTypeConverterOption(
287  \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class,
288  \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
289  'Y-m-d'
290  );
291 
292  $date = $this->converter->convertFrom($source, 'DateTime', [], $mappingConfiguration);
293  $actualResult = $date->format('Y-m-d');
294  $this->assertSame('2010-10-13', $actualResult);
295  }
296 
301  {
302  $source = [
303  'date' => '2011-06-16',
304  'dateFormat' => 'Y-m-d',
305  'hour' => '12',
306  'minute' => '30',
307  'second' => '59',
308  ];
309  $date = $this->converter->convertFrom($source, 'DateTime');
310  $this->assertSame('2011-06-16', $date->format('Y-m-d'));
311  $this->assertSame('12', $date->format('H'));
312  $this->assertSame('30', $date->format('i'));
313  $this->assertSame('59', $date->format('s'));
314  }
315 
320  {
321  $source = [
322  'date' => '2011-06-16 12:30:59',
323  'dateFormat' => 'Y-m-d H:i:s',
324  'timezone' => 'Atlantic/Reykjavik',
325  ];
326  $date = $this->converter->convertFrom($source, 'DateTime');
327  $this->assertSame('2011-06-16', $date->format('Y-m-d'));
328  $this->assertSame('12', $date->format('H'));
329  $this->assertSame('30', $date->format('i'));
330  $this->assertSame('59', $date->format('s'));
331  $this->assertSame('Atlantic/Reykjavik', $date->getTimezone()->getName());
332  }
333 
338  {
339  $this->expectException(TypeConverterException::class);
340  $this->expectExceptionCode(1308240974);
341  $source = [
342  'date' => '2011-06-16',
343  'dateFormat' => 'Y-m-d',
344  'timezone' => 'Invalid/Timezone',
345  ];
346  $this->converter->convertFrom($source, 'DateTime');
347  }
348 
353  {
354  $this->expectException(TypeConverterException::class);
355  $this->expectExceptionCode(1308003914);
356  $this->converter->convertFrom([], 'DateTime', [], null);
357  }
358 
363  {
364  $this->assertNull($this->converter->convertFrom(['date' => ''], 'DateTime', [], null));
365  }
366 
372  {
373  return [
374  [['date' => '2005-08-15T15:52:01+01:00'], true],
375  [['date' => '1308174051', 'dateFormat' => ''], false],
376  [['date' => '13-12-1980', 'dateFormat' => 'd.m.Y'], false],
377  [['date' => '1308174051', 'dateFormat' => 'Y-m-d'], false],
378  [['date' => '12:13', 'dateFormat' => 'H:i'], true],
379  [['date' => '13.12.1980', 'dateFormat' => 'd.m.Y'], true],
380  [['date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => ''], true],
381  [['date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => \DateTime::ATOM], true],
382  [['date' => '1308174051', 'dateFormat' => 'U'], true],
383  [['date' => 1308174051, 'dateFormat' => 'U'], true],
384  ];
385  }
386 
393  public function convertFromArrayTests(array $source, $isValid)
394  {
395  $dateFormat = isset($source['dateFormat']) && $source['dateFormat'] !== '' ? $source['dateFormat'] : null;
396  if ($dateFormat !== null) {
397  $mockMappingConfiguration = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
398  $mockMappingConfiguration
399  ->expects($this->atLeastOnce())
400  ->method('getConfigurationValue')
401  ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
402  ->will($this->returnValue($dateFormat));
403  } else {
404  $mockMappingConfiguration = null;
405  }
406  $date = $this->converter->convertFrom($source, 'DateTime', [], $mockMappingConfiguration);
407 
408  if ($isValid !== true) {
409  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $date);
410  return;
411  }
412 
413  $this->assertInstanceOf('DateTime', $date);
414  if ($dateFormat === null) {
416  }
417  $dateAsString = isset($source['date']) ? strval($source['date']) : '';
418  $this->assertSame($dateAsString, $date->format($dateFormat));
419  }
420 
425  {
426  $className = DateTimeSubFixture::class;
427  $date = $this->converter->convertFrom('2005-08-15T15:52:01+00:00', $className);
428 
429  $this->assertInstanceOf($className, $date);
430  $this->assertSame('Bar', $date->foo());
431  }
432 }