Skip to content

Commit 2076361

Browse files
committed
PHPLIB-358: Do not require collection name for Aggregate operation
1 parent 930fb18 commit 2076361

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/Operation/Aggregate.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ class Aggregate implements Executable
116116
* This is not supported for server versions < 3.4 and will result in an
117117
* exception at execution time if used.
118118
*
119-
* @param string $databaseName Database name
120-
* @param string $collectionName Collection name
121-
* @param array $pipeline List of pipeline operations
122-
* @param array $options Command options
119+
* Note: Collection-agnostic commands (e.g. $currentOp) may be executed by
120+
* specifying null for the collection name.
121+
*
122+
* @param string $databaseName Database name
123+
* @param string|null $collectionName Collection name
124+
* @param array $pipeline List of pipeline operations
125+
* @param array $options Command options
123126
* @throws InvalidArgumentException for parameter/option parsing errors
124127
*/
125128
public function __construct($databaseName, $collectionName, array $pipeline, array $options = [])
@@ -220,7 +223,7 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
220223
}
221224

222225
$this->databaseName = (string) $databaseName;
223-
$this->collectionName = (string) $collectionName;
226+
$this->collectionName = isset($collectionName) ? (string) $collectionName : null;
224227
$this->pipeline = $pipeline;
225228
$this->options = $options;
226229
}
@@ -289,7 +292,7 @@ public function execute(Server $server)
289292
private function createCommand(Server $server)
290293
{
291294
$cmd = [
292-
'aggregate' => $this->collectionName,
295+
'aggregate' => isset($this->collectionName) ? $this->collectionName : 1,
293296
'pipeline' => $this->pipeline,
294297
];
295298
$cmdOptions = [];

tests/Operation/AggregateFunctionalTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@
1212

1313
class AggregateFunctionalTest extends FunctionalTestCase
1414
{
15+
public function testCurrentOpCommand()
16+
{
17+
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
18+
$this->markTestSkipped('$currentOp is not supported');
19+
}
20+
21+
(new CommandObserver)->observe(
22+
function() {
23+
$operation = new Aggregate(
24+
'admin',
25+
null,
26+
[['$currentOp' => (object) []]]
27+
);
28+
29+
$operation->execute($this->getPrimaryServer());
30+
},
31+
function(stdClass $command) {
32+
$this->assertSame(1, $command->aggregate);
33+
}
34+
);
35+
}
36+
1537
public function testDefaultReadConcernIsOmitted()
1638
{
1739
(new CommandObserver)->observe(

0 commit comments

Comments
 (0)