TYPO3 CMS  TYPO3_7-6
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 
25 
30 {
34  protected $converter;
35 
36  protected function setUp()
37  {
38  $this->converter = new \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter();
39  }
40 
44  public function checkMetadata()
45  {
46  $this->assertEquals(['string', 'integer', 'array'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
47  $this->assertEquals('DateTime', $this->converter->getSupportedTargetType(), 'Target type does not match');
48  $this->assertEquals(1, $this->converter->getPriority(), 'Priority does not match');
49  }
50 
57  {
58  $this->assertFalse($this->converter->canConvertFrom('Foo', 'SomeOtherType'));
59  }
60 
65  {
66  $this->assertTrue($this->converter->canConvertFrom('Foo', 'DateTime'));
67  }
68 
73  {
74  $this->assertTrue($this->converter->canConvertFrom('', 'DateTime'));
75  }
76 
81  {
82  $error = $this->converter->convertFrom('1980-12-13', 'DateTime');
83  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $error);
84  }
85 
90  {
91  $expectedResult = '1980-12-13T20:15:07+01:23';
92  $date = $this->converter->convertFrom($expectedResult, 'DateTime');
93  $actualResult = $date->format('Y-m-d\\TH:i:sP');
94  $this->assertSame($expectedResult, $actualResult);
95  }
96 
101  {
102  $expectedResult = '1980-12-13T20:15:07+01:23';
103  $mockMappingConfiguration = $this->getMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
104  $mockMappingConfiguration
105  ->expects($this->atLeastOnce())
106  ->method('getConfigurationValue')
107  ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
108  ->will($this->returnValue(null));
109 
110  $date = $this->converter->convertFrom($expectedResult, 'DateTime', [], $mockMappingConfiguration);
111  $actualResult = $date->format(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT);
112  $this->assertSame($expectedResult, $actualResult);
113  }
114 
119  {
120  $date = $this->converter->convertFrom('', 'DateTime', [], null);
121  $this->assertNull($date);
122  }
123 
129  {
130  return [
131  ['1308174051', '', false],
132  ['13-12-1980', 'd.m.Y', false],
133  ['1308174051', 'Y-m-d', false],
134  ['12:13', 'H:i', true],
135  ['13.12.1980', 'd.m.Y', true],
136  ['2005-08-15T15:52:01+00:00', null, true],
137  ['2005-08-15T15:52:01+0000', \DateTime::ISO8601, true],
138  ['1308174051', 'U', true],
139  ];
140  }
141 
149  public function convertFromStringTests($source, $dateFormat, $isValid)
150  {
151  if ($dateFormat !== null) {
152  $mockMappingConfiguration = $this->getMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
153  $mockMappingConfiguration
154  ->expects($this->atLeastOnce())
155  ->method('getConfigurationValue')
156  ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
157  ->will($this->returnValue($dateFormat));
158  } else {
159  $mockMappingConfiguration = null;
160  }
161  $date = $this->converter->convertFrom($source, 'DateTime', [], $mockMappingConfiguration);
162  if ($isValid !== true) {
163  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $date);
164  return;
165  }
166  $this->assertInstanceOf('DateTime', $date);
167 
168  if ($dateFormat === null) {
170  }
171  $this->assertSame($source, $date->format($dateFormat));
172  }
173 
180  {
181  return [
182  ['1308174051'],
183  [1308174051],
184  ];
185  }
186 
193  {
194  $date = $this->converter->convertFrom($source, 'DateTime', [], null);
195  $this->assertInstanceOf('DateTime', $date);
196  $this->assertSame(strval($source), $date->format('U'));
197  }
198 
207  {
208  $date = $this->converter->convertFrom(['date' => $source], 'DateTime', [], null);
209  $this->assertInstanceOf('DateTime', $date);
210  $this->assertSame(strval($source), $date->format('U'));
211  }
212 
217  {
218  $this->assertTrue($this->converter->canConvertFrom([], 'DateTime'));
219  }
220 
225  {
226  $error = $this->converter->convertFrom(['date' => '1980-12-13'], 'DateTime');
227  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $error);
228  }
229 
235  {
236  $this->converter->convertFrom(['hour' => '12', 'minute' => '30'], 'DateTime');
237  }
238 
243  {
244  $expectedResult = '1980-12-13T20:15:07+01:23';
245  $date = $this->converter->convertFrom(['date' => $expectedResult], 'DateTime');
246  $actualResult = $date->format('Y-m-d\\TH:i:sP');
247  $this->assertSame($expectedResult, $actualResult);
248  }
249 
255  {
256  return [
257  [['day' => '13.0', 'month' => '10', 'year' => '2010']],
258  [['day' => '13', 'month' => '10.0', 'year' => '2010']],
259  [['day' => '13', 'month' => '10', 'year' => '2010.0']],
260  [['day' => '-13', 'month' => '10', 'year' => '2010']],
261  [['day' => '13', 'month' => '-10', 'year' => '2010']],
262  [['day' => '13', 'month' => '10', 'year' => '-2010']],
263  ];
264  }
265 
272  {
273  $this->converter->convertFrom($source, 'DateTime');
274  }
275 
280  {
281  $source = ['day' => '13', 'month' => '10', 'year' => '2010'];
282  $mappingConfiguration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
283  $mappingConfiguration->setTypeConverterOption(
284  \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class,
285  \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
286  'Y-m-d'
287  );
288 
289  $date = $this->converter->convertFrom($source, 'DateTime', [], $mappingConfiguration);
290  $actualResult = $date->format('Y-m-d');
291  $this->assertSame('2010-10-13', $actualResult);
292  }
293 
298  {
299  $source = [
300  'date' => '2011-06-16',
301  'dateFormat' => 'Y-m-d',
302  'hour' => '12',
303  'minute' => '30',
304  'second' => '59',
305  ];
306  $date = $this->converter->convertFrom($source, 'DateTime');
307  $this->assertSame('2011-06-16', $date->format('Y-m-d'));
308  $this->assertSame('12', $date->format('H'));
309  $this->assertSame('30', $date->format('i'));
310  $this->assertSame('59', $date->format('s'));
311  }
312 
317  {
318  $source = [
319  'date' => '2011-06-16 12:30:59',
320  'dateFormat' => 'Y-m-d H:i:s',
321  'timezone' => 'Atlantic/Reykjavik',
322  ];
323  $date = $this->converter->convertFrom($source, 'DateTime');
324  $this->assertSame('2011-06-16', $date->format('Y-m-d'));
325  $this->assertSame('12', $date->format('H'));
326  $this->assertSame('30', $date->format('i'));
327  $this->assertSame('59', $date->format('s'));
328  $this->assertSame('Atlantic/Reykjavik', $date->getTimezone()->getName());
329  }
330 
336  {
337  $source = [
338  'date' => '2011-06-16',
339  'dateFormat' => 'Y-m-d',
340  'timezone' => 'Invalid/Timezone',
341  ];
342  $this->converter->convertFrom($source, 'DateTime');
343  }
344 
350  {
351  $this->converter->convertFrom([], 'DateTime', [], null);
352  }
353 
358  {
359  $this->assertNull($this->converter->convertFrom(['date' => ''], 'DateTime', [], null));
360  }
361 
367  {
368  return [
369  [['date' => '2005-08-15T15:52:01+01:00'], true],
370  [['date' => '1308174051', 'dateFormat' => ''], false],
371  [['date' => '13-12-1980', 'dateFormat' => 'd.m.Y'], false],
372  [['date' => '1308174051', 'dateFormat' => 'Y-m-d'], false],
373  [['date' => '12:13', 'dateFormat' => 'H:i'], true],
374  [['date' => '13.12.1980', 'dateFormat' => 'd.m.Y'], true],
375  [['date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => ''], true],
376  [['date' => '2005-08-15T15:52:01+0000', 'dateFormat' => \DateTime::ISO8601], true],
377  [['date' => '1308174051', 'dateFormat' => 'U'], true],
378  [['date' => 1308174051, 'dateFormat' => 'U'], true],
379  ];
380  }
381 
388  public function convertFromArrayTests(array $source, $isValid)
389  {
390  $dateFormat = isset($source['dateFormat']) && $source['dateFormat'] !== '' ? $source['dateFormat'] : null;
391  if ($dateFormat !== null) {
392  $mockMappingConfiguration = $this->getMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
393  $mockMappingConfiguration
394  ->expects($this->atLeastOnce())
395  ->method('getConfigurationValue')
396  ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
397  ->will($this->returnValue($dateFormat));
398  } else {
399  $mockMappingConfiguration = null;
400  }
401  $date = $this->converter->convertFrom($source, 'DateTime', [], $mockMappingConfiguration);
402 
403  if ($isValid !== true) {
404  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $date);
405  return;
406  }
407 
408  $this->assertInstanceOf('DateTime', $date);
409  if ($dateFormat === null) {
411  }
412  $dateAsString = isset($source['date']) ? strval($source['date']) : '';
413  $this->assertSame($dateAsString, $date->format($dateFormat));
414  }
415 
420  {
421  $className = DateTimeSubFixture::class;
422  $date = $this->converter->convertFrom('2005-08-15T15:52:01+00:00', $className);
423 
424  $this->assertInstanceOf($className, $date);
425  $this->assertSame('Bar', $date->foo());
426  }
427 }