Skip to content
Permalink
Browse files

Add tests for invalid data.

  • Loading branch information...
markstory committed Jan 28, 2014
1 parent d4bf1d0 commit 5ee6ff100d9ba15c90ef6eaa6a8ac911e1200b69
Showing with 49 additions and 13 deletions.
  1. +17 −13 src/View/Input/DateTime.php
  2. +32 −0 tests/TestCase/View/Input/DateTimeTest.php
@@ -186,20 +186,24 @@ protected function _deconstructDate($value, $options) {
'meridian' => '',
];
}
if (is_string($value)) {
$date = new \DateTime($value);
} elseif (is_int($value)) {
$date = new \DateTime('@' . $value);
} elseif (is_array($value)) {
$date = new \DateTime();
if (isset($value['year'], $value['month'], $value['day'])) {
$date->setDate($value['year'], $value['month'], $value['day']);
}
if (isset($value['hour'], $value['minute'], $value['second'])) {
$date->setTime($value['hour'], $value['minute'], $value['second']);
try {
if (is_string($value)) {
$date = new \DateTime($value);
} elseif (is_int($value)) {
$date = new \DateTime('@' . $value);
} elseif (is_array($value)) {
$date = new \DateTime();
if (isset($value['year'], $value['month'], $value['day'])) {
$date->setDate($value['year'], $value['month'], $value['day']);
}
if (isset($value['hour'], $value['minute'], $value['second'])) {
$date->setTime($value['hour'], $value['minute'], $value['second']);
}
} else {
$date = clone $value;
}
} else {
$date = clone $value;
} catch (\Exception $e) {
$date = new \DateTime();
}
if (isset($options['minute']['interval'])) {
@@ -42,6 +42,38 @@ public function setUp() {
$this->DateTime = new DateTime($this->templates, $this->selectBox);
}
/**
* Data provider for testing various types of invalid selected values.
*
* @return array
*/
public static function invalidSelectedValuesProvider() {
$date = new \DateTime('2014-01-20 12:30:45');
return [
'string' => ['Bag of poop'],
'int' => [-1],
'array' => [[
'derp' => 'hurt'
]]
];
}
/**
* test rendering selected values.
*
* @dataProvider selectedValuesProvider
* @return void
*/
public function testRenderSelectedInvalid($selected) {
$result = $this->DateTime->render(['val' => $selected]);
$now = new \DateTime();
$format = '<option value="%s" selected="selected">%s</option>';
$this->assertContains(
sprintf($format, $now->format('Y'), $now->format('Y')),
$result
);
}
/**
* Data provider for testing various acceptable selected values.
*

0 comments on commit 5ee6ff1

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