Commit 9d087e5a authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #48

parents a4992db2 0cd9709a
...@@ -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();
......
...@@ -341,6 +341,11 @@ class BulkWriteTest extends TestCase ...@@ -341,6 +341,11 @@ class BulkWriteTest extends TestCase
); );
} }
public function provideInvalidBooleanValues()
{
return $this->wrapValuesForDataProvider($this->getInvalidBooleanValues());
}
public function provideInvalidConstructorOptions() public function provideInvalidConstructorOptions()
{ {
$options = []; $options = [];
......
...@@ -3,50 +3,10 @@ ...@@ -3,50 +3,10 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Tests\TestCase as BaseTestCase; use MongoDB\Tests\TestCase as BaseTestCase;
use stdClass;
/** /**
* Base class for Operation unit tests. * Base class for Operation unit tests.
*/ */
abstract class TestCase extends BaseTestCase abstract class TestCase extends BaseTestCase
{ {
public function provideInvalidDocumentValues()
{
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
}
public function provideInvalidBooleanValues()
{
return $this->wrapValuesForDataProvider($this->getInvalidBooleanValues());
}
protected function getInvalidArrayValues()
{
return [123, 3.14, 'foo', true, new stdClass];
}
protected function getInvalidBooleanValues()
{
return [123, 3.14, 'foo', [], new stdClass];
}
protected function getInvalidDocumentValues()
{
return [123, 3.14, 'foo', true];
}
protected function getInvalidIntegerValues()
{
return [3.14, 'foo', true, [], new stdClass];
}
protected function getInvalidStringValues()
{
return [123, 3.14, true, [], new stdClass];
}
protected function wrapValuesForDataProvider(array $values)
{
return array_map(function($value) { return [$value]; }, $values);
}
} }
...@@ -7,6 +7,11 @@ use stdClass; ...@@ -7,6 +7,11 @@ use stdClass;
abstract class TestCase extends \PHPUnit_Framework_TestCase abstract class TestCase extends \PHPUnit_Framework_TestCase
{ {
public function provideInvalidDocumentValues()
{
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
}
/** /**
* Return the test collection name. * Return the test collection name.
* *
...@@ -29,11 +34,71 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase ...@@ -29,11 +34,71 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
return getenv('MONGODB_DATABASE') ?: 'phplib_test'; return getenv('MONGODB_DATABASE') ?: 'phplib_test';
} }
/**
* Return a list of invalid array values.
*
* @return array
*/
protected function getInvalidArrayValues()
{
return [123, 3.14, 'foo', true, new stdClass];
}
/**
* Return a list of invalid boolean values.
*
* @return array
*/
protected function getInvalidBooleanValues()
{
return [123, 3.14, 'foo', [], new stdClass];
}
/**
* Return a list of invalid document values.
*
* @return array
*/
protected function getInvalidDocumentValues()
{
return [123, 3.14, 'foo', true];
}
/**
* Return a list of invalid integer values.
*
* @return array
*/
protected function getInvalidIntegerValues()
{
return [3.14, 'foo', true, [], new stdClass];
}
/**
* Return a list of invalid ReadPreference values.
*
* @return array
*/
protected function getInvalidReadPreferenceValues() protected function getInvalidReadPreferenceValues()
{ {
return [123, 3.14, 'foo', true, [], new stdClass]; return [123, 3.14, 'foo', true, [], new stdClass];
} }
/**
* Return a list of invalid string values.
*
* @return array
*/
protected function getInvalidStringValues()
{
return [123, 3.14, true, [], new stdClass];
}
/**
* Return a list of invalid WriteConcern values.
*
* @return array
*/
protected function getInvalidWriteConcernValues() protected function getInvalidWriteConcernValues()
{ {
return [123, 3.14, 'foo', true, [], new stdClass]; return [123, 3.14, 'foo', true, [], new stdClass];
...@@ -58,4 +123,15 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase ...@@ -58,4 +123,15 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
{ {
return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017'; return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017';
} }
/**
* Wrap a list of values for use as a single-argument data provider.
*
* @param array $values List of values
* @return array
*/
protected function wrapValuesForDataProvider(array $values)
{
return array_map(function($value) { return [$value]; }, $values);
}
} }
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