Bladeren bron

Merge pull request #122 from alcaeus/find-and-modify-operators

Fix findAndModify when specifying update param and option
Andreas 9 jaren geleden
bovenliggende
commit
db7ef240fc
3 gewijzigde bestanden met toevoegingen van 33 en 1 verwijderingen
  1. 3 0
      CHANGELOG-1.0.md
  2. 1 1
      lib/Mongo/MongoCollection.php
  3. 29 0
      tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

+ 3 - 0
CHANGELOG-1.0.md

@@ -14,6 +14,9 @@ milestone.
  flag to indexes when calling `MongoCollection::getIndexInfo`.
  * [#120](https://github.com/alcaeus/mongo-php-adapter/pull/120) throws the proper
  `MongoWriteConcernException` when encountering bulk write errors.
+ * [#122](https://github.com/alcaeus/mongo-php-adapter/pull/122) fixes an error in
+ `MongoCollection::findAndModify` when specifying both the `update` parameter as
+ well as the `update` option.
 
 1.0.4 (2016-06-22)
 ------------------

+ 1 - 1
lib/Mongo/MongoCollection.php

@@ -503,7 +503,7 @@ class MongoCollection
             } else {
                 $update = is_array($update) ? $update : [];
                 if (isset($options['update']) && is_array($options['update'])) {
-                    $update = array_merge($update, $options['update']);
+                    $update = $options['update'];
                     unset($options['update']);
                 }
 

+ 29 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -1254,6 +1254,35 @@ class MongoCollectionTest extends TestCase
         $this->assertObjectNotHasAttribute('foo', $object);
     }
 
+    public function testFindAndModifyWithUpdateParamAndOption()
+    {
+        $id = '54203e08d51d4a1f868b456e';
+        $collection = $this->getCollection();
+
+        $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
+        $collection->insert($document);
+
+        $data = ['foo' => 'foo', 'bar' => 'bar'];
+
+        $this->getCollection()->findAndModify(
+            ['_id' => new \MongoId($id)],
+            [$data],
+            [],
+            [
+                'update' => ['$set' => ['foo' => 'foobar']],
+                'upsert' => true,
+            ]
+        );
+
+        $newCollection = $this->getCheckDatabase()->selectCollection('test');
+        $this->assertSame(1, $newCollection->count());
+        $object = $newCollection->findOne();
+
+        $this->assertNotNull($object);
+        $this->assertAttributeSame('foobar', 'foo', $object);
+        $this->assertObjectNotHasAttribute('bar', $object);
+    }
+
     public function testFindAndModifyUpdateReplace()
     {
         $id = '54203e08d51d4a1f868b456e';