Commit 0cd9709a authored by Anton Tuyakhov's avatar Anton Tuyakhov Committed by Jeremy Mikola

PHPLIB-153: Add Database::command() helper

parent ae97a8d9
...@@ -92,6 +92,32 @@ class Database ...@@ -92,6 +92,32 @@ class Database
return $this->databaseName; return $this->databaseName;
} }
/**
* Execute a command on this database.
*
* @param array|object $command Command document
* @param ReadPreference|null $readPreference Read preference
* @return Cursor
*/
public function command($command, ReadPreference $readPreference = null)
{
if ( ! is_array($command) && ! is_object($command)) {
throw new InvalidArgumentTypeException('$command', $command, 'array or object');
}
if ( ! $command instanceof Command) {
$command = new Command($command);
}
if ( ! isset($readPreference)) {
$readPreference = $this->readPreference;
}
$server = $this->manager->selectServer($readPreference);
return $server->executeCommand($this->databaseName, $command);
}
/** /**
* Create a new collection explicitly. * Create a new collection explicitly.
* *
......
...@@ -64,6 +64,29 @@ class DatabaseFunctionalTest extends FunctionalTestCase ...@@ -64,6 +64,29 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertEquals($this->getDatabaseName(), $this->database->getDatabaseName()); $this->assertEquals($this->getDatabaseName(), $this->database->getDatabaseName());
} }
public function testCommand()
{
$command = ['isMaster' => 1];
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
$cursor = $this->database->command($command, $readPreference);
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
$commandResult = current($cursor->toArray());
$this->assertCommandSucceeded($commandResult);
$this->assertTrue(isset($commandResult->ismaster));
$this->assertTrue($commandResult->ismaster);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentValues
*/
public function testCommandCommandArgumentTypeCheck($command)
{
$this->database->command($command);
}
public function testDrop() public function testDrop()
{ {
$bulkWrite = new BulkWrite(); $bulkWrite = new BulkWrite();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment