فهرست منبع

Ensure correct return types in MongoCollection

Andreas Braun 10 سال پیش
والد
کامیت
06d38b9398
2فایلهای تغییر یافته به همراه83 افزوده شده و 7 حذف شده
  1. 26 7
      lib/Mongo/MongoCollection.php
  2. 57 0
      tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

+ 26 - 7
lib/Mongo/MongoCollection.php

@@ -354,10 +354,25 @@ class MongoCollection
      */
     public function remove(array $criteria = [], array $options = [])
     {
-        $multiple = isset($options['justOne']) ? !$options['justOne'] : false;
+        $multiple = isset($options['justOne']) ? !$options['justOne'] : true;
         $method = $multiple ? 'deleteMany' : 'deleteOne';
 
-        return $this->collection->$method($criteria, $options);
+        /** @var \MongoDB\DeleteResult $result */
+        $result = $this->collection->$method(
+            TypeConverter::convertLegacyArrayToObject($criteria),
+            $this->convertWriteConcernOptions($options)
+        );
+
+        if (! $result->isAcknowledged()) {
+            return true;
+        }
+
+        return [
+            'ok' => 1.0,
+            'n' => $result->getDeletedCount(),
+            'err' => null,
+            'errmsg' => null
+        ];
     }
 
     /**
@@ -370,7 +385,7 @@ class MongoCollection
      */
     public function find(array $query = [], array $fields = [])
     {
-        $cursor = new MongoCursor($this->db->getConnection(), (string)$this, $query, $fields);
+        $cursor = new MongoCursor($this->db->getConnection(), (string) $this, $query, $fields);
         $cursor->setReadPreference($this->getReadPreference());
 
         return $cursor;
@@ -430,11 +445,14 @@ class MongoCollection
      * @link http://www.php.net/manual/en/mongocollection.findone.php
      * @param array $query The fields for which to search.
      * @param array $fields Fields of the results to return.
+     * @param array $options
      * @return array|null
      */
-    public function findOne(array $query = [], array $fields = [])
+    public function findOne(array $query = [], array $fields = [], array $options = [])
     {
-        $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query), ['projection' => $fields]);
+        $options = ['projection' => $fields] + $options;
+
+        $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query), $options);
         if ($document !== null) {
             $document = TypeConverter::convertObjectToLegacyArray($document);
         }
@@ -531,11 +549,12 @@ class MongoCollection
      * Counts the number of documents in this collection
      * @link http://www.php.net/manual/en/mongocollection.count.php
      * @param array|stdClass $query
+     * @param array $options
      * @return int Returns the number of documents matching the query.
      */
-    public function count($query = [])
+    public function count($query = [], array $options = [])
     {
-        return $this->collection->count($query);
+        return $this->collection->count(TypeConverter::convertLegacyArrayToObject($query), $options);
     }
 
     /**

+ 57 - 0
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -76,6 +76,8 @@ class MongoCollectionTest extends TestCase
 
         $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
         $this->assertSame($expected, $result);
+
+        $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
     }
 
     public function testUpdateMany()
@@ -94,6 +96,61 @@ class MongoCollectionTest extends TestCase
 
         $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
         $this->assertSame($expected, $result);
+
+        $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
+    }
+
+    public function testUnacknowledgedUpdate()
+    {
+        $this->getCollection()->insert(['foo' => 'bar']);
+        $this->getCollection()->insert(['foo' => 'bar']);
+
+        $this->assertTrue($this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']], ['w' => 0]));
+    }
+
+    public function testRemoveMultiple()
+    {
+        $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
+        $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
+        $this->getCollection()->insert(['change' => true, 'foo' => 'foo']);
+        $expected = [
+            'ok' => 1.0,
+            'n' => 2,
+            'err' => null,
+            'errmsg' => null,
+        ];
+
+        $result = $this->getCollection()->remove(['foo' => 'bar']);
+        $this->assertSame($expected, $result);
+
+        $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
+    }
+
+    public function testRemoveSingle()
+    {
+        $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
+        $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
+        $this->getCollection()->insert(['change' => true, 'foo' => 'foo']);
+        $expected = [
+            'ok' => 1.0,
+            'n' => 1,
+            'err' => null,
+            'errmsg' => null,
+        ];
+
+        $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
+        $this->assertSame($expected, $result);
+
+        $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
+    }
+
+    public function testRemoveUnacknowledged()
+    {
+        $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
+        $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
+        $this->getCollection()->insert(['change' => true, 'foo' => 'foo']);
+
+        $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
     }
 
     public function testFindReturnsCursor()