Skip to content

Commit 7a54cbe

Browse files
committed
PHPLIB-558: Don't inherit read preference in Database::command
1 parent 364e7ab commit 7a54cbe

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/Database.php

-4
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,6 @@ public function aggregate(array $pipeline, array $options = [])
249249
*/
250250
public function command($command, array $options = [])
251251
{
252-
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
253-
$options['readPreference'] = $this->readPreference;
254-
}
255-
256252
if (! isset($options['typeMap'])) {
257253
$options['typeMap'] = $this->typeMap;
258254
}

tests/Database/DatabaseFunctionalTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Driver\Cursor;
88
use MongoDB\Driver\ReadConcern;
99
use MongoDB\Driver\ReadPreference;
10+
use MongoDB\Driver\Server;
1011
use MongoDB\Driver\WriteConcern;
1112
use MongoDB\Exception\InvalidArgumentException;
1213
use MongoDB\Operation\CreateIndexes;
@@ -100,6 +101,22 @@ public function testCommand()
100101
$this->assertTrue($commandResult->ismaster);
101102
}
102103

104+
public function testCommandDoesNotInheritReadPreference()
105+
{
106+
if ($this->isReplicaSet()) {
107+
$this->markTestSkipped('Test only applies to replica sets');
108+
}
109+
110+
$this->database = new Database($this->manager, $this->getDatabaseName(), ['readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY)]);
111+
112+
$command = ['ping' => 1];
113+
114+
$cursor = $this->database->command($command);
115+
116+
$this->assertInstanceOf(Cursor::class, $cursor);
117+
$this->assertTrue($cursor->getServer()->isPrimary());
118+
}
119+
103120
public function testCommandAppliesTypeMapToCursor()
104121
{
105122
$command = ['isMaster' => 1];

tests/FunctionalTestCase.php

+5
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ protected function getServerStorageEngine(ReadPreference $readPreference = null)
301301
throw new UnexpectedValueException('Could not determine server storage engine');
302302
}
303303

304+
protected function isReplicaSet()
305+
{
306+
return $this->getPrimaryServer()->getType() !== Server::TYPE_RS_PRIMARY;
307+
}
308+
304309
protected function isShardedCluster()
305310
{
306311
if ($this->getPrimaryServer()->getType() == Server::TYPE_MONGOS) {

0 commit comments

Comments
 (0)