[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Property / TypeConverter / DateTimeConverterTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter;
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
24 use TYPO3\CMS\Extbase\Property\Exception\TypeConverterException;
25 use TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\DateTimeSubFixture;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28 /**
29 * Test case
30 */
31 class DateTimeConverterTest extends UnitTestCase
32 {
33 /**
34 * @var \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
35 */
36 protected $converter;
37
38 protected function setUp()
39 {
40 $this->converter = new \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter();
41 }
42
43 /**
44 * @test
45 */
46 public function checkMetadata()
47 {
48 $this->assertEquals(['string', 'integer', 'array'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
49 $this->assertEquals('DateTime', $this->converter->getSupportedTargetType(), 'Target type does not match');
50 $this->assertEquals(10, $this->converter->getPriority(), 'Priority does not match');
51 }
52
53 /** String to DateTime testcases **/
54
55 /**
56 * @test
57 */
58 public function canConvertFromReturnsFalseIfTargetTypeIsNotDateTime()
59 {
60 $this->assertFalse($this->converter->canConvertFrom('Foo', 'SomeOtherType'));
61 }
62
63 /**
64 * @test
65 */
66 public function canConvertFromReturnsTrueIfSourceTypeIsAString()
67 {
68 $this->assertTrue($this->converter->canConvertFrom('Foo', 'DateTime'));
69 }
70
71 /**
72 * @test
73 */
74 public function canConvertFromReturnsTrueIfSourceTypeIsAnEmptyString()
75 {
76 $this->assertTrue($this->converter->canConvertFrom('', 'DateTime'));
77 }
78
79 /**
80 * @test
81 */
82 public function convertFromReturnsErrorIfGivenStringCantBeConverted()
83 {
84 $error = $this->converter->convertFrom('1980-12-13', 'DateTime');
85 $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $error);
86 }
87
88 /**
89 * @test
90 */
91 public function convertFromProperlyConvertsStringWithDefaultDateFormat()
92 {
93 $expectedResult = '1980-12-13T20:15:07+01:23';
94 $date = $this->converter->convertFrom($expectedResult, 'DateTime');
95 $actualResult = $date->format('Y-m-d\\TH:i:sP');
96 $this->assertSame($expectedResult, $actualResult);
97 }
98
99 /**
100 * @test
101 */
102 public function convertFromUsesDefaultDateFormatIfItIsNotConfigured()
103 {
104 $expectedResult = '1980-12-13T20:15:07+01:23';
105 $mockMappingConfiguration = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
106 $mockMappingConfiguration
107 ->expects($this->atLeastOnce())
108 ->method('getConfigurationValue')
109 ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
110 ->will($this->returnValue(null));
111
112 $date = $this->converter->convertFrom($expectedResult, 'DateTime', [], $mockMappingConfiguration);
113 $actualResult = $date->format(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT);
114 $this->assertSame($expectedResult, $actualResult);
115 }
116
117 /**
118 * @test
119 */
120 public function convertFromEmptyStringReturnsNull()
121 {
122 $date = $this->converter->convertFrom('', 'DateTime', [], null);
123 $this->assertNull($date);
124 }
125
126 /**
127 * @return array
128 * @see convertFromStringTests()
129 */
130 public function convertFromStringDataProvider()
131 {
132 return [
133 ['1308174051', '', false],
134 ['13-12-1980', 'd.m.Y', false],
135 ['1308174051', 'Y-m-d', false],
136 ['12:13', 'H:i', true],
137 ['13.12.1980', 'd.m.Y', true],
138 ['2005-08-15T15:52:01+00:00', null, true],
139 ['2005-08-15T15:52:01+00:00', \DateTime::ATOM, true],
140 ['1308174051', 'U', true],
141 ];
142 }
143
144 /**
145 * @param string $source the string to be converted
146 * @param string $dateFormat the expected date format
147 * @param bool $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
148 * @test
149 * @dataProvider convertFromStringDataProvider
150 */
151 public function convertFromStringTests($source, $dateFormat, $isValid)
152 {
153 if ($dateFormat !== null) {
154 $mockMappingConfiguration = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
155 $mockMappingConfiguration
156 ->expects($this->atLeastOnce())
157 ->method('getConfigurationValue')
158 ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
159 ->will($this->returnValue($dateFormat));
160 } else {
161 $mockMappingConfiguration = null;
162 }
163 $date = $this->converter->convertFrom($source, 'DateTime', [], $mockMappingConfiguration);
164 if ($isValid !== true) {
165 $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $date);
166 return;
167 }
168 $this->assertInstanceOf('DateTime', $date);
169
170 if ($dateFormat === null) {
171 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
172 }
173 $this->assertSame($source, $date->format($dateFormat));
174 }
175
176 /**
177 * @return array
178 * @see convertFromIntegerOrDigitStringWithoutConfigurationTests()
179 * @see convertFromIntegerOrDigitStringInArrayWithoutConfigurationTests()
180 */
181 public function convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider()
182 {
183 return [
184 ['1308174051'],
185 [1308174051],
186 ];
187 }
188
189 /**
190 * @test
191 * @param $source
192 * @dataProvider convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider
193 */
194 public function convertFromIntegerOrDigitStringWithoutConfigurationTests($source)
195 {
196 $date = $this->converter->convertFrom($source, 'DateTime', [], null);
197 $this->assertInstanceOf('DateTime', $date);
198 $this->assertSame(strval($source), $date->format('U'));
199 }
200
201 /** Array to DateTime testcases **/
202
203 /**
204 * @test
205 * @param $source
206 * @dataProvider convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider
207 */
208 public function convertFromIntegerOrDigitStringInArrayWithoutConfigurationTests($source)
209 {
210 $date = $this->converter->convertFrom(['date' => $source], 'DateTime', [], null);
211 $this->assertInstanceOf('DateTime', $date);
212 $this->assertSame(strval($source), $date->format('U'));
213 }
214
215 /**
216 * @test
217 */
218 public function canConvertFromReturnsTrueIfSourceTypeIsAnArray()
219 {
220 $this->assertTrue($this->converter->canConvertFrom([], 'DateTime'));
221 }
222
223 /**
224 * @test
225 */
226 public function convertFromReturnsErrorIfGivenArrayCantBeConverted()
227 {
228 $error = $this->converter->convertFrom(['date' => '1980-12-13'], 'DateTime');
229 $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $error);
230 }
231
232 /**
233 * @test
234 */
235 public function convertFromThrowsExceptionIfGivenArrayDoesNotSpecifyTheDate()
236 {
237 $this->expectException(TypeConverterException::class);
238 $this->expectExceptionCode(1308003914);
239 $this->converter->convertFrom(['hour' => '12', 'minute' => '30'], 'DateTime');
240 }
241
242 /**
243 * @test
244 */
245 public function convertFromProperlyConvertsArrayWithDefaultDateFormat()
246 {
247 $expectedResult = '1980-12-13T20:15:07+01:23';
248 $date = $this->converter->convertFrom(['date' => $expectedResult], 'DateTime');
249 $actualResult = $date->format('Y-m-d\\TH:i:sP');
250 $this->assertSame($expectedResult, $actualResult);
251 }
252
253 /**
254 * @return array
255 * @see convertFromThrowsExceptionIfDatePartKeysHaveInvalidValuesSpecified
256 */
257 public function invalidDatePartKeyValuesDataProvider()
258 {
259 return [
260 [['day' => '13.0', 'month' => '10', 'year' => '2010']],
261 [['day' => '13', 'month' => '10.0', 'year' => '2010']],
262 [['day' => '13', 'month' => '10', 'year' => '2010.0']],
263 [['day' => '-13', 'month' => '10', 'year' => '2010']],
264 [['day' => '13', 'month' => '-10', 'year' => '2010']],
265 [['day' => '13', 'month' => '10', 'year' => '-2010']],
266 ];
267 }
268
269 /**
270 * @test
271 * @dataProvider invalidDatePartKeyValuesDataProvider
272 */
273 public function convertFromThrowsExceptionIfDatePartKeysHaveInvalidValuesSpecified($source)
274 {
275 $this->expectException(TypeConverterException::class);
276 $this->expectExceptionCode(1308003914);
277 $this->converter->convertFrom($source, 'DateTime');
278 }
279
280 /**
281 * @test
282 */
283 public function convertFromProperlyConvertsArrayWithDateAsArray()
284 {
285 $source = ['day' => '13', 'month' => '10', 'year' => '2010'];
286 $mappingConfiguration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
287 $mappingConfiguration->setTypeConverterOption(
288 \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class,
289 \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
290 'Y-m-d'
291 );
292
293 $date = $this->converter->convertFrom($source, 'DateTime', [], $mappingConfiguration);
294 $actualResult = $date->format('Y-m-d');
295 $this->assertSame('2010-10-13', $actualResult);
296 }
297
298 /**
299 * @test
300 */
301 public function convertFromAllowsToOverrideTheTime()
302 {
303 $source = [
304 'date' => '2011-06-16',
305 'dateFormat' => 'Y-m-d',
306 'hour' => '12',
307 'minute' => '30',
308 'second' => '59',
309 ];
310 $date = $this->converter->convertFrom($source, 'DateTime');
311 $this->assertSame('2011-06-16', $date->format('Y-m-d'));
312 $this->assertSame('12', $date->format('H'));
313 $this->assertSame('30', $date->format('i'));
314 $this->assertSame('59', $date->format('s'));
315 }
316
317 /**
318 * @test
319 */
320 public function convertFromAllowsToOverrideTheTimezone()
321 {
322 $source = [
323 'date' => '2011-06-16 12:30:59',
324 'dateFormat' => 'Y-m-d H:i:s',
325 'timezone' => 'Atlantic/Reykjavik',
326 ];
327 $date = $this->converter->convertFrom($source, 'DateTime');
328 $this->assertSame('2011-06-16', $date->format('Y-m-d'));
329 $this->assertSame('12', $date->format('H'));
330 $this->assertSame('30', $date->format('i'));
331 $this->assertSame('59', $date->format('s'));
332 $this->assertSame('Atlantic/Reykjavik', $date->getTimezone()->getName());
333 }
334
335 /**
336 * @test
337 */
338 public function convertFromThrowsExceptionIfSpecifiedTimezoneIsInvalid()
339 {
340 $this->expectException(TypeConverterException::class);
341 $this->expectExceptionCode(1308240974);
342 $source = [
343 'date' => '2011-06-16',
344 'dateFormat' => 'Y-m-d',
345 'timezone' => 'Invalid/Timezone',
346 ];
347 $this->converter->convertFrom($source, 'DateTime');
348 }
349
350 /**
351 * @test
352 */
353 public function convertFromArrayThrowsExceptionForEmptyArray()
354 {
355 $this->expectException(TypeConverterException::class);
356 $this->expectExceptionCode(1308003914);
357 $this->converter->convertFrom([], 'DateTime', [], null);
358 }
359
360 /**
361 * @test
362 */
363 public function convertFromArrayReturnsNullForEmptyDate()
364 {
365 $this->assertNull($this->converter->convertFrom(['date' => ''], 'DateTime', [], null));
366 }
367
368 /**
369 * @return array
370 * @see convertFromArrayTests()
371 */
372 public function convertFromArrayDataProvider()
373 {
374 return [
375 [['date' => '2005-08-15T15:52:01+01:00'], true],
376 [['date' => '1308174051', 'dateFormat' => ''], false],
377 [['date' => '13-12-1980', 'dateFormat' => 'd.m.Y'], false],
378 [['date' => '1308174051', 'dateFormat' => 'Y-m-d'], false],
379 [['date' => '12:13', 'dateFormat' => 'H:i'], true],
380 [['date' => '13.12.1980', 'dateFormat' => 'd.m.Y'], true],
381 [['date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => ''], true],
382 [['date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => \DateTime::ATOM], true],
383 [['date' => '1308174051', 'dateFormat' => 'U'], true],
384 [['date' => 1308174051, 'dateFormat' => 'U'], true],
385 ];
386 }
387
388 /**
389 * @param array $source the array to be converted
390 * @param bool $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
391 * @test
392 * @dataProvider convertFromArrayDataProvider
393 */
394 public function convertFromArrayTests(array $source, $isValid)
395 {
396 $dateFormat = isset($source['dateFormat']) && $source['dateFormat'] !== '' ? $source['dateFormat'] : null;
397 if ($dateFormat !== null) {
398 $mockMappingConfiguration = $this->createMock(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface::class);
399 $mockMappingConfiguration
400 ->expects($this->atLeastOnce())
401 ->method('getConfigurationValue')
402 ->with(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
403 ->will($this->returnValue($dateFormat));
404 } else {
405 $mockMappingConfiguration = null;
406 }
407 $date = $this->converter->convertFrom($source, 'DateTime', [], $mockMappingConfiguration);
408
409 if ($isValid !== true) {
410 $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Error::class, $date);
411 return;
412 }
413
414 $this->assertInstanceOf('DateTime', $date);
415 if ($dateFormat === null) {
416 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
417 }
418 $dateAsString = isset($source['date']) ? strval($source['date']) : '';
419 $this->assertSame($dateAsString, $date->format($dateFormat));
420 }
421
422 /**
423 * @test
424 */
425 public function convertFromSupportsDateTimeSubClasses()
426 {
427 $className = DateTimeSubFixture::class;
428 $date = $this->converter->convertFrom('2005-08-15T15:52:01+00:00', $className);
429
430 $this->assertInstanceOf($className, $date);
431 $this->assertSame('Bar', $date->foo());
432 }
433 }