Skip to content

Commit 9d087e5

Browse files
committed
Merge pull request #48
2 parents a4992db + 0cd9709 commit 9d087e5

File tree

5 files changed

+130
-40
lines changed

5 files changed

+130
-40
lines changed

src/Database.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ public function __toString()
9292
return $this->databaseName;
9393
}
9494

95+
/**
96+
* Execute a command on this database.
97+
*
98+
* @param array|object $command Command document
99+
* @param ReadPreference|null $readPreference Read preference
100+
* @return Cursor
101+
*/
102+
public function command($command, ReadPreference $readPreference = null)
103+
{
104+
if ( ! is_array($command) && ! is_object($command)) {
105+
throw new InvalidArgumentTypeException('$command', $command, 'array or object');
106+
}
107+
108+
if ( ! $command instanceof Command) {
109+
$command = new Command($command);
110+
}
111+
112+
if ( ! isset($readPreference)) {
113+
$readPreference = $this->readPreference;
114+
}
115+
116+
$server = $this->manager->selectServer($readPreference);
117+
118+
return $server->executeCommand($this->databaseName, $command);
119+
}
120+
95121
/**
96122
* Create a new collection explicitly.
97123
*

tests/Database/DatabaseFunctionalTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ public function getGetDatabaseName()
6464
$this->assertEquals($this->getDatabaseName(), $this->database->getDatabaseName());
6565
}
6666

67+
public function testCommand()
68+
{
69+
$command = ['isMaster' => 1];
70+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
71+
$cursor = $this->database->command($command, $readPreference);
72+
73+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
74+
$commandResult = current($cursor->toArray());
75+
76+
$this->assertCommandSucceeded($commandResult);
77+
$this->assertTrue(isset($commandResult->ismaster));
78+
$this->assertTrue($commandResult->ismaster);
79+
}
80+
81+
/**
82+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
83+
* @dataProvider provideInvalidDocumentValues
84+
*/
85+
public function testCommandCommandArgumentTypeCheck($command)
86+
{
87+
$this->database->command($command);
88+
}
89+
6790
public function testDrop()
6891
{
6992
$bulkWrite = new BulkWrite();

tests/Operation/BulkWriteTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ public function testConstructorOptionTypeChecks(array $options)
341341
);
342342
}
343343

344+
public function provideInvalidBooleanValues()
345+
{
346+
return $this->wrapValuesForDataProvider($this->getInvalidBooleanValues());
347+
}
348+
344349
public function provideInvalidConstructorOptions()
345350
{
346351
$options = [];

tests/Operation/TestCase.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,10 @@
33
namespace MongoDB\Tests\Operation;
44

55
use MongoDB\Tests\TestCase as BaseTestCase;
6-
use stdClass;
76

87
/**
98
* Base class for Operation unit tests.
109
*/
1110
abstract class TestCase extends BaseTestCase
1211
{
13-
public function provideInvalidDocumentValues()
14-
{
15-
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
16-
}
17-
18-
public function provideInvalidBooleanValues()
19-
{
20-
return $this->wrapValuesForDataProvider($this->getInvalidBooleanValues());
21-
}
22-
23-
protected function getInvalidArrayValues()
24-
{
25-
return [123, 3.14, 'foo', true, new stdClass];
26-
}
27-
28-
protected function getInvalidBooleanValues()
29-
{
30-
return [123, 3.14, 'foo', [], new stdClass];
31-
}
32-
33-
protected function getInvalidDocumentValues()
34-
{
35-
return [123, 3.14, 'foo', true];
36-
}
37-
38-
protected function getInvalidIntegerValues()
39-
{
40-
return [3.14, 'foo', true, [], new stdClass];
41-
}
42-
43-
protected function getInvalidStringValues()
44-
{
45-
return [123, 3.14, true, [], new stdClass];
46-
}
47-
48-
protected function wrapValuesForDataProvider(array $values)
49-
{
50-
return array_map(function($value) { return [$value]; }, $values);
51-
}
5212
}

tests/TestCase.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
abstract class TestCase extends \PHPUnit_Framework_TestCase
99
{
10+
public function provideInvalidDocumentValues()
11+
{
12+
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
13+
}
14+
1015
/**
1116
* Return the test collection name.
1217
*
@@ -29,11 +34,71 @@ protected function getDatabaseName()
2934
return getenv('MONGODB_DATABASE') ?: 'phplib_test';
3035
}
3136

37+
/**
38+
* Return a list of invalid array values.
39+
*
40+
* @return array
41+
*/
42+
protected function getInvalidArrayValues()
43+
{
44+
return [123, 3.14, 'foo', true, new stdClass];
45+
}
46+
47+
/**
48+
* Return a list of invalid boolean values.
49+
*
50+
* @return array
51+
*/
52+
protected function getInvalidBooleanValues()
53+
{
54+
return [123, 3.14, 'foo', [], new stdClass];
55+
}
56+
57+
/**
58+
* Return a list of invalid document values.
59+
*
60+
* @return array
61+
*/
62+
protected function getInvalidDocumentValues()
63+
{
64+
return [123, 3.14, 'foo', true];
65+
}
66+
67+
/**
68+
* Return a list of invalid integer values.
69+
*
70+
* @return array
71+
*/
72+
protected function getInvalidIntegerValues()
73+
{
74+
return [3.14, 'foo', true, [], new stdClass];
75+
}
76+
77+
/**
78+
* Return a list of invalid ReadPreference values.
79+
*
80+
* @return array
81+
*/
3282
protected function getInvalidReadPreferenceValues()
3383
{
3484
return [123, 3.14, 'foo', true, [], new stdClass];
3585
}
3686

87+
/**
88+
* Return a list of invalid string values.
89+
*
90+
* @return array
91+
*/
92+
protected function getInvalidStringValues()
93+
{
94+
return [123, 3.14, true, [], new stdClass];
95+
}
96+
97+
/**
98+
* Return a list of invalid WriteConcern values.
99+
*
100+
* @return array
101+
*/
37102
protected function getInvalidWriteConcernValues()
38103
{
39104
return [123, 3.14, 'foo', true, [], new stdClass];
@@ -58,4 +123,15 @@ protected function getUri()
58123
{
59124
return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017';
60125
}
126+
127+
/**
128+
* Wrap a list of values for use as a single-argument data provider.
129+
*
130+
* @param array $values List of values
131+
* @return array
132+
*/
133+
protected function wrapValuesForDataProvider(array $values)
134+
{
135+
return array_map(function($value) { return [$value]; }, $values);
136+
}
61137
}

0 commit comments

Comments
 (0)