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

MongoDB\BSON\Timestamp and MongoTimestamp ctor arg order differs

Jeremy Mikola 10 лет назад
Родитель
Сommit
5997aa47a8

+ 4 - 4
lib/Mongo/MongoTimestamp.php

@@ -52,10 +52,10 @@ class MongoTimestamp implements TypeInterface
     public function __construct($sec = 0, $inc = 0)
     {
         if ($sec instanceof Timestamp) {
-            // Only way is to convert is from string: [<sec>:<inc>]
+            // Only way is to convert is from string: [<inc>:<sec>]
             $parts = explode(':', substr((string) $sec, 1, -1));
-            $this->sec = (int) $parts[0];
-            $this->inc = (int) $parts[1];
+            $this->sec = (int) $parts[1];
+            $this->inc = (int) $parts[0];
 
             return;
         }
@@ -89,6 +89,6 @@ class MongoTimestamp implements TypeInterface
      */
     public function toBSONType()
     {
-        return new Timestamp($this->sec, $this->inc);
+        return new Timestamp($this->inc, $this->sec);
     }
 }

+ 15 - 2
tests/Alcaeus/MongoDbAdapter/Mongo/MongoTimestampTest.php

@@ -30,7 +30,7 @@ class MongoTimestampTest extends TestCase
 
         $bsonTimestamp = $timestamp->toBSONType();
         $this->assertInstanceOf('MongoDB\BSON\Timestamp', $bsonTimestamp);
-        $this->assertSame('[1234567890:987654321]', (string) $bsonTimestamp);
+        $this->assertSame('[987654321:1234567890]', (string) $bsonTimestamp);
     }
 
     public function testCreateWithGlobalInc()
@@ -46,10 +46,23 @@ class MongoTimestampTest extends TestCase
     {
         $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoTimestamp')));
 
-        $bsonTimestamp = new \MongoDB\BSON\Timestamp(1234567890, 987654321);
+        $bsonTimestamp = new \MongoDB\BSON\Timestamp(987654321, 1234567890);
         $timestamp = new \MongoTimestamp($bsonTimestamp);
 
         $this->assertAttributeSame(1234567890, 'sec', $timestamp);
         $this->assertAttributeSame(987654321, 'inc', $timestamp);
     }
+
+    public function testContructorArgumentOrderDiffers()
+    {
+        $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoTimestamp')));
+
+        /* The legacy MongoTimestamp's constructor takes seconds before the
+         * increment, while MongoDB\BSON\Timestamp takes the increment first.
+         */
+        $bsonTimestamp = new \MongoDB\BSON\Timestamp(12345, 67890);
+        $timestamp = new \MongoTimestamp(67890, 12345);
+
+        $this->assertSame((string) $bsonTimestamp, (string)$timestamp->toBSONType());
+    }
 }