Skip to content

Commit 289478a

Browse files
authored
Merge pull request #261 from alcaeus/fix-cursorinterface-inheritance
Fix cursor interface inheritance
2 parents cd2393e + 1bfccc6 commit 289478a

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

lib/Mongo/MongoCursor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* Result object for database query.
2929
* @link http://www.php.net/manual/en/class.mongocursor.php
3030
*/
31-
class MongoCursor extends AbstractCursor implements Iterator, Countable
31+
class MongoCursor extends AbstractCursor implements Iterator, Countable, MongoCursorInterface
3232
{
3333
/**
3434
* @var bool

lib/Mongo/MongoGridFSCursor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
return;
1818
}
1919

20-
class MongoGridFSCursor extends MongoCursor
20+
class MongoGridFSCursor extends MongoCursor implements Countable
2121
{
2222
/**
2323
* @static

tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
44

5+
use MongoCursorInterface;
56
use MongoDB\Database;
67
use MongoDB\Driver\ReadPreference;
78
use Alcaeus\MongoDbAdapter\Tests\TestCase;
@@ -202,4 +203,12 @@ public function dataCommandAppliesCorrectReadPreference()
202203
],
203204
];
204205
}
206+
207+
public function testInterfaces()
208+
{
209+
$this->prepareData();
210+
$cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
211+
212+
$this->assertInstanceOf(MongoCursorInterface::class, $cursor);
213+
}
205214
}

tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Alcaeus\MongoDbAdapter\Tests\TestCase;
66
use Alcaeus\MongoDbAdapter\TypeConverter;
7+
use Countable;
8+
use MongoCursorInterface;
79
use MongoDB\Driver\ReadPreference;
810
use MongoDB\Model\BSONDocument;
911
use MongoDB\Operation\Find;
@@ -516,6 +518,19 @@ public function testExplainConvertsQuery()
516518
$this->assertArraySubset($expected, $cursor->explain());
517519
}
518520

521+
public function testInterfaces()
522+
{
523+
$collection = $this->getCollection();
524+
$cursor = $collection->find();
525+
526+
$this->assertInstanceOf(MongoCursorInterface::class, $cursor);
527+
528+
// The countable interface is necessary for compatibility with PHP 7.3+, but not implemented by MongoCursor
529+
if (! extension_loaded('mongo')) {
530+
$this->assertInstanceOf(Countable::class, $cursor);
531+
}
532+
}
533+
519534

520535
/**
521536
* @return \PHPUnit_Framework_MockObject_MockObject

tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
44

55
use Alcaeus\MongoDbAdapter\Tests\TestCase;
6+
use Countable;
67

78
class MongoGridFSCursorTest extends TestCase
89
{
@@ -37,4 +38,16 @@ public function testCursorItems()
3738
], $value->file);
3839
}
3940
}
41+
42+
public function testInterfaces()
43+
{
44+
$this->skipTestIf(extension_loaded('mongo'));
45+
46+
$gridfs = $this->getGridFS();
47+
$id = $gridfs->storeBytes('foo', ['filename' => 'foo.txt']);
48+
$gridfs->storeBytes('bar', ['filename' => 'bar.txt']);
49+
50+
$cursor = $gridfs->find(['filename' => 'foo.txt']);
51+
$this->assertInstanceOf(Countable::class, $cursor);
52+
}
4053
}

0 commit comments

Comments
 (0)