]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToStringTransformerTest.php
987df1d81c88881c1ec463d2115981979ace36d1
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Core / DataTransformer / DateTimeToStringTransformerTest.php
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\DateTimeToStringTransformer;
15
16 class DateTimeToStringTransformerTest extends DateTimeTestCase
17 {
18 public function dataProvider()
19 {
20 $data = array(
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'),
33
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'),
37
38 // not bijective
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'),
44
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'),
49
50 // different year representations
51 array('y-m-d', '10-02-03', '2010-02-03 00:00:00 UTC'),
52
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'),
57
58 // seconds since unix
59 array('U', '1265213106', '2010-02-03 16:05:06 UTC'),
60 );
61
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');
65 }
66
67 return $data;
68 }
69
70 /**
71 * @dataProvider dataProvider
72 */
73 public function testTransform($format, $output, $input)
74 {
75 $transformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);
76
77 $input = new \DateTime($input);
78
79 $this->assertEquals($output, $transformer->transform($input));
80 }
81
82 public function testTransformEmpty()
83 {
84 $transformer = new DateTimeToStringTransformer();
85
86 $this->assertSame('', $transformer->transform(null));
87 }
88
89 public function testTransformWithDifferentTimezones()
90 {
91 $transformer = new DateTimeToStringTransformer('Asia/Hong_Kong', 'America/New_York', 'Y-m-d H:i:s');
92
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'));
96
97 $this->assertEquals($output, $transformer->transform($input));
98 }
99
100 public function testTransformExpectsDateTime()
101 {
102 $transformer = new DateTimeToStringTransformer();
103
104 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
105
106 $transformer->transform('1234');
107 }
108
109 /**
110 * @dataProvider dataProvider
111 */
112 public function testReverseTransformUsingPipe($format, $input, $output)
113 {
114 if (version_compare(phpversion(), '5.3.7', '<')) {
115 $this->markTestSkipped('Pipe usage requires PHP 5.3.7 or newer.');
116 }
117
118 $reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, true);
119
120 $output = new \DateTime($output);
121
122 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
123 }
124
125 /**
126 * @dataProvider dataProvider
127 */
128 public function testReverseTransformWithoutUsingPipe($format, $input, $output)
129 {
130 $reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);
131
132 $output = new \DateTime($output);
133
134 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
135 }
136
137 public function testReverseTransformEmpty()
138 {
139 $reverseTransformer = new DateTimeToStringTransformer();
140
141 $this->assertNull($reverseTransformer->reverseTransform(''));
142 }
143
144 public function testReverseTransformWithDifferentTimezones()
145 {
146 $reverseTransformer = new DateTimeToStringTransformer('America/New_York', 'Asia/Hong_Kong', 'Y-m-d H:i:s');
147
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'));
151
152 $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
153 }
154
155 public function testReverseTransformExpectsString()
156 {
157 $reverseTransformer = new DateTimeToStringTransformer();
158
159 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
160
161 $reverseTransformer->reverseTransform(1234);
162 }
163
164 public function testReverseTransformExpectsValidDateString()
165 {
166 $reverseTransformer = new DateTimeToStringTransformer();
167
168 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
169
170 $reverseTransformer->reverseTransform('2010-2010-2010');
171 }
172
173 public function testReverseTransformWithNonExistingDate()
174 {
175 $reverseTransformer = new DateTimeToStringTransformer();
176
177 $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
178
179 $reverseTransformer->reverseTransform('2010-04-31');
180 }
181 }