Andreas Braun 10 лет назад
Родитель
Сommit
c370fc711c
2 измененных файлов с 28 добавлено и 10 удалено
  1. 19 5
      lib/Mongo/MongoWriteBatch.php
  2. 9 5
      tests/Alcaeus/MongoDbAdapter/MongoUpdateBatchTest.php

+ 19 - 5
lib/Mongo/MongoWriteBatch.php

@@ -127,13 +127,27 @@ class MongoWriteBatch
 
         switch ($this->batchType) {
             case self::COMMAND_UPDATE:
-                return [
+                $upsertedIds = [];
+                foreach ($result->getUpsertedIds() as $index => $id) {
+                    $upsertedIds[] = [
+                        'index' => $index,
+                        '_id' => TypeConverter::toLegacy($id)
+                    ];
+                }
+
+                $result = [
                     'nMatched' => $result->getMatchedCount(),
                     'nModified' => $result->getModifiedCount(),
                     'nUpserted' => $result->getUpsertedCount(),
                     'ok' => $ok,
                 ];
 
+                if (count($upsertedIds)) {
+                    $result['upserted'] = $upsertedIds;
+                }
+
+                return $result;
+
             case self::COMMAND_DELETE:
                 return [
                     'nRemoved' => $result->getDeletedCount(),
@@ -153,19 +167,19 @@ class MongoWriteBatch
         switch ($this->batchType) {
             case self::COMMAND_UPDATE:
                 if (! isset($item['q'])) {
-                    throw new Exception("Expected $item to contain 'q' key");
+                    throw new Exception("Expected \$item to contain 'q' key");
                 }
                 if (! isset($item['u'])) {
-                    throw new Exception("Expected $item to contain 'u' key");
+                    throw new Exception("Expected \$item to contain 'u' key");
                 }
                 break;
 
             case self::COMMAND_DELETE:
                 if (! isset($item['q'])) {
-                    throw new Exception("Expected $item to contain 'q' key");
+                    throw new Exception("Expected \$item to contain 'q' key");
                 }
                 if (! isset($item['limit'])) {
-                    throw new Exception("Expected $item to contain 'limit' key");
+                    throw new Exception("Expected \$item to contain 'limit' key");
                 }
                 break;
         }

+ 9 - 5
tests/Alcaeus/MongoDbAdapter/MongoUpdateBatchTest.php

@@ -65,18 +65,21 @@ class MongoUpdateBatchTest extends TestCase
 
     public function testUpsert()
     {
+        $document = ['foo' => 'foo'];
+        $this->getCollection()->insert($document);
         $batch = new \MongoUpdateBatch($this->getCollection());
 
-        $this->assertTrue($batch->add(['q' => [], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
+        $this->assertTrue($batch->add(['q' => ['foo' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
+        $this->assertTrue($batch->add(['q' => ['bar' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
 
         $expected = [
             'upserted' => [
                 [
-                    'index' => 0,
+                    'index' => 1,
                 ]
             ],
-            'nMatched' => 0,
-            'nModified' => 0,
+            'nMatched' => 1,
+            'nModified' => 1,
             'nUpserted' => 1,
             'ok' => true,
         ];
@@ -87,7 +90,8 @@ class MongoUpdateBatchTest extends TestCase
         $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']);
 
         $newCollection = $this->getCheckDatabase()->selectCollection('test');
-        $this->assertSame(1, $newCollection->count());
+        $this->assertSame(0, $newCollection->count(['foo' => 'foo']));
+        $this->assertSame(2, $newCollection->count());
         $record = $newCollection->findOne();
         $this->assertNotNull($record);
         $this->assertObjectHasAttribute('foo', $record);