Skip to content

Commit

Permalink
Fixing array to string conversion for dates. Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 4, 2015
1 parent 73f8b6d commit 8fcf95d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Validation/Validation.php
Expand Up @@ -987,7 +987,9 @@ protected static function _getDateString($value) {
$value['hour'] = strtolower($value['meridian']) === 'am' ? $value['hour'] : $value['hour'] + 12;
}
$value += ['minute' => 0, 'second' => 0];
$formatted .= sprintf('%02d:%02d:%02d', $value['hour'], $value['minute'], $value['second']);
if (is_numeric($value['hour']) && is_numeric($value['minute']) && is_numeric($value['second'])) {
$formatted .= sprintf('%02d:%02d:%02d', $value['hour'], $value['minute'], $value['second']);
}
}

return trim($formatted);
Expand Down
35 changes: 35 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -1482,6 +1482,41 @@ public function testDateY()
$this->assertFalse(Validation::date('1799', ['y']));
}

/**
* test date validation when passing an array
*
* @return void
*/
public function testDateArray() {
$date = ['year' => 2014, 'month' => 2, 'day' => 14];
$this->assertTrue(Validation::date($date));
$date = ['year' => 'farts', 'month' => 'derp', 'day' => 'farts'];
$this->assertFalse(Validation::date($date));
}

/**
* test datetime validation when passing an array
*
* @return void
*/
public function testDateTimeArray() {
$date = ['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15];
$this->assertTrue(Validation::datetime($date));

$date = [
'year' => 2014, 'month' => 2, 'day' => 14,
'hour' => 1, 'minute' => 14, 'second' => 15,
'meridian' => 'am'
];
$this->assertTrue(Validation::datetime($date));

$date = [
'year' => '2014', 'month' => '02', 'day' => '14',
'hour' => 'farts', 'minute' => 'farts'
];
$this->assertFalse(Validation::datetime($date));
}

/**
* Test validating dates with multiple formats
*
Expand Down

0 comments on commit 8fcf95d

Please sign in to comment.