Skip to content
Permalink
Browse files

Add invalid value tests to DateTimeType::marshall()

  • Loading branch information...
markstory committed Mar 2, 2014
1 parent 8d63374 commit edd99b34b68094ef94192f7524f872495f9e9dfd
Showing with 28 additions and 2 deletions.
  1. +5 −2 src/Database/Type/DateTimeType.php
  2. +23 −0 tests/TestCase/Database/Type/DateTimeTypeTest.php
@@ -81,14 +81,17 @@ public function marshall($value) {
$date = new DateTime();
$date->setTime(0, 0, 0);
if (isset($value['year'], $value['month'], $value['day'])) {
if (
isset($value['year'], $value['month'], $value['day']) &&
(is_numeric($value['year']) & is_numeric($value['month']) && is_numeric($value['day']))
) {
$date->setDate($value['year'], $value['month'], $value['day']);
}
if (isset($value['hour'], $value['minute'])) {
if (isset($value['meridian'])) {
$value['hour'] = strtolower($value['meridian']) === 'am' ? $value['hour'] : $value['hour'] + 12;
}
$date->setTime($value['hour'], $value['minute'], $value['second']);
$date->setTime((int)$value['hour'], (int)$value['minute'], (int)$value['second']);
}
return $date;
}
@@ -111,6 +111,29 @@ public static function marshallProvider() {
],
new \DateTime('2014-02-14 13:14:15')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
],
new \DateTime('2014-02-14 00:00:00')
],
// Invalid array types
[
['year' => 'farts', 'month' => 'derp'],
new \DateTime(date('Y-m-d 00:00:00'))
],
[
['year' => 'farts', 'month' => 'derp', 'day' => 'farts'],
new \DateTime(date('Y-m-d 00:00:00'))
],
[
[
'year' => '2014', 'month' => '02', 'day' => '14',
'hour' => 'farts', 'minute' => 'farts'
],
new \DateTime('2014-02-14 00:00:00')
],
];
}

0 comments on commit edd99b3

Please sign in to comment.
You can’t perform that action at this time.