Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Throw exception for DateType conversion error and add tests #540

Merged
merged 2 commits into from

3 participants

@jmikola
Owner

@jwage: Please take a look at the first commit here, as it reverts behavior you originally added in b702787. I don't have a record of you following up on my comment there, but I think the behavior was incorrect.

I decided to follow up on this and add some unit tests while merging #533.

@tystr

:thumbsup: Seems correct to throw an exception in this case

@jmikola
Owner

Note: this will need rebasing if #546 is merged first. Additionally, the test class will need to be fixed to respect the private Type constructor.

@jwage
Owner

:+1:

jmikola added some commits
@jmikola jmikola Throw exception in DateType when conversion to MongoDate fails
This reverts a change introduced in b702787, which would store an ISO8601 date string on error.
c36c060
@jmikola jmikola Add unit tests for DateType 59b82e9
@jmikola jmikola merged commit 8c001be into master
@jmikola jmikola deleted the datetype-tests branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 30, 2013
  1. @jmikola

    Throw exception in DateType when conversion to MongoDate fails

    jmikola authored
    This reverts a change introduced in b702787, which would store an ISO8601 date string on error.
  2. @jmikola

    Add unit tests for DateType

    jmikola authored
This page is out of date. Refresh to see the latest.
View
8 lib/Doctrine/ODM/MongoDB/Types/DateType.php
@@ -44,14 +44,10 @@ public function convertToDatabaseValue($value)
} elseif (is_string($value)) {
$timestamp = strtotime($value);
}
- // Could not convert date to timestamp so store ISO 8601 formatted date instead
if ($timestamp === false) {
- $date = new \DateTime($value);
- return $date->format('c');
- } else {
- $value = $timestamp;
+ throw new \InvalidArgumentException(sprintf('Could not convert %s to a date value', is_scalar($value) ? '"'.$value.'"' : gettype($value)));
}
- return new \MongoDate($value);
+ return new \MongoDate($timestamp);
}
public function convertToPHPValue($value)
View
99 tests/Doctrine/ODM/MongoDB/Tests/Mapping/Types/DateTypeTest.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Doctrine\ODM\MongoDB\Tests\Mapping\Types;
+
+use Doctrine\ODM\MongoDB\Types\DateType;
+use Doctrine\ODM\MongoDB\Types\Type;
+
+class DateTypeTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConvertToDatabaseValue()
+ {
+ $type = Type::getType(Type::DATE);
+
+ $this->assertNull($type->convertToDatabaseValue(null), 'null is not converted');
+
+ $mongoDate = new \MongoDate();
+ $this->assertSame($mongoDate, $type->convertToDatabaseValue($mongoDate), 'MongoDate objects are not converted');
+
+ $yesterday = strtotime('yesterday');
+ $mongoDate = new \MongoDate($yesterday);
+ $dateTime = new \DateTime('@' . $yesterday);
+ $this->assertEquals($mongoDate, $type->convertToDatabaseValue($dateTime), 'DateTime objects are converted to MongoDate objects');
+ $this->assertEquals($mongoDate, $type->convertToDatabaseValue($yesterday), 'Numeric timestamps are converted to MongoDate objects');
+ $this->assertEquals($mongoDate, $type->convertToDatabaseValue(date('c', $yesterday)), 'String dates are converted to MongoDate objects');
+ }
+
+ /**
+ * @dataProvider provideInvalidDateValues
+ * @expectedException InvalidArgumentException
+ */
+ public function testConvertToDatabaseValueWithInvalidValues($value)
+ {
+ $type = Type::getType(Type::DATE);
+ $type->convertToDatabaseValue($value);
+ }
+
+ public function provideInvalidDateValues()
+ {
+ return array(
+ 'array' => array(array()),
+ 'string' => array('whatever'),
+ 'bool' => array(false),
+ 'object' => array(new \stdClass()),
+ );
+ }
+
+ /**
+ * @dataProvider provideDatabaseToPHPValues
+ */
+ public function testConvertToPHPValue($input, $output)
+ {
+ $type = Type::getType(Type::DATE);
+ $return = $type->convertToPHPValue($input);
+
+ $this->assertInstanceOf('DateTime', $return);
+ $this->assertTimestampEquals($output, $return);
+ }
+
+ public function testConvertToPHPValueDoesNotConvertNull()
+ {
+ $type = Type::getType(Type::DATE);
+
+ $this->assertNull($type->convertToPHPValue(null));
+ }
+
+ /**
+ * @dataProvider provideDatabaseToPHPValues
+ */
+ public function testClosureToPHP($input, $output)
+ {
+ $type = Type::getType(Type::DATE);
+ $return = null;
+
+ call_user_func(function($value) use ($type, &$return) {
+ eval($type->closureToPHP());
+ }, $input);
+
+ $this->assertInstanceOf('DateTime', $return);
+ $this->assertTimestampEquals($output, $return);
+ }
+
+ public function provideDatabaseToPHPValues()
+ {
+ $yesterday = strtotime('yesterday');
+ $mongoDate = new \MongoDate($yesterday);
+ $dateTime = new \DateTime('@' . $yesterday);
+
+ return array(
+ array($mongoDate, $dateTime),
+ array($yesterday, $dateTime),
+ array(date('c', $yesterday), $dateTime),
+ );
+ }
+
+ private function assertTimestampEquals(\DateTime $expected, \DateTime $actual)
+ {
+ $this->assertEquals($expected->getTimestamp(), $actual->getTimestamp());
+ }
+}
Something went wrong with that request. Please try again.