Skip to content

Commit

Permalink
[Form] Fix for DateTimeToStringTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Dec 20, 2012
1 parent 06e1de9 commit 8beee64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
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
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 comments on commit 8beee64

Please sign in to comment.