Преглед на файлове

Use MongoDBRef class for references

Andreas Braun преди 10 години
родител
ревизия
e9082aef2a
променени са 2 файла, в които са добавени 51 реда и са изтрити 9 реда
  1. 10 9
      lib/Mongo/MongoDB.php
  2. 41 0
      tests/Alcaeus/MongoDbAdapter/MongoDBRefTest.php

+ 10 - 9
lib/Mongo/MongoDB.php

@@ -306,9 +306,14 @@ class MongoDB
      */
     public function createDBRef($collection, $document_or_id)
     {
-        if (is_object($document_or_id)) {
-            $id = isset($document_or_id->_id) ? $document_or_id->_id : null;
-//            $id = $document_or_id->_id ?? null;
+        if ($document_or_id instanceof \MongoId) {
+            $id = $document_or_id;
+        } elseif (is_object($document_or_id)) {
+            if (! isset($document_or_id->_id)) {
+                return null;
+            }
+
+            $id = $document_or_id->_id;
         } elseif (is_array($document_or_id)) {
             if (! isset($document_or_id['_id'])) {
                 return null;
@@ -319,11 +324,7 @@ class MongoDB
             $id = $document_or_id;
         }
 
-        return [
-            '$ref' => $collection,
-            '$id' => $id,
-            '$db' => $this->name,
-        ];
+        return MongoDBRef::create($collection, $id, $this->name);
     }
 
 
@@ -336,7 +337,7 @@ class MongoDB
      */
     public function getDBRef(array $ref)
     {
-        $this->notImplemented();
+        return MongoDBRef::get($this, $ref);
     }
 
     /**

+ 41 - 0
tests/Alcaeus/MongoDbAdapter/MongoDBRefTest.php

@@ -22,6 +22,33 @@ class MongoDBRefTest extends TestCase
     }
 
     /**
+     * @dataProvider dataCreateThroughMongoDB
+     */
+    public function testCreateThroughMongoDB($expected, $document_or_id)
+    {
+        $ref = $this->getDatabase()->createDBRef('test', $document_or_id);
+
+        $this->assertEquals($expected, $ref);
+    }
+
+    public static function dataCreateThroughMongoDB()
+    {
+        $id = new \MongoId();
+        $validRef = ['$ref' => 'test', '$id' => $id, '$db' => 'mongo-php-adapter'];
+
+        $object = new \stdClass();
+        $object->_id = $id;
+
+        return [
+            'simpleId' => [$validRef, $id],
+            'arrayWithIdProperty' => [$validRef, ['_id' => $id]],
+            'objectWithIdProperty' => [$validRef, $object],
+            'arrayWithoutId' => [null, []],
+            'objectWithoutId' => [null, new \stdClass()],
+        ];
+    }
+
+    /**
      * @dataProvider dataIsRef
      */
     public function testIsRef($expected, $ref)
@@ -59,6 +86,20 @@ class MongoDBRefTest extends TestCase
         $this->assertEquals($document, $fetchedRef);
     }
 
+    public function testGetThroughMongoDB()
+    {
+        $id = new \MongoId();
+
+        $db = $this->getDatabase();
+
+        $document = ['_id' => $id, 'foo' => 'bar'];
+        $db->selectCollection('test')->insert($document);
+
+        $fetchedRef = $db->getDBRef(['$ref' => 'test', '$id' => $id]);
+        $this->assertInternalType('array', $fetchedRef);
+        $this->assertEquals($document, $fetchedRef);
+    }
+
     public function testGetWithNonExistingDocument()
     {
         $db = $this->getDatabase();