Commit f64135cd authored by Bas de Nooijer's avatar Bas de Nooijer

- fixed several formatDate unittests that where failing

- corrected the input format for the datetime objects (prefix timestamps with @)
- fixed timezone issue by forcing UTC
- refactored formatDate into a single function and without duplicate date objects
parent d7a64a59
...@@ -131,51 +131,58 @@ class Solarium_Query_Helper ...@@ -131,51 +131,58 @@ class Solarium_Query_Helper
* *
* @see http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html * @see http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
* *
* @param mixed $input accepted formats: timestamp, date string or DateTime * @param int|string|DateTime $input accepted formats: timestamp, date string or DateTime
* @return string or false when input is invalid * @return string|false false is returned in case of invalid input
*/ */
public function formatDate($input) public function formatDate($input)
{ {
switch(true){
switch(true) {
case is_numeric($input) && $this->_isTimestamp($input):
$dateTime = new DateTime($input);
break;
case is_string($input) && $this->_isTimestamp(strtotime($input)): // input of datetime object
$dateTime = new DateTime(strtotime($input)); case $input instanceof DateTime:
// no work needed
break; break;
case $input instanceof DateTime:
$dateTime = $input; // input of timestamp or date/time string
case is_string($input) || is_numeric($input):
// if date/time string: convert to timestamp first
if (is_string($input)) $input = strtotime($input);
// now try converting the timestamp to a datetime instance, on failure return false
try {
$input = new DateTime('@' . $input);
} catch (Exception $e) {
$input = false;
}
break; break;
//if input does not match any of these requirements then fail
// any other input formats can be added in additional cases here...
// case $input instanceof Zend_Date:
// unsupported input format
default: default:
return false; $input = false;
break;
} }
$iso8601 = $dateTime->format(DateTime::ISO8601);
// handle the filtered input
if ($input) {
// when we get here the input is always a datetime object
$input->setTimezone(new DateTimeZone('UTC'));
$iso8601 = $input->format(DateTime::ISO8601);
$iso8601 = strstr($iso8601, '+', true); //strip timezone $iso8601 = strstr($iso8601, '+', true); //strip timezone
$iso8601 .= 'Z'; $iso8601 .= 'Z';
return $iso8601; return $iso8601;
} } else {
// unsupported input
/**
* Validate if date is valid
* note: do not use checkdate() and support negative timestamps
*
* @return boolean
*/
protected function _isTimestamp($timestamp)
{
try {
new DateTime($timestamp);
} catch (Exception $e) {
return false; return false;
} }
return true;
} }
/** /**
......
...@@ -31,7 +31,15 @@ ...@@ -31,7 +31,15 @@
class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
{ {
protected $_helper, $_query; /**
* @var Solarium_Query_Helper
*/
protected $_helper;
/**
* @var Solarium_Query_Select
*/
protected $_query;
public function setUp() public function setUp()
{ {
...@@ -189,21 +197,21 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase ...@@ -189,21 +197,21 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
{ {
$this->assertFalse( $this->assertFalse(
$this->_helper->formatDate(strtotime('2011---')), $this->_helper->formatDate(strtotime('2011---')),
'Expects invalid strtotime/timestamp input (false) not to be accpted' 'Expects invalid strtotime/timestamp input (false) not to be accepted'
); );
//allow negative dates. //allow negative dates.
$this->assertNotEquals( $this->assertNotEquals(
false, false,
$this->_helper->formatDate(strtotime(strtotime('2011-10-01'))), $this->_helper->formatDate(strtotime('2011-10-01')),
'Expects negative timestamp input to be accpted' 'Expects negative timestamp input to be accepted'
); );
//@todo find out if we need to any test for php versions / platforms which do not support negative timestamp //@todo find out if we need to any test for php versions / platforms which do not support negative timestamp
$this->assertFalse( $this->assertFalse(
$this->_helper->formatDate(strtotime('2010-31-02')), $this->_helper->formatDate(strtotime('2010-31-02')),
'Expects invalid timestamp input (not in calendar) not to be accpted' 'Expects invalid timestamp input (not in calendar) not to be accepted'
); );
$this->assertEquals( $this->assertEquals(
...@@ -216,19 +224,20 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase ...@@ -216,19 +224,20 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
public function testFormatDateInputString() public function testFormatDateInputString()
{ {
$this->assertFalse( $this->assertFalse(
$this->_helper->formatDate(date('Y-m-d', strtotime('2011-11-31'))), $this->_helper->formatDate('2011-13-31'),
'Expects an invalid date string input not to be accepted' 'Expects an invalid date string input not to be accepted'
); );
$this->assertEquals( $this->assertEquals(
$this->_mockFormatDateOutput(strtotime('2011-10-01')), $this->_mockFormatDateOutput(strtotime('2011-10-01')),
$this->_helper->formatDate(date('Y-m-d', strtotime('2011-10-01'))), $this->_helper->formatDate('2011-10-01'),
'Expects formatDate with String input to output ISO8601 with stripped timezone' 'Expects formatDate with String input to output ISO8601 with stripped timezone'
); );
} }
public function testFormatDateInputDateTime() public function testFormatDateInputDateTime()
{ {
date_default_timezone_set("UTC"); // prevent timezone differences
$this->assertFalse( $this->assertFalse(
$this->_helper->formatDate(new stdClass()), $this->_helper->formatDate(new stdClass()),
...@@ -237,7 +246,7 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase ...@@ -237,7 +246,7 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
$this->assertEquals( $this->assertEquals(
$this->_mockFormatDateOutput(strtotime('2011-10-01')), $this->_mockFormatDateOutput(strtotime('2011-10-01')),
$this->_helper->formatDate(new DateTime(strtotime('2011-10-01'))), $this->_helper->formatDate(new DateTime('2011-10-01')),
'Expects formatDate with DateTime input to output ISO8601 with stripped timezone' 'Expects formatDate with DateTime input to output ISO8601 with stripped timezone'
); );
} }
...@@ -257,7 +266,7 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase ...@@ -257,7 +266,7 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
protected function _mockFormatDateOutput($timestamp) protected function _mockFormatDateOutput($timestamp)
{ {
$date = new DateTime($timestamp); $date = new DateTime('@'.$timestamp);
return strstr($date->format(DateTime::ISO8601), '+', true) . 'Z'; return strstr($date->format(DateTime::ISO8601), '+', true) . 'Z';
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment