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\DateTimeToTimestampTransformer
;
16 class DateTimeToTimestampTransformerTest
extends DateTimeTestCase
18 public function testTransform()
20 $transformer = new DateTimeToTimestampTransformer('UTC', 'UTC');
22 $input = new \
DateTime('2010-02-03 04:05:06 UTC');
23 $output = $input->format('U');
25 $this->assertEquals($output, $transformer->transform($input));
28 public function testTransformEmpty()
30 $transformer = new DateTimeToTimestampTransformer();
32 $this->assertNull($transformer->transform(null));
35 public function testTransformWithDifferentTimezones()
37 $transformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'America/New_York');
39 $input = new \
DateTime('2010-02-03 04:05:06 America/New_York');
40 $output = $input->format('U');
41 $input->setTimezone(new \
DateTimeZone('Asia/Hong_Kong'));
43 $this->assertEquals($output, $transformer->transform($input));
46 public function testTransformFromDifferentTimezone()
48 $transformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'UTC');
50 $input = new \
DateTime('2010-02-03 04:05:06 Asia/Hong_Kong');
52 $dateTime = clone $input;
53 $dateTime->setTimezone(new \
DateTimeZone('UTC'));
54 $output = $dateTime->format('U');
56 $this->assertEquals($output, $transformer->transform($input));
59 public function testTransformExpectsDateTime()
61 $transformer = new DateTimeToTimestampTransformer();
63 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
65 $transformer->transform('1234');
68 public function testReverseTransform()
70 $reverseTransformer = new DateTimeToTimestampTransformer('UTC', 'UTC');
72 $output = new \
DateTime('2010-02-03 04:05:06 UTC');
73 $input = $output->format('U');
75 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
78 public function testReverseTransformEmpty()
80 $reverseTransformer = new DateTimeToTimestampTransformer();
82 $this->assertNull($reverseTransformer->reverseTransform(null));
85 public function testReverseTransformWithDifferentTimezones()
87 $reverseTransformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'America/New_York');
89 $output = new \
DateTime('2010-02-03 04:05:06 America/New_York');
90 $input = $output->format('U');
91 $output->setTimezone(new \
DateTimeZone('Asia/Hong_Kong'));
93 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
96 public function testReverseTransformExpectsValidTimestamp()
98 $reverseTransformer = new DateTimeToTimestampTransformer();
100 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
102 $reverseTransformer->reverseTransform('2010-2010-2010');