File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,31 @@ public function dropCollection($collectionName)
135
135
return $ operation ->execute ($ server );
136
136
}
137
137
138
+ /**
139
+ * This is the method to issue database commands.
140
+ *
141
+ * @param array|object $command A database command to send
142
+ * @param ReadPreference|null $readPreference Default read preference to apply
143
+ * @return Cursor
144
+ */
145
+ public function executeCommand ($ command , ReadPreference $ readPreference = null )
146
+ {
147
+ if ( ! is_array ($ command ) && ! is_object ($ command )) {
148
+ throw new InvalidArgumentTypeException ('"command" parameter ' , $ command , 'array or object ' );
149
+ }
150
+
151
+ if ( ! $ command instanceof Command) {
152
+ $ command = new Command ($ command );
153
+ }
154
+
155
+ if ( ! isset ($ readPreference )) {
156
+ $ readPreference = $ this ->readPreference ;
157
+ }
158
+
159
+ $ server = $ this ->manager ->selectServer ($ readPreference );
160
+ return $ server ->executeCommand ($ this ->databaseName , $ command );
161
+ }
162
+
138
163
/**
139
164
* Returns the database name.
140
165
*
Original file line number Diff line number Diff line change 6
6
use MongoDB \Driver \BulkWrite ;
7
7
use MongoDB \Driver \ReadPreference ;
8
8
use MongoDB \Driver \WriteConcern ;
9
+ use MongoDB \Driver \Command ;
9
10
10
11
/**
11
12
* Functional tests for the Database class.
@@ -77,6 +78,19 @@ public function testDrop()
77
78
$ this ->assertCollectionCount ($ this ->getNamespace (), 0 );
78
79
}
79
80
81
+ public function testCommand ()
82
+ {
83
+ $ command = new Command (['isMaster ' => 1 ]);
84
+ $ readPreference = new ReadPreference (ReadPreference::RP_PRIMARY );
85
+ $ cursor = $ this ->database ->executeCommand ($ command , $ readPreference );
86
+ $ this ->assertInstanceOf ('MongoDB\Driver\Cursor ' , $ cursor );
87
+ $ cursor ->setTypeMap (['root ' => 'array ' , 'document ' => 'array ' ]);
88
+ $ document = current ($ cursor ->toArray ());
89
+ $ this ->assertCommandSucceeded ($ document );
90
+ $ this ->assertArrayHasKey ('ismaster ' , $ document );
91
+ $ this ->assertTrue ($ document ['ismaster ' ]);
92
+ }
93
+
80
94
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern ()
81
95
{
82
96
$ databaseOptions = [
Original file line number Diff line number Diff line change 10
10
*/
11
11
abstract class FunctionalTestCase extends BaseFunctionalTestCase
12
12
{
13
+ /**
14
+ * @var $database Database
15
+ */
13
16
protected $ database ;
14
17
15
18
public function setUp ()
You can’t perform that action at this time.
0 commit comments