| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
- use MongoCursorInterface;
- use MongoDB\Database;
- use MongoDB\Driver\ReadPreference;
- use Alcaeus\MongoDbAdapter\Tests\TestCase;
- /**
- * @author alcaeus <alcaeus@alcaeus.org>
- */
- class MongoCommandCursorTest extends TestCase
- {
- public function testSerialize()
- {
- $this->prepareData();
- $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
- $this->assertIsString(serialize($cursor));
- }
- public function testInfo()
- {
- $this->prepareData();
- $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
- $expected = [
- 'ns' => 'mongo-php-adapter.test',
- 'limit' => 0,
- 'batchSize' => 0,
- 'skip' => 0,
- 'flags' => 0,
- 'query' => [
- 'aggregate' => 'test',
- 'pipeline' => [
- [
- '$match' => ['foo' => 'bar']
- ]
- ],
- 'cursor' => new \stdClass(),
- ],
- 'fields' => null,
- 'started_iterating' => false,
- ];
- $info = $cursor->info();
- $this->assertEquals($expected, $info);
- // Ensure cursor started iterating
- $array = iterator_to_array($cursor);
- $expected['started_iterating'] = true;
- $expected += [
- 'id' => 0,
- 'at' => 0,
- 'numReturned' => 0,
- 'server' => 'localhost:27017;-;.;' . getmypid(),
- 'host' => 'localhost',
- 'port' => 27017,
- 'connection_type_desc' => 'STANDALONE',
- ];
- $this->assertMatches($expected, $cursor->info());
- $i = 0;
- foreach ($array as $key => $value) {
- $this->assertEquals($i, $key);
- $i++;
- }
- }
- /**
- * @dataProvider dataCommandAppliesCorrectReadPreference
- */
- public function testCommandAppliesCorrectReadPreference($command, $expectedReadPreference)
- {
- $this->skipTestIf(extension_loaded('mongo'));
- $checkReadPreference = function ($other) use ($expectedReadPreference) {
- if (!is_array($other)) {
- return false;
- }
- if (!array_key_exists('readPreference', $other)) {
- return false;
- }
- if (!$other['readPreference'] instanceof ReadPreference) {
- return false;
- }
- return $other['readPreference']->getMode() === $expectedReadPreference;
- };
- $databaseMock = $this->createMock(Database::class);
- $databaseMock
- ->expects($this->once())
- ->method('command')
- ->with($this->anything(), $this->callback($checkReadPreference))
- ->will($this->returnValue(new \ArrayIterator()));
- $cursor = new \MongoCommandCursor($this->getClient(), (string) $this->getDatabase(), $command);
- $reflection = new \ReflectionProperty($cursor, 'db');
- $reflection->setAccessible(true);
- $reflection->setValue($cursor, $databaseMock);
- $cursor->setReadPreference(\MongoClient::RP_SECONDARY);
- iterator_to_array($cursor);
- self::assertSame(\MongoClient::RP_SECONDARY, $cursor->getReadPreference()['type']);
- }
- public function dataCommandAppliesCorrectReadPreference()
- {
- return [
- 'findAndUpdate' => [
- [
- 'findandmodify' => (string) $this->getCollection(),
- 'query' => [],
- 'update' => ['$inc' => ['field' => 1]],
- ],
- ReadPreference::RP_PRIMARY,
- ],
- 'findAndRemove' => [
- [
- 'findandremove' => (string) $this->getCollection(),
- 'query' => [],
- ],
- ReadPreference::RP_PRIMARY,
- ],
- 'mapReduceWithOut' => [
- [
- 'mapReduce' => (string) $this->getCollection(),
- 'out' => 'sample',
- ],
- ReadPreference::RP_PRIMARY,
- ],
- 'mapReduceWithOutInline' => [
- [
- 'mapReduce' => (string) $this->getCollection(),
- 'out' => ['inline' => 1],
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'count' => [
- [
- 'count' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'group' => [
- [
- 'group' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'dbStats' => [
- [
- 'dbStats' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'geoNear' => [
- [
- 'geoNear' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'geoWalk' => [
- [
- 'geoWalk' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'distinct' => [
- [
- 'distinct' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'aggregate' => [
- [
- 'aggregate' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'collStats' => [
- [
- 'collStats' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'geoSearch' => [
- [
- 'geoSearch' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- 'parallelCollectionScan' => [
- [
- 'parallelCollectionScan' => (string) $this->getCollection(),
- ],
- ReadPreference::RP_SECONDARY,
- ],
- ];
- }
- public function testInterfaces()
- {
- $this->prepareData();
- $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
- $this->assertInstanceOf(MongoCursorInterface::class, $cursor);
- }
- }
|