Просмотр исходного кода

Ensure sec and usec properties are cast to int

Andreas Braun 9 лет назад
Родитель
Сommit
86c3582d29
3 измененных файлов с 12 добавлено и 3 удалено
  1. 2 0
      CHANGELOG-1.0.md
  2. 3 3
      lib/Mongo/MongoDate.php
  3. 7 0
      tests/Alcaeus/MongoDbAdapter/Mongo/MongoDateTest.php

+ 2 - 0
CHANGELOG-1.0.md

@@ -16,6 +16,8 @@ milestone.
  `MongoCursor::count` now always re-issues a `count` command to the server.
  * [#98](https://github.com/alcaeus/mongo-php-adapter/pull/98) fixes an error
  where using BSON types in a query projection would result in wrong results.
+ * [#99](https://github.com/alcaeus/mongo-php-adapter/pull/99) ensures that the
+ `sec` and `usec` properties for `MongoDate` are cast to int.
 
 1.0.2 (2016-04-08)
 ------------------

+ 3 - 3
lib/Mongo/MongoDate.php

@@ -50,12 +50,12 @@ class MongoDate implements TypeInterface
         } elseif ($sec instanceof UTCDateTime) {
             $msecString = (string) $sec;
 
-            $sec = (int) substr($msecString, 0, -3);
+            $sec = substr($msecString, 0, -3);
             $usec = ((int) substr($msecString, -3)) * 1000;
         }
 
-        $this->sec = $sec;
-        $this->usec = $this->truncateMicroSeconds($usec);
+        $this->sec = (int) $sec;
+        $this->usec = (int) $this->truncateMicroSeconds($usec);
     }
 
     /**

+ 7 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDateTest.php

@@ -46,6 +46,13 @@ class MongoDateTest extends TestCase
         $this->assertSame((float) $timestamp, (float) $bsonTimestamp);
     }
 
+    public function testCreateWithString()
+    {
+        $date = new \MongoDate('1234567890', '123456');
+        $this->assertAttributeSame(1234567890, 'sec', $date);
+        $this->assertAttributeSame(123000, 'usec', $date);
+    }
+
     public function testCreateWithBsonDate()
     {
         $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoDate')));