Skip to content

Commit b795377

Browse files
committed
add executeCommand method to Database
1 parent 34b74f8 commit b795377

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-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
@@ -101,6 +101,19 @@ public function dropCollection($collectionName)
101101
return $operation->execute($server);
102102
}
103103

104+
/**
105+
* This is the method to issue database commands.
106+
*
107+
* @param Command $command A database command to send
108+
* @param ReadPreference|null $readPreference Default read preference to apply
109+
* @return Cursor
110+
*/
111+
public function executeCommand(Command $command, ReadPreference $readPreference = null)
112+
{
113+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
114+
return $server->executeCommand($this->databaseName, $command, $readPreference);
115+
}
116+
104117
/**
105118
* Returns the database name.
106119
*

tests/Database/DatabaseFunctionalTest.php

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

55
use MongoDB\Database;
66
use MongoDB\Driver\BulkWrite;
7+
use MongoDB\Driver\Command;
8+
use MongoDB\Driver\ReadPreference;
79

810
/**
911
* Functional tests for the Database class.
@@ -50,4 +52,17 @@ public function testDrop()
5052
$this->assertCommandSucceeded($commandResult);
5153
$this->assertCollectionCount($this->getNamespace(), 0);
5254
}
55+
56+
public function testCommand()
57+
{
58+
$command = new Command(['isMaster' => 1]);
59+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
60+
$cursor = $this->database->executeCommand($command, $readPreference);
61+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
62+
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
63+
$document = current($cursor->toArray());
64+
$this->assertCommandSucceeded($document);
65+
$this->assertArrayHasKey('ismaster', $document);
66+
$this->assertTrue($document['ismaster']);
67+
}
5368
}

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)