Skip to content

Commit 798d510

Browse files
committed
Merged pull request #566
2 parents f4b5a6f + da70fdc commit 798d510

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/Operation/CountDocuments.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,15 @@ public function execute(Server $server)
152152
}
153153

154154
$cursor = $server->executeReadCommand($this->databaseName, new Command($this->createCommandDocument()), $this->createOptions());
155-
$result = current($cursor->toArray());
155+
$allResults = $cursor->toArray();
156156

157+
/* If there are no documents to count, the aggregation pipeline has no items to group, and
158+
* hence the result is an empty array (PHPLIB-376) */
159+
if (count($allResults) == 0) {
160+
return 0;
161+
}
162+
163+
$result = current($allResults);
157164
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
158165
throw new UnexpectedValueException('count command did not return a numeric "n" value');
159166
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\CountDocuments;
6+
use MongoDB\Operation\InsertMany;
7+
use stdClass;
8+
9+
class CountDocumentsFunctionalTest extends FunctionalTestCase
10+
{
11+
public function testEmptyCollection()
12+
{
13+
$operation = new CountDocuments($this->getDatabaseName(), $this->getCollectionName(), []);
14+
$this->assertSame(0, $operation->execute($this->getPrimaryServer()));
15+
}
16+
17+
public function testNonEmptyCollection()
18+
{
19+
$insertMany = new InsertMany($this->getDatabaseName(), $this->getCollectionName(), [
20+
['x' => 1],
21+
['x' => 2],
22+
['y' => 3],
23+
['z' => 4],
24+
]);
25+
$insertMany->execute($this->getPrimaryServer());
26+
27+
$operation = new CountDocuments($this->getDatabaseName(), $this->getCollectionName(), []);
28+
$this->assertSame(4, $operation->execute($this->getPrimaryServer()));
29+
}
30+
}

tests/Operation/CountFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testHintOption()
5555

5656
foreach ($hintsUsingSparseIndex as $hint) {
5757
$operation = new Count($this->getDatabaseName(), $this->getCollectionName(), $filter, ['hint' => $hint]);
58-
$this->assertEquals(2, $operation->execute($this->getPrimaryServer()));
58+
$this->assertSame(2, $operation->execute($this->getPrimaryServer()));
5959
}
6060

6161
$hintsNotUsingSparseIndex = [
@@ -66,7 +66,7 @@ public function testHintOption()
6666

6767
foreach ($hintsNotUsingSparseIndex as $hint) {
6868
$operation = new Count($this->getDatabaseName(), $this->getCollectionName(), $filter, ['hint' => $hint]);
69-
$this->assertEquals(3, $operation->execute($this->getPrimaryServer()));
69+
$this->assertSame(3, $operation->execute($this->getPrimaryServer()));
7070
}
7171
}
7272

0 commit comments

Comments
 (0)