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