4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer
;
14 use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer
;
15 use Symfony\Component\Intl\Util\IntlTestHelper
;
17 class DateTimeToLocalizedStringTransformerTest
extends DateTimeTestCase
20 protected $dateTimeWithoutSeconds;
22 protected function setUp()
26 // Since we test against "de_AT", we need the full implementation
27 IntlTestHelper
::requireFullIntl($this);
29 \Locale
::setDefault('de_AT');
31 $this->dateTime
= new \
DateTime('2010-02-03 04:05:06 UTC');
32 $this->dateTimeWithoutSeconds
= new \
DateTime('2010-02-03 04:05:00 UTC');
35 protected function tearDown()
37 $this->dateTime
= null;
38 $this->dateTimeWithoutSeconds
= null;
41 public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
43 if ($expected instanceof \DateTime
&& $actual instanceof \DateTime
) {
44 $expected = $expected->format('c');
45 $actual = $actual->format('c');
48 parent
::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
51 public function dataProvider()
54 array(\IntlDateFormatter
::SHORT
, null, null, '03.02.10 04:05', '2010-02-03 04:05:00 UTC'),
55 array(\IntlDateFormatter
::MEDIUM
, null, null, '03.02.2010 04:05', '2010-02-03 04:05:00 UTC'),
56 array(\IntlDateFormatter
::LONG
, null, null, '03. Februar 2010 04:05', '2010-02-03 04:05:00 UTC'),
57 array(\IntlDateFormatter
::FULL
, null, null, 'Mittwoch, 03. Februar 2010 04:05', '2010-02-03 04:05:00 UTC'),
58 array(\IntlDateFormatter
::SHORT
, \IntlDateFormatter
::NONE
, null, '03.02.10', '2010-02-03 00:00:00 UTC'),
59 array(\IntlDateFormatter
::MEDIUM
, \IntlDateFormatter
::NONE
, null, '03.02.2010', '2010-02-03 00:00:00 UTC'),
60 array(\IntlDateFormatter
::LONG
, \IntlDateFormatter
::NONE
, null, '03. Februar 2010', '2010-02-03 00:00:00 UTC'),
61 array(\IntlDateFormatter
::FULL
, \IntlDateFormatter
::NONE
, null, 'Mittwoch, 03. Februar 2010', '2010-02-03 00:00:00 UTC'),
62 array(null, \IntlDateFormatter
::SHORT
, null, '03.02.2010 04:05', '2010-02-03 04:05:00 UTC'),
63 array(null, \IntlDateFormatter
::MEDIUM
, null, '03.02.2010 04:05:06', '2010-02-03 04:05:06 UTC'),
64 array(null, \IntlDateFormatter
::LONG
, null, '03.02.2010 04:05:06 GMT', '2010-02-03 04:05:06 UTC'),
65 // see below for extra test case for time format FULL
66 array(\IntlDateFormatter
::NONE
, \IntlDateFormatter
::SHORT
, null, '04:05', '1970-01-01 04:05:00 UTC'),
67 array(\IntlDateFormatter
::NONE
, \IntlDateFormatter
::MEDIUM
, null, '04:05:06', '1970-01-01 04:05:06 UTC'),
68 array(\IntlDateFormatter
::NONE
, \IntlDateFormatter
::LONG
, null, '04:05:06 GMT', '1970-01-01 04:05:06 UTC'),
69 array(null, null, 'yyyy-MM-dd HH:mm:00', '2010-02-03 04:05:00', '2010-02-03 04:05:00 UTC'),
70 array(null, null, 'yyyy-MM-dd HH:mm', '2010-02-03 04:05', '2010-02-03 04:05:00 UTC'),
71 array(null, null, 'yyyy-MM-dd HH', '2010-02-03 04', '2010-02-03 04:00:00 UTC'),
72 array(null, null, 'yyyy-MM-dd', '2010-02-03', '2010-02-03 00:00:00 UTC'),
73 array(null, null, 'yyyy-MM', '2010-02', '2010-02-01 00:00:00 UTC'),
74 array(null, null, 'yyyy', '2010', '2010-01-01 00:00:00 UTC'),
75 array(null, null, 'dd-MM-yyyy', '03-02-2010', '2010-02-03 00:00:00 UTC'),
76 array(null, null, 'HH:mm:ss', '04:05:06', '1970-01-01 04:05:06 UTC'),
77 array(null, null, 'HH:mm:00', '04:05:00', '1970-01-01 04:05:00 UTC'),
78 array(null, null, 'HH:mm', '04:05', '1970-01-01 04:05:00 UTC'),
79 array(null, null, 'HH', '04', '1970-01-01 04:00:00 UTC'),
84 * @dataProvider dataProvider
86 public function testTransform($dateFormat, $timeFormat, $pattern, $output, $input)
88 $transformer = new DateTimeToLocalizedStringTransformer(
93 \IntlDateFormatter
::GREGORIAN
,
97 $input = new \
DateTime($input);
99 $this->assertEquals($output, $transformer->transform($input));
102 public function testTransformFullTime()
104 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter
::FULL
);
106 $this->assertEquals('03.02.2010 04:05:06 GMT', $transformer->transform($this->dateTime
));
109 public function testTransformToDifferentLocale()
111 \Locale
::setDefault('en_US');
113 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
115 $this->assertEquals('Feb 3, 2010, 4:05 AM', $transformer->transform($this->dateTime
));
118 public function testTransformEmpty()
120 $transformer = new DateTimeToLocalizedStringTransformer();
122 $this->assertSame('', $transformer->transform(null));
125 public function testTransformWithDifferentTimezones()
127 $transformer = new DateTimeToLocalizedStringTransformer('America/New_York', 'Asia/Hong_Kong');
129 $input = new \
DateTime('2010-02-03 04:05:06 America/New_York');
131 $dateTime = clone $input;
132 $dateTime->setTimezone(new \
DateTimeZone('Asia/Hong_Kong'));
134 $this->assertEquals($dateTime->format('d.m.Y H:i'), $transformer->transform($input));
137 public function testTransformWithDifferentPatterns()
139 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter
::FULL
, \IntlDateFormatter
::FULL
, \IntlDateFormatter
::GREGORIAN
, 'MM*yyyy*dd HH|mm|ss');
141 $this->assertEquals('02*2010*03 04|05|06', $transformer->transform($this->dateTime
));
145 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
147 public function testTransformRequiresValidDateTime()
149 $transformer = new DateTimeToLocalizedStringTransformer();
150 $transformer->transform('2010-01-01');
153 public function testTransformWrapsIntlErrors()
155 $transformer = new DateTimeToLocalizedStringTransformer();
159 //$this->setExpectedException('Symfony\Component\Form\Extension\Core\DataTransformer\Transdate_formationFailedException');
161 //$transformer->transform(1.5);
165 * @dataProvider dataProvider
167 public function testReverseTransform($dateFormat, $timeFormat, $pattern, $input, $output)
169 $transformer = new DateTimeToLocalizedStringTransformer(
174 \IntlDateFormatter
::GREGORIAN
,
178 $output = new \
DateTime($output);
180 $this->assertEquals($output, $transformer->reverseTransform($input));
183 public function testReverseTransformFullTime()
185 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter
::FULL
);
187 $this->assertDateTimeEquals($this->dateTime
, $transformer->reverseTransform('03.02.2010 04:05:06 GMT+00:00'));
190 public function testReverseTransformFromDifferentLocale()
192 \Locale
::setDefault('en_US');
194 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
196 $this->assertDateTimeEquals($this->dateTimeWithoutSeconds
, $transformer->reverseTransform('Feb 3, 2010, 04:05 AM'));
199 public function testReverseTransformWithDifferentTimezones()
201 $transformer = new DateTimeToLocalizedStringTransformer('America/New_York', 'Asia/Hong_Kong');
203 $dateTime = new \
DateTime('2010-02-03 04:05:00 Asia/Hong_Kong');
204 $dateTime->setTimezone(new \
DateTimeZone('America/New_York'));
206 $this->assertDateTimeEquals($dateTime, $transformer->reverseTransform('03.02.2010 04:05'));
209 public function testReverseTransformWithDifferentPatterns()
211 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter
::FULL
, \IntlDateFormatter
::FULL
, \IntlDateFormatter
::GREGORIAN
, 'MM*yyyy*dd HH|mm|ss');
213 $this->assertDateTimeEquals($this->dateTime
, $transformer->reverseTransform('02*2010*03 04|05|06'));
216 public function testReverseTransformEmpty()
218 $transformer = new DateTimeToLocalizedStringTransformer();
220 $this->assertNull($transformer->reverseTransform(''));
224 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
226 public function testReverseTransformRequiresString()
228 $transformer = new DateTimeToLocalizedStringTransformer();
229 $transformer->reverseTransform(12345);
233 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
235 public function testReverseTransformWrapsIntlErrors()
237 $transformer = new DateTimeToLocalizedStringTransformer();
238 $transformer->reverseTransform('12345');
242 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
244 public function testValidateDateFormatOption()
246 new DateTimeToLocalizedStringTransformer(null, null, 'foobar');
250 * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
252 public function testValidateTimeFormatOption()
254 new DateTimeToLocalizedStringTransformer(null, null, null, 'foobar');
258 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
260 public function testReverseTransformWithNonExistingDate()
262 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', \IntlDateFormatter
::SHORT
);
264 $this->assertDateTimeEquals($this->dateTimeWithoutSeconds
, $transformer->reverseTransform('31.04.10 04:05'));
268 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
270 public function testReverseTransformOutOfTimestampRange()
272 $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
273 $transformer->reverseTransform('1789-07-14');