Parcourir la source

Merge pull request #261 from alcaeus/fix-cursorinterface-inheritance

Fix cursor interface inheritance
Andreas Braun il y a 6 ans
Parent
commit
289478acf5

+ 1 - 1
lib/Mongo/MongoCursor.php

@@ -28,7 +28,7 @@ use MongoDB\Operation\Find;
  * Result object for database query.
  * @link http://www.php.net/manual/en/class.mongocursor.php
  */
-class MongoCursor extends AbstractCursor implements Iterator, Countable
+class MongoCursor extends AbstractCursor implements Iterator, Countable, MongoCursorInterface
 {
     /**
      * @var bool

+ 1 - 1
lib/Mongo/MongoGridFSCursor.php

@@ -17,7 +17,7 @@ if (class_exists('MongoGridFSCursor', false)) {
     return;
 }
 
-class MongoGridFSCursor extends MongoCursor
+class MongoGridFSCursor extends MongoCursor implements Countable
 {
     /**
      * @static

+ 9 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php

@@ -2,6 +2,7 @@
 
 namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
 
+use MongoCursorInterface;
 use MongoDB\Database;
 use MongoDB\Driver\ReadPreference;
 use Alcaeus\MongoDbAdapter\Tests\TestCase;
@@ -202,4 +203,12 @@ class MongoCommandCursorTest extends TestCase
             ],
         ];
     }
+
+    public function testInterfaces()
+    {
+        $this->prepareData();
+        $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
+
+        $this->assertInstanceOf(MongoCursorInterface::class, $cursor);
+    }
 }

+ 15 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

@@ -4,6 +4,8 @@ namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
 
 use Alcaeus\MongoDbAdapter\Tests\TestCase;
 use Alcaeus\MongoDbAdapter\TypeConverter;
+use Countable;
+use MongoCursorInterface;
 use MongoDB\Driver\ReadPreference;
 use MongoDB\Model\BSONDocument;
 use MongoDB\Operation\Find;
@@ -516,6 +518,19 @@ class MongoCursorTest extends TestCase
         $this->assertArraySubset($expected, $cursor->explain());
     }
 
+    public function testInterfaces()
+    {
+        $collection = $this->getCollection();
+        $cursor = $collection->find();
+
+        $this->assertInstanceOf(MongoCursorInterface::class, $cursor);
+
+        // The countable interface is necessary for compatibility with PHP 7.3+, but not implemented by MongoCursor
+        if (! extension_loaded('mongo')) {
+            $this->assertInstanceOf(Countable::class, $cursor);
+        }
+    }
+
 
     /**
      * @return \PHPUnit_Framework_MockObject_MockObject

+ 13 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php

@@ -3,6 +3,7 @@
 namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
 
 use Alcaeus\MongoDbAdapter\Tests\TestCase;
+use Countable;
 
 class MongoGridFSCursorTest extends TestCase
 {
@@ -37,4 +38,16 @@ class MongoGridFSCursorTest extends TestCase
             ], $value->file);
         }
     }
+
+    public function testInterfaces()
+    {
+        $this->skipTestIf(extension_loaded('mongo'));
+
+        $gridfs = $this->getGridFS();
+        $id = $gridfs->storeBytes('foo', ['filename' => 'foo.txt']);
+        $gridfs->storeBytes('bar', ['filename' => 'bar.txt']);
+
+        $cursor = $gridfs->find(['filename' => 'foo.txt']);
+        $this->assertInstanceOf(Countable::class, $cursor);
+    }
 }