Skip to content

Commit f2c1021

Browse files
committed
Merge pull request #28
2 parents 694c211 + 424fc2c commit f2c1021

18 files changed

+495
-132
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ install:
2828
- sudo apt-get -y install gdb
2929

3030
before_script:
31+
- phpenv config-rm xdebug.ini
3132
- if dpkg --compare-versions ${SERVER_VERSION} le "2.4"; then export SERVER_SERVICE=mongodb; else export SERVER_SERVICE=mongod; fi
3233
- if ! nc -z localhost 27017; then sudo service ${SERVER_SERVICE} start; fi
3334
- mongod --version

src/Collection.php

Lines changed: 12 additions & 12 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));
@@ -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));
@@ -292,11 +292,11 @@ public function dropIndexes()
292292
*
293293
* @see Find::__construct() for supported options
294294
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
295-
* @param array $filter Query by which to filter documents
296-
* @param array $options Additional options
295+
* @param array|object $filter Query by which to filter documents
296+
* @param array $options Additional options
297297
* @return Cursor
298298
*/
299-
public function find(array $filter = array(), array $options = array())
299+
public function find($filter = array(), array $options = array())
300300
{
301301
$operation = new Find($this->dbname, $this->collname, $filter, $options);
302302
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
@@ -309,11 +309,11 @@ public function find(array $filter = array(), array $options = array())
309309
*
310310
* @see FindOne::__construct() for supported options
311311
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
312-
* @param array $filter The find query to execute
313-
* @param array $options Additional options
312+
* @param array|object $filter Query by which to filter documents
313+
* @param array $options Additional options
314314
* @return object|null
315315
*/
316-
public function findOne(array $filter = array(), array $options = array())
316+
public function findOne($filter = array(), array $options = array())
317317
{
318318
$operation = new FindOne($this->dbname, $this->collname, $filter, $options);
319319
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));

src/Operation/Aggregate.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ class Aggregate implements Executable
5959
*/
6060
public function __construct($databaseName, $collectionName, array $pipeline, array $options = array())
6161
{
62+
if (empty($pipeline)) {
63+
throw new InvalidArgumentException('$pipeline is empty');
64+
}
65+
66+
$expectedIndex = 0;
67+
68+
foreach ($pipeline as $i => $operation) {
69+
if ($i !== $expectedIndex) {
70+
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
71+
}
72+
73+
if ( ! is_array($operation) && ! is_object($operation)) {
74+
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
75+
}
76+
77+
$expectedIndex += 1;
78+
}
79+
6280
$options += array(
6381
'allowDiskUse' => false,
6482
'useCursor' => true,
@@ -84,20 +102,6 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
84102
throw new InvalidArgumentException('"batchSize" option should not be used if "useCursor" is false');
85103
}
86104

87-
$expectedIndex = 0;
88-
89-
foreach ($pipeline as $i => $op) {
90-
if ($i !== $expectedIndex) {
91-
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
92-
}
93-
94-
if ( ! is_array($op) && ! is_object($op)) {
95-
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $op, 'array or object');
96-
}
97-
98-
$expectedIndex += 1;
99-
}
100-
101105
$this->databaseName = (string) $databaseName;
102106
$this->collectionName = (string) $collectionName;
103107
$this->pipeline = $pipeline;

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']);

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/AggregateTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\Aggregate;
6+
7+
class AggregateTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentException
11+
* @expectedExceptionMessage $pipeline is empty
12+
*/
13+
public function testConstructorPipelineArgumentMustNotBeEmpty()
14+
{
15+
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), array());
16+
}
17+
18+
/**
19+
* @expectedException MongoDB\Exception\InvalidArgumentException
20+
* @expectedExceptionMessage $pipeline is not a list (unexpected index: "1")
21+
*/
22+
public function testConstructorPipelineArgumentMustBeAList()
23+
{
24+
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), array(1 => array('$match' => array('x' => 1))));
25+
}
26+
27+
/**
28+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
29+
* @dataProvider provideInvalidConstructorOptions
30+
*/
31+
public function testConstructorOptionTypeChecks(array $options)
32+
{
33+
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), array(array('$match' => array('x' => 1))), $options);
34+
}
35+
36+
public function provideInvalidConstructorOptions()
37+
{
38+
$options = array();
39+
40+
foreach ($this->getInvalidBooleanValues() as $value) {
41+
$options[][] = array('allowDiskUse' => $value);
42+
}
43+
44+
foreach ($this->getInvalidIntegerValues() as $value) {
45+
$options[][] = array('batchSize' => $value);
46+
}
47+
48+
foreach ($this->getInvalidIntegerValues() as $value) {
49+
$options[][] = array('maxTimeMS' => $value);
50+
}
51+
52+
foreach ($this->getInvalidBooleanValues() as $value) {
53+
$options[][] = array('useCursor' => $value);
54+
}
55+
56+
return $options;
57+
}
58+
59+
/**
60+
* @expectedException MongoDB\Exception\InvalidArgumentException
61+
* @expectedExceptionMessage "batchSize" option should not be used if "useCursor" is false
62+
*/
63+
public function testConstructorBatchSizeOptionRequiresUseCursor()
64+
{
65+
new Aggregate(
66+
$this->getDatabaseName(),
67+
$this->getCollectionName(),
68+
array(array('$match' => array('x' => 1))),
69+
array('batchSize' => 100, 'useCursor' => false)
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)