10000 [Form] Fix for `DateTimeToStringTransformer` by stloyd · Pull Request #6440 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix for DateTimeToStringTransformer #6440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Fix for DateTimeToStringTransformer
  • Loading branch information
stloyd committed Dec 20, 2012
commit 8beee644a52e3290328e21ee3868ced31bbb1cfe
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
/**
* Whether to parse by appending a pipe "|" to the parse format.
*
* This only works as of PHP 5.3.8.
* This only works as of PHP 5.3.7.
*
* @var Boolean
*/
Expand All @@ -66,9 +66,10 @@ public function __construct($inputTimezone = null, $outputTimezone = null, $form

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

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

// See http://php.net/manual/en/datetime.createfromformat.php
Expand Down Expand Up @@ -151,7 +152,7 @@ public function reverseTransform($value)
);
}

// On PHP versions < 5.3.8 we need to emulate the pipe operator
// On PHP versions < 5.3.7 we need to emulate the pipe operator
// and reset parts not given in the format to their equivalent
// of the UNIX base timestamp.
if (!$this->parseUsingPipe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
{
public function dataProvider()
{
return array(
$data = array(
array('Y-m-d H:i:s', '2010-02-03 16:05:06', '2010-02-03 16:05:06 UTC'),
array('Y-m-d H:i:00', '2010-02-03 16:05:00', '2010-02-03 16:05:00 UTC'),
array('Y-m-d H:i', '2010-02-03 16:05', '2010-02-03 16:05:00 UTC'),
Expand All @@ -33,10 +33,12 @@ public function dataProvider()

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

// not bijective
// this will not work as php will use actual date to replace missing info
// and after change of date will lookup for closest Wednesday
// i.e. value: 2010-02, php value: 2010-02-(today i.e. 20), parsed date: 2010-02-24
//array('Y-m-D', '2010-02-Wed', '2010-02-03 00:00:00 UTC'),
//array('Y-m-l', '2010-02-Wednesday', '2010-02-03 00:00:00 UTC'),

Expand All @@ -56,6 +58,13 @@ public function dataProvider()
// seconds since unix
array('U', '1265213106', '2010-02-03 16:05:06 UTC'),
);

// This test will fail < 5.3.9 - see https://bugs.php.net/51994
if (version_compare(phpversion(), '5.3.9', '>=')) {
$data[] = array('Y-z', '2010-33', '2010-02-03 00:00:00 UTC');
}

return $data;
}

/**
Expand Down Expand Up @@ -100,8 +109,12 @@ public function testTransformExpectsDateTime()
/**
* @dataProvider dataProvider
*/
public function testReverseTransformBeforePhp538($format, $input, $output)
public function testReverseTransformUsingPipe($format, $input, $output)
{
if (version_compare(phpversion(), '5.3.7', '>=')) {
$this->markTestSkipped('Pipe usage requires PHP 5.3.7 or newer.');
}

$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);

$output = new \DateTime($output);
Expand All @@ -112,13 +125,9 @@ public function testReverseTransformBeforePhp538($format, $input, $output)
/**
* @dataProvider dataProvider
*/
public function testReverseTransformAsOfPhp538($format, $input, $output)
public function testReverseTransformWithoutUsingPipe($format, $input, $output)
{
if (version_compare(phpversion(), '5.3.8', '<')) {
$this->markTestSkipped('Requires PHP 5.3.8 or newer');
}

$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);
$reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);

$output = new \DateTime($output);

Expand Down
0