Skip to content

Commit 107ece4

Browse files
committed
Unit tests for Count operation and allow array/object $filter
1 parent 0c8b44f commit 107ece4

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/Collection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ public function bulkWrite(array $operations, array $options = array())
124124
* Gets the number of documents matching the filter.
125125
*
126126
* @see Count::__construct() for supported options
127-
* @param array $filter Query by which to filter documents
128-
* @param array $options Command options
127+
* @param array|object $filter Query by which to filter documents
128+
* @param array $options Command options
129129
* @return integer
130130
*/
131-
public function count(array $filter = array(), array $options = array())
131+
public function count($filter = array(), array $options = array())
132132
{
133133
$operation = new Count($this->dbname, $this->collname, $filter, $options);
134134
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));

src/Operation/Count.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ class Count implements Executable
4545
* @param array $options Command options
4646
* @throws InvalidArgumentException
4747
*/
48-
public function __construct($databaseName, $collectionName, array $filter = array(), array $options = array())
48+
public function __construct($databaseName, $collectionName, $filter = array(), array $options = array())
4949
{
50+
if ( ! is_array($filter) && ! is_object($filter)) {
51+
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
52+
}
53+
5054
if (isset($options['hint'])) {
5155
if (is_array($options['hint']) || is_object($options['hint'])) {
5256
$options['hint'] = \MongoDB\generate_index_name($options['hint']);

tests/Operation/CountTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\Count;
6+
7+
class CountTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11+
* @dataProvider provideInvalidDocumentValues
12+
*/
13+
public function testConstructorFilterArgumentTypeCheck($filter)
14+
{
15+
new Count($this->getDatabaseName(), $this->getCollectionName(), $filter);
16+
}
17+
18+
/**
19+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
20+
* @dataProvider provideInvalidConstructorOptions
21+
*/
22+
public function testConstructorOptionTypeChecks(array $options)
23+
{
24+
new Count($this->getDatabaseName(), $this->getCollectionName(), array(), $options);
25+
}
26+
27+
public function provideInvalidConstructorOptions()
28+
{
29+
$options = array();
30+
31+
foreach ($this->getInvalidHintValues() as $value) {
32+
$options[][] = array('hint' => $value);
33+
}
34+
35+
foreach ($this->getInvalidIntegerValues() as $value) {
36+
$options[][] = array('limit' => $value);
37+
}
38+
39+
foreach ($this->getInvalidIntegerValues() as $value) {
40+
$options[][] = array('maxTimeMS' => $value);
41+
}
42+
43+
foreach ($this->getInvalidIntegerValues() as $value) {
44+
$options[][] = array('skip' => $value);
45+
}
46+
47+
return $options;
48+
}
49+
50+
private function getInvalidHintValues()
51+
{
52+
return array(123, 3.14, true);
53+
}
54+
}

0 commit comments

Comments
 (0)