28ff2393648e55b5db4547251edfd869016ad0f2
[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 * Testcase for the DateTime converter
25 *
26 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
27 * @covers \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter<extended>
28 */
29 class DateTimeConverterTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
33 */
34 protected $converter;
35
36 public function setUp() {
37 $this->converter = new \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter();
38 }
39
40 /**
41 * @test
42 * @author Bastian Waidelich <bastian@typo3.org>
43 */
44 public function checkMetadata() {
45 $this->assertEquals(array('string', 'array'), $this->converter->getSupportedSourceTypes(), 'Source types do not match');
46 $this->assertEquals('DateTime', $this->converter->getSupportedTargetType(), 'Target type does not match');
47 $this->assertEquals(1, $this->converter->getPriority(), 'Priority does not match');
48 }
49
50 /**
51 * String to DateTime testcases *
52 */
53 /**
54 * @test
55 * @author Bastian Waidelich <bastian@typo3.org>
56 */
57 public function canConvertFromReturnsFalseIfTargetTypeIsNotDateTime() {
58 $this->assertFalse($this->converter->canConvertFrom('Foo', 'SomeOtherType'));
59 }
60
61 /**
62 * @test
63 * @author Bastian Waidelich <bastian@typo3.org>
64 */
65 public function canConvertFromReturnsTrueIfSourceTypeIsAString() {
66 $this->assertTrue($this->converter->canConvertFrom('Foo', 'DateTime'));
67 }
68
69 /**
70 * @test
71 * @author Bastian Waidelich <bastian@typo3.org>
72 */
73 public function canConvertFromReturnsTrueIfSourceTypeIsAnEmptyString() {
74 $this->assertTrue($this->converter->canConvertFrom('', 'DateTime'));
75 }
76
77 /**
78 * @test
79 * @author Bastian Waidelich <bastian@typo3.org>
80 */
81 public function convertFromReturnsErrorIfGivenStringCantBeConverted() {
82 $error = $this->converter->convertFrom('1980-12-13', 'DateTime');
83 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $error);
84 }
85
86 /**
87 * @test
88 * @author Bastian Waidelich <bastian@typo3.org>
89 */
90 public function convertFromProperlyConvertsStringWithDefaultDateFormat() {
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
97 /**
98 * @test
99 * @author Bastian Waidelich <bastian@typo3.org>
100 */
101 public function convertFromUsesDefaultDateFormatIfItIsNotConfigured() {
102 $expectedResult = '1980-12-13T20:15:07+01:23';
103 $mockMappingConfiguration = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMappingConfigurationInterface');
104 $mockMappingConfiguration->expects($this->atLeastOnce())->method('getConfigurationValue')->with('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)->will($this->returnValue(NULL));
105 $date = $this->converter->convertFrom($expectedResult, 'DateTime', array(), $mockMappingConfiguration);
106 $actualResult = $date->format(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT);
107 $this->assertSame($expectedResult, $actualResult);
108 }
109
110 /**
111 * @test
112 */
113 public function convertFromEmptyStringReturnsNull() {
114 $date = $this->converter->convertFrom('', 'DateTime', array(), NULL);
115 $this->assertNull($date);
116 }
117
118 /**
119 * @return array
120 * @see convertFromStringTests()
121 * @author Bastian Waidelich <bastian@typo3.org>
122 */
123 public function convertFromStringDataProvider() {
124 return array(
125 array('1308174051', '', FALSE),
126 array('13-12-1980', 'd.m.Y', FALSE),
127 array('1308174051', 'Y-m-d', FALSE),
128 array('12:13', 'H:i', TRUE),
129 array('13.12.1980', 'd.m.Y', TRUE),
130 array('2005-08-15T15:52:01+00:00', NULL, TRUE),
131 array('2005-08-15T15:52:01+0000', \DateTime::ISO8601, TRUE),
132 array('1308174051', 'U', TRUE)
133 );
134 }
135
136 /**
137 * @param string $source the string to be converted
138 * @param string $dateFormat the expected date format
139 * @param boolean $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
140 * @test
141 * @dataProvider convertFromStringDataProvider
142 * @author Bastian Waidelich <bastian@typo3.org>
143 */
144 public function convertFromStringTests($source, $dateFormat, $isValid) {
145 if ($dateFormat !== NULL) {
146 $mockMappingConfiguration = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMappingConfigurationInterface');
147 $mockMappingConfiguration->expects($this->atLeastOnce())->method('getConfigurationValue')->with('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)->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', $date);
154 return;
155 }
156 $this->assertInstanceOf('DateTime', $date);
157 if ($dateFormat === NULL) {
158 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
159 }
160 $this->assertSame($source, $date->format($dateFormat));
161 }
162
163 /**
164 * Array to DateTime testcases *
165 */
166 /**
167 * @test
168 * @author Bastian Waidelich <bastian@typo3.org>
169 */
170 public function canConvertFromReturnsTrueIfSourceTypeIsAnArray() {
171 $this->assertTrue($this->converter->canConvertFrom(array(), 'DateTime'));
172 }
173
174 /**
175 * @test
176 * @author Bastian Waidelich <bastian@typo3.org>
177 */
178 public function convertFromReturnsErrorIfGivenArrayCantBeConverted() {
179 $error = $this->converter->convertFrom(array('date' => '1980-12-13'), 'DateTime');
180 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $error);
181 }
182
183 /**
184 * @test
185 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
186 * @author Bastian Waidelich <bastian@typo3.org>
187 */
188 public function convertFromThrowsExceptionIfGivenArrayDoesNotSpecifyTheDate() {
189 $this->converter->convertFrom(array('hour' => '12', 'minute' => '30'), 'DateTime');
190 }
191
192 /**
193 * @test
194 * @author Bastian Waidelich <bastian@typo3.org>
195 */
196 public function convertFromProperlyConvertsArrayWithDefaultDateFormat() {
197 $expectedResult = '1980-12-13T20:15:07+01:23';
198 $date = $this->converter->convertFrom(array('date' => $expectedResult), 'DateTime');
199 $actualResult = $date->format('Y-m-d\\TH:i:sP');
200 $this->assertSame($expectedResult, $actualResult);
201 }
202
203 /**
204 * @test
205 * @author Bastian Waidelich <bastian@typo3.org>
206 */
207 public function convertFromAllowsToOverrideTheTime() {
208 $source = array(
209 'date' => '2011-06-16',
210 'dateFormat' => 'Y-m-d',
211 'hour' => '12',
212 'minute' => '30',
213 'second' => '59'
214 );
215 $date = $this->converter->convertFrom($source, 'DateTime');
216 $this->assertSame('2011-06-16', $date->format('Y-m-d'));
217 $this->assertSame('12', $date->format('H'));
218 $this->assertSame('30', $date->format('i'));
219 $this->assertSame('59', $date->format('s'));
220 }
221
222 /**
223 * @test
224 * @author Bastian Waidelich <bastian@typo3.org>
225 */
226 public function convertFromAllowsToOverrideTheTimezone() {
227 $source = array(
228 'date' => '2011-06-16',
229 'dateFormat' => 'Y-m-d',
230 'timezone' => 'Atlantic/Reykjavik'
231 );
232 $date = $this->converter->convertFrom($source, 'DateTime');
233 $this->assertSame('Atlantic/Reykjavik', $date->getTimezone()->getName());
234 }
235
236 /**
237 * @test
238 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
239 * @author Bastian Waidelich <bastian@typo3.org>
240 */
241 public function convertFromThrowsExceptionIfSpecifiedTimezoneIsInvalid() {
242 $source = array(
243 'date' => '2011-06-16',
244 'dateFormat' => 'Y-m-d',
245 'timezone' => 'Invalid/Timezone'
246 );
247 $this->converter->convertFrom($source, 'DateTime');
248 }
249
250 /**
251 * @test
252 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
253 * @author Sebastian Kurfürst <sebastian@typo3.org>
254 */
255 public function convertFromArrayThrowsExceptionForEmptyArray() {
256 $this->converter->convertFrom(array(), 'DateTime', array(), NULL);
257 }
258
259 /**
260 * @test
261 * @author Sebastian Kurfürst <sebastian@typo3.org>
262 */
263 public function convertFromArrayReturnsNullForEmptyDate() {
264 $this->assertNull($this->converter->convertFrom(array('date' => ''), 'DateTime', array(), NULL));
265 }
266
267 /**
268 * @return array
269 * @see convertFromArrayTests()
270 * @author Bastian Waidelich <bastian@typo3.org>
271 */
272 public function convertFromArrayDataProvider() {
273 return array(
274 array(array('date' => '1308174051'), FALSE),
275 array(array('date' => '2005-08-15T15:52:01+01:00'), TRUE),
276 array(array('date' => '1308174051', 'dateFormat' => ''), FALSE),
277 array(array('date' => '13-12-1980', 'dateFormat' => 'd.m.Y'), FALSE),
278 array(array('date' => '1308174051', 'dateFormat' => 'Y-m-d'), FALSE),
279 array(array('date' => '12:13', 'dateFormat' => 'H:i'), TRUE),
280 array(array('date' => '13.12.1980', 'dateFormat' => 'd.m.Y'), TRUE),
281 array(array('date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => ''), TRUE),
282 array(array('date' => '2005-08-15T15:52:01+0000', 'dateFormat' => \DateTime::ISO8601), TRUE),
283 array(array('date' => '1308174051', 'dateFormat' => 'U'), TRUE)
284 );
285 }
286
287 /**
288 * @param array $source the array to be converted
289 * @param boolean $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
290 * @test
291 * @dataProvider convertFromArrayDataProvider
292 * @author Bastian Waidelich <bastian@typo3.org>
293 */
294 public function convertFromArrayTests(array $source, $isValid) {
295 $dateFormat = isset($source['dateFormat']) && strlen($source['dateFormat']) > 0 ? $source['dateFormat'] : NULL;
296 if ($dateFormat !== NULL) {
297 $mockMappingConfiguration = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMappingConfigurationInterface');
298 $mockMappingConfiguration->expects($this->atLeastOnce())->method('getConfigurationValue')->with('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)->will($this->returnValue($dateFormat));
299 } else {
300 $mockMappingConfiguration = NULL;
301 }
302 $date = $this->converter->convertFrom($source, 'DateTime', array(), $mockMappingConfiguration);
303 if ($isValid !== TRUE) {
304 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $date);
305 return;
306 }
307 $this->assertInstanceOf('DateTime', $date);
308 if ($dateFormat === NULL) {
309 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
310 }
311 $dateAsString = isset($source['date']) ? $source['date'] : '';
312 $this->assertSame($dateAsString, $date->format($dateFormat));
313 }
314 }
315
316 ?>