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\DateTimeToStringTransformer
;
16 class DateTimeToStringTransformerTest
extends DateTimeTestCase
18 public function dataProvider()
21 array('Y-m-d H:i:s', '2010-02-03 16:05:06', '2010-02-03 16:05:06 UTC'),
22 array('Y-m-d H:i:00', '2010-02-03 16:05:00', '2010-02-03 16:05:00 UTC'),
23 array('Y-m-d H:i', '2010-02-03 16:05', '2010-02-03 16:05:00 UTC'),
24 array('Y-m-d H', '2010-02-03 16', '2010-02-03 16:00:00 UTC'),
25 array('Y-m-d', '2010-02-03', '2010-02-03 00:00:00 UTC'),
26 array('Y-m', '2010-12', '2010-12-01 00:00:00 UTC'),
27 array('Y', '2010', '2010-01-01 00:00:00 UTC'),
28 array('d-m-Y', '03-02-2010', '2010-02-03 00:00:00 UTC'),
29 array('H:i:s', '16:05:06', '1970-01-01 16:05:06 UTC'),
30 array('H:i:00', '16:05:00', '1970-01-01 16:05:00 UTC'),
31 array('H:i', '16:05', '1970-01-01 16:05:00 UTC'),
32 array('H', '16', '1970-01-01 16:00:00 UTC'),
34 // different day representations
35 array('Y-m-j', '2010-02-3', '2010-02-03 00:00:00 UTC'),
36 array('z', '33', '1970-02-03 00:00:00 UTC'),
39 // this will not work as php will use actual date to replace missing info
40 // and after change of date will lookup for closest Wednesday
41 // i.e. value: 2010-02, php value: 2010-02-(today i.e. 20), parsed date: 2010-02-24
42 //array('Y-m-D', '2010-02-Wed', '2010-02-03 00:00:00 UTC'),
43 //array('Y-m-l', '2010-02-Wednesday', '2010-02-03 00:00:00 UTC'),
45 // different month representations
46 array('Y-n-d', '2010-2-03', '2010-02-03 00:00:00 UTC'),
47 array('Y-M-d', '2010-Feb-03', '2010-02-03 00:00:00 UTC'),
48 array('Y-F-d', '2010-February-03', '2010-02-03 00:00:00 UTC'),
50 // different year representations
51 array('y-m-d', '10-02-03', '2010-02-03 00:00:00 UTC'),
53 // different time representations
54 array('G:i:s', '16:05:06', '1970-01-01 16:05:06 UTC'),
55 array('g:i:s a', '4:05:06 pm', '1970-01-01 16:05:06 UTC'),
56 array('h:i:s a', '04:05:06 pm', '1970-01-01 16:05:06 UTC'),
59 array('U', '1265213106', '2010-02-03 16:05:06 UTC'),
62 // This test will fail < 5.3.9 - see https://bugs.php.net/51994
63 if (version_compare(phpversion(), '5.3.9', '>=')) {
64 $data[] = array('Y-z', '2010-33', '2010-02-03 00:00:00 UTC');
71 * @dataProvider dataProvider
73 public function testTransform($format, $output, $input)
75 $transformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);
77 $input = new \
DateTime($input);
79 $this->assertEquals($output, $transformer->transform($input));
82 public function testTransformEmpty()
84 $transformer = new DateTimeToStringTransformer();
86 $this->assertSame('', $transformer->transform(null));
89 public function testTransformWithDifferentTimezones()
91 $transformer = new DateTimeToStringTransformer('Asia/Hong_Kong', 'America/New_York', 'Y-m-d H:i:s');
93 $input = new \
DateTime('2010-02-03 12:05:06 America/New_York');
94 $output = $input->format('Y-m-d H:i:s');
95 $input->setTimezone(new \
DateTimeZone('Asia/Hong_Kong'));
97 $this->assertEquals($output, $transformer->transform($input));
100 public function testTransformExpectsDateTime()
102 $transformer = new DateTimeToStringTransformer();
104 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
106 $transformer->transform('1234');
110 * @dataProvider dataProvider
112 public function testReverseTransformUsingPipe($format, $input, $output)
114 if (version_compare(phpversion(), '5.3.7', '<')) {
115 $this->markTestSkipped('Pipe usage requires PHP 5.3.7 or newer.');
118 $reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, true);
120 $output = new \
DateTime($output);
122 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
126 * @dataProvider dataProvider
128 public function testReverseTransformWithoutUsingPipe($format, $input, $output)
130 $reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);
132 $output = new \
DateTime($output);
134 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
137 public function testReverseTransformEmpty()
139 $reverseTransformer = new DateTimeToStringTransformer();
141 $this->assertNull($reverseTransformer->reverseTransform(''));
144 public function testReverseTransformWithDifferentTimezones()
146 $reverseTransformer = new DateTimeToStringTransformer('America/New_York', 'Asia/Hong_Kong', 'Y-m-d H:i:s');
148 $output = new \
DateTime('2010-02-03 16:05:06 Asia/Hong_Kong');
149 $input = $output->format('Y-m-d H:i:s');
150 $output->setTimeZone(new \
DateTimeZone('America/New_York'));
152 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
155 public function testReverseTransformExpectsString()
157 $reverseTransformer = new DateTimeToStringTransformer();
159 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
161 $reverseTransformer->reverseTransform(1234);
164 public function testReverseTransformExpectsValidDateString()
166 $reverseTransformer = new DateTimeToStringTransformer();
168 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
170 $reverseTransformer->reverseTransform('2010-2010-2010');
173 public function testReverseTransformWithNonExistingDate()
175 $reverseTransformer = new DateTimeToStringTransformer();
177 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
179 $reverseTransformer->reverseTransform('2010-04-31');