]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer; | |
13 | ||
14 | use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer; | |
15 | ||
16 | class DateTimeToTimestampTransformerTest extends DateTimeTestCase | |
17 | { | |
18 | public function testTransform() | |
19 | { | |
20 | $transformer = new DateTimeToTimestampTransformer('UTC', 'UTC'); | |
21 | ||
22 | $input = new \DateTime('2010-02-03 04:05:06 UTC'); | |
23 | $output = $input->format('U'); | |
24 | ||
25 | $this->assertEquals($output, $transformer->transform($input)); | |
26 | } | |
27 | ||
28 | public function testTransformEmpty() | |
29 | { | |
30 | $transformer = new DateTimeToTimestampTransformer(); | |
31 | ||
32 | $this->assertNull($transformer->transform(null)); | |
33 | } | |
34 | ||
35 | public function testTransformWithDifferentTimezones() | |
36 | { | |
37 | $transformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'America/New_York'); | |
38 | ||
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')); | |
42 | ||
43 | $this->assertEquals($output, $transformer->transform($input)); | |
44 | } | |
45 | ||
46 | public function testTransformFromDifferentTimezone() | |
47 | { | |
48 | $transformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'UTC'); | |
49 | ||
50 | $input = new \DateTime('2010-02-03 04:05:06 Asia/Hong_Kong'); | |
51 | ||
52 | $dateTime = clone $input; | |
53 | $dateTime->setTimezone(new \DateTimeZone('UTC')); | |
54 | $output = $dateTime->format('U'); | |
55 | ||
56 | $this->assertEquals($output, $transformer->transform($input)); | |
57 | } | |
58 | ||
59 | public function testTransformExpectsDateTime() | |
60 | { | |
61 | $transformer = new DateTimeToTimestampTransformer(); | |
62 | ||
63 | $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException'); | |
64 | ||
65 | $transformer->transform('1234'); | |
66 | } | |
67 | ||
68 | public function testReverseTransform() | |
69 | { | |
70 | $reverseTransformer = new DateTimeToTimestampTransformer('UTC', 'UTC'); | |
71 | ||
72 | $output = new \DateTime('2010-02-03 04:05:06 UTC'); | |
73 | $input = $output->format('U'); | |
74 | ||
75 | $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input)); | |
76 | } | |
77 | ||
78 | public function testReverseTransformEmpty() | |
79 | { | |
80 | $reverseTransformer = new DateTimeToTimestampTransformer(); | |
81 | ||
82 | $this->assertNull($reverseTransformer->reverseTransform(null)); | |
83 | } | |
84 | ||
85 | public function testReverseTransformWithDifferentTimezones() | |
86 | { | |
87 | $reverseTransformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'America/New_York'); | |
88 | ||
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')); | |
92 | ||
93 | $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input)); | |
94 | } | |
95 | ||
96 | public function testReverseTransformExpectsValidTimestamp() | |
97 | { | |
98 | $reverseTransformer = new DateTimeToTimestampTransformer(); | |
99 | ||
100 | $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException'); | |
101 | ||
102 | $reverseTransformer->reverseTransform('2010-2010-2010'); | |
103 | } | |
104 | } |