Skip to content

Commit

Permalink
Merge pull request alcaeus#261 from alcaeus/fix-cursorinterface-inher…
Browse files Browse the repository at this point in the history
…itance

Fix cursor interface inheritance
  • Loading branch information
alcaeus authored Nov 6, 2019
2 parents cd2393e + 1bfccc6 commit 289478a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Mongo/MongoCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* 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
Expand Down
2 changes: 1 addition & 1 deletion lib/Mongo/MongoGridFSCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
return;
}

class MongoGridFSCursor extends MongoCursor
class MongoGridFSCursor extends MongoCursor implements Countable
{
/**
* @static
Expand Down
9 changes: 9 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Alcaeus\MongoDbAdapter\Tests\Mongo;

use MongoCursorInterface;
use MongoDB\Database;
use MongoDB\Driver\ReadPreference;
use Alcaeus\MongoDbAdapter\Tests\TestCase;
Expand Down Expand Up @@ -202,4 +203,12 @@ public function dataCommandAppliesCorrectReadPreference()
],
];
}

public function testInterfaces()
{
$this->prepareData();
$cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);

$this->assertInstanceOf(MongoCursorInterface::class, $cursor);
}
}
15 changes: 15 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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;
Expand Down Expand Up @@ -516,6 +518,19 @@ public function testExplainConvertsQuery()
$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
Expand Down
13 changes: 13 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Alcaeus\MongoDbAdapter\Tests\Mongo;

use Alcaeus\MongoDbAdapter\Tests\TestCase;
use Countable;

class MongoGridFSCursorTest extends TestCase
{
Expand Down Expand Up @@ -37,4 +38,16 @@ public function testCursorItems()
], $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);
}
}

0 comments on commit 289478a

Please sign in to comment.