From 79b10d48a14d62e11fcd17668de581e299837d10 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Sat, 3 Dec 2016 18:27:56 +0100 Subject: [PATCH] Fix microsecond usage in tests --- .../DocumentPersisterGetShardKeyQueryTest.php | 4 +++- tests/Doctrine/ODM/MongoDB/Tests/Types/TypeTest.php | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Persisters/DocumentPersisterGetShardKeyQueryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Persisters/DocumentPersisterGetShardKeyQueryTest.php index f1c58cf459..0e525a791c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Persisters/DocumentPersisterGetShardKeyQueryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Persisters/DocumentPersisterGetShardKeyQueryTest.php @@ -50,7 +50,9 @@ public function testGetShardKeyQueryObjects() $this->assertInstanceOf('MongoDate', $shardKeyQuery['date']); $this->assertSame($o->date->getTimestamp(), $shardKeyQuery['date']->sec); - $this->assertSame(0, $shardKeyQuery['date']->usec); + + $microseconds = (int)floor(((int)$o->date->format('u')) / 1000) * 1000; + $this->assertSame($microseconds, $shardKeyQuery['date']->usec); } public function testShardById() diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Types/TypeTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Types/TypeTest.php index 04c20e9822..cbd3fe48cd 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Types/TypeTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Types/TypeTest.php @@ -28,7 +28,6 @@ public function provideTypes() array(Type::getType(Type::INTEGER), 42), array(Type::getType(Type::FLOAT), 3.14), array(Type::getType(Type::STRING), 'ohai'), - array(Type::getType(Type::DATE), new \DateTime()), array(Type::getType(Type::KEY), 0), array(Type::getType(Type::KEY), 1), array(Type::getType(Type::TIMESTAMP), time()), @@ -74,6 +73,18 @@ public function provideTypesForIdempotent() ); } + public function testConvertDatePreservesMilliseconds() + { + $date = new \DateTime(); + $expectedDate = clone $date; + + $cleanMicroseconds = (int) floor(((int) $date->format('u')) / 1000) * 1000; + $expectedDate->modify($date->format('H:i:s') . '.' . $cleanMicroseconds); + + $type = Type::getType(Type::DATE); + $this->assertEquals($expectedDate, $type->convertToPHPValue($type->convertToDatabaseValue($date))); + } + public function testConvertImmutableDate() { $date = new \DateTimeImmutable('now');