Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ public function dropCollection($collectionName)
return $operation->execute($server);
}

/**
* This is the method to issue database commands.
*
* @param array|object $command A database command to send
* @param ReadPreference|null $readPreference Default read preference to apply
* @return Cursor
*/
public function executeCommand($command, ReadPreference $readPreference = null)
{
if ( ! is_array($command) && ! is_object($command)) {
throw new InvalidArgumentTypeException('"command" parameter', $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);
}

/**
* Returns the database name.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/DatabaseFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Command;

/**
* Functional tests for the Database class.
Expand Down Expand Up @@ -77,6 +78,19 @@ public function testDrop()
$this->assertCollectionCount($this->getNamespace(), 0);
}

public function testCommand()
{
$command = new Command(['isMaster' => 1]);
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
$cursor = $this->database->executeCommand($command, $readPreference);
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$document = current($cursor->toArray());
$this->assertCommandSucceeded($document);
$this->assertArrayHasKey('ismaster', $document);
$this->assertTrue($document['ismaster']);
}

public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
{
$databaseOptions = [
Expand Down
3 changes: 3 additions & 0 deletions tests/Database/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
/**
* @var $database Database
*/
protected $database;

public function setUp()
Expand Down