Skip to content

Commit ff680a2

Browse files
committed
add executeCommand method to Database
1 parent 2b2d8ef commit ff680a2

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ php.ini
77
phpunit.xml
88
apigen.phar
99
site
10+
.idea

src/Database.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ public function dropCollection($collectionName)
135135
return $operation->execute($server);
136136
}
137137

138+
/**
139+
* This is the method to issue database commands.
140+
*
141+
* @param Command $command A database command to send
142+
* @param ReadPreference|null $readPreference Default read preference to apply
143+
* @return Cursor
144+
*/
145+
public function executeCommand(Command $command, ReadPreference $readPreference = null)
146+
{
147+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
148+
return $server->executeCommand($this->databaseName, $command, $readPreference);
149+
}
150+
138151
/**
139152
* Returns the database name.
140153
*

tests/Database/DatabaseFunctionalTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use MongoDB\Driver\BulkWrite;
77
use MongoDB\Driver\ReadPreference;
88
use MongoDB\Driver\WriteConcern;
9+
use MongoDB\Driver\Command;
910

1011
/**
1112
* Functional tests for the Database class.
@@ -77,6 +78,19 @@ public function testDrop()
7778
$this->assertCollectionCount($this->getNamespace(), 0);
7879
}
7980

81+
public function testCommand()
82+
{
83+
$command = new Command(['isMaster' => 1]);
84+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
85+
$cursor = $this->database->executeCommand($command, $readPreference);
86+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
87+
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
88+
$document = current($cursor->toArray());
89+
$this->assertCommandSucceeded($document);
90+
$this->assertArrayHasKey('ismaster', $document);
91+
$this->assertTrue($document['ismaster']);
92+
}
93+
8094
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
8195
{
8296
$databaseOptions = [

tests/Database/FunctionalTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111
abstract class FunctionalTestCase extends BaseFunctionalTestCase
1212
{
13+
/**
14+
* @var $database Database
15+
*/
1316
protected $database;
1417

1518
public function setUp()

0 commit comments

Comments
 (0)