Skip to content

Commit 1bedfc9

Browse files
committed
Unit tests for Distinct operation and allow array/object $filter
1 parent 107ece4 commit 1bedfc9

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/Collection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ public function deleteOne($filter, array $options = array())
228228
*
229229
* @see Distinct::__construct() for supported options
230230
* @param string $fieldName Field for which to return distinct values
231-
* @param array $filter Query by which to filter documents
232-
* @param array $options Command options
231+
* @param array|object $filter Query by which to filter documents
232+
* @param array $options Command options
233233
* @return mixed[]
234234
*/
235-
public function distinct($fieldName, array $filter = array(), array $options = array())
235+
public function distinct($fieldName, $filter = array(), array $options = array())
236236
{
237237
$operation = new Distinct($this->dbname, $this->collname, $fieldName, $filter, $options);
238238
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));

src/Operation/Distinct.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ class Distinct implements Executable
3939
* @param array $options Command options
4040
* @throws InvalidArgumentException
4141
*/
42-
public function __construct($databaseName, $collectionName, $fieldName, array $filter = array(), array $options = array())
42+
public function __construct($databaseName, $collectionName, $fieldName, $filter = array(), array $options = array())
4343
{
44+
if ( ! is_array($filter) && ! is_object($filter)) {
45+
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
46+
}
47+
4448
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
4549
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
4650
}

tests/Operation/DistinctTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\Distinct;
6+
7+
class DistinctTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
11+
* @dataProvider provideInvalidDocumentValues
12+
*/
13+
public function testConstructorFilterArgumentTypeCheck($filter)
14+
{
15+
new Distinct($this->getDatabaseName(), $this->getCollectionName(), 'x', $filter);
16+
}
17+
18+
/**
19+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
20+
* @dataProvider provideInvalidConstructorOptions
21+
*/
22+
public function testConstructorOptionTypeChecks(array $options)
23+
{
24+
new Distinct($this->getDatabaseName(), $this->getCollectionName(), 'x', array(), $options);
25+
}
26+
27+
public function provideInvalidConstructorOptions()
28+
{
29+
$options = array();
30+
31+
foreach ($this->getInvalidIntegerValues() as $value) {
32+
$options[][] = array('maxTimeMS' => $value);
33+
}
34+
35+
return $options;
36+
}
37+
}

0 commit comments

Comments
 (0)