8000 [Form] Fix for `DateTimeToStringTransformer` · symfony/symfony@8beee64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8beee64

Browse files
committed
[Form] Fix for DateTimeToStringTransformer
1 parent 06e1de9 commit 8beee64

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
4242
/**
4343
* Whether to parse by appending a pipe "|" to the parse format.
4444
*
45-
* This only works as of PHP 5.3.8.
45+
* This only works as of PHP 5.3.7.
4646
*
4747
* @var Boolean
4848
*/
@@ -66,9 +66,10 @@ public function __construct($inputTimezone = null, $outputTimezone = null, $form
6666

6767
$this->generateFormat = $this->parseFormat = $format;
6868

69-
// The pipe in the parser pattern only works as of PHP 5.3.8
69+
// The pipe in the parser pattern only works as of PHP 5.3.7
70+
// See http://bugs.php.net/54316
7071
$this->parseUsingPipe = null === $parseUsingPipe
71-
? version_compare(phpversion(), '5.3.8', '>=')
72+
? version_compare(phpversion(), '5.3.7', '>=')
7273
: $parseUsingPipe;
7374

7475
// See http://php.net/manual/en/datetime.createfromformat.php
@@ -151,7 +152,7 @@ public function reverseTransform($value)
151152
);
152153
}
153154

154-
// On PHP versions < 5.3.8 we need to emulate the pipe operator
155+
// On PHP versions < 5.3.7 we need to emulate the pipe operator
155156
// and reset parts not given in the format to their equivalent
156157
// of the UNIX base timestamp.
157158
if (!$this->parseUsingPipe) {

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToStringTransformerTest.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
1717
{
1818
public function dataProvider()
1919
{
20-
return array(
20+
$data = array(
2121
array('Y-m-d H:i:s', '2010-02-03 16:05:06', '2010-02-03 16:05:06 UTC'),
2222
array('Y-m-d H:i:00', '2010-02-03 16:05:00', '2010-02-03 16:05:00 UTC'),
2323
array('Y-m-d H:i', '2010-02-03 16:05', '2010-02-03 16:05:00 UTC'),
@@ -33,10 +33,12 @@ public function dataProvider()
3333

3434
// different day representations
3535
array('Y-m-j', '2010-02-3', '2010-02-03 00:00:00 UTC'),
36-
array('Y-z', '2010-33', '2010-02-03 00:00:00 UTC'),
3736
array('z', '33', '1970-02-03 00:00:00 UTC'),
3837

3938
// 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
4042
//array('Y-m-D', '2010-02-Wed', '2010-02-03 00:00:00 UTC'),
4143
//array('Y-m-l', '2010-02-Wednesday', '2010-02-03 00:00:00 UTC'),
4244

@@ -56,6 +58,13 @@ public function dataProvider()
5658
// seconds since unix
5759
array('U', '1265213106', '2010-02-03 16:05:06 UTC'),
5860
);
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;
5968
}
6069

6170
/**
@@ -100,8 +109,12 @@ public function testTransformExpectsDateTime()
100109
/**
101110
* @dataProvider dataProvider
102111
*/
103-
public function testReverseTransformBeforePhp538($format, $input, $output)
112+
public function testReverseTransformUsingPipe($format, $input, $output)
104113
{
114+
if (version_compare(phpversion(), '5.3.7', '>=')) {
115+
$this->markTestSkipped('Pipe usage requires PHP 5.3.7 or newer.');
116+
}
117+
105118
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);
106119

107120
$output = new \DateTime($output);
@@ -112,13 +125,9 @@ public function testReverseTransformBeforePhp538($format, $input, $output)
112125
/**
113126
* @dataProvider dataProvider
114127
*/
115-
public function testReverseTransformAsOfPhp538($format, $input, $output)
128+
public function testReverseTransformWithoutUsingPipe($format, $input, $output)
116129
{
117-
if (version_compare(phpversion(), '5.3.8', '<')) {
118-
$this->markTestSkipped('Requires PHP 5.3.8 or newer');
119-
}
120-
121-
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);
130+
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);
122131

123132
$output = new \DateTime($output);
124133

0 commit comments

Comments
 (0)
0