Skip to content

Commit

Permalink
Merge branch 'fix-aggregate-parameters' into 1.0.x
Browse files Browse the repository at this point in the history
* fix-aggregate-parameters:
  Check result of aggregation call
  Add changelog entry for fix
  fix and test for MongoCollection::aggregate

Fixes #114.
  • Loading branch information
alcaeus committed Jun 16, 2016
2 parents 70d5653 + d874ea5 commit 8806ab0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGELOG-1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ CHANGELOG

This changelog references the relevant changes done in minor version updates.

1.0.4 (xxxx-xx-xx)
------------------

All issues and pull requests under this release may be found under the
[1.0.4](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.4)
milestone.

* [#115](https://github.com/alcaeus/mongo-php-adapter/pull/115) fixes an error
where using the alternate syntax for `MongoCollection::aggregate` would lead to
empty aggregation pipelines

1.0.3 (2016-04-13)
------------------

Expand Down
3 changes: 2 additions & 1 deletion lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ public function __set($name, $value)
public function aggregate(array $pipeline, array $op = [])
{
if (! TypeConverter::isNumericArray($pipeline)) {
$operators = func_get_args();
$pipeline = [];
$options = [];

$i = 0;
foreach (func_get_args() as $operator) {
foreach ($operators as $operator) {
$i++;
if (! is_array($operator)) {
trigger_error("Argument $i is not an array", E_WARNING);
Expand Down
34 changes: 34 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,40 @@ public function testAggregate()
], $result['result']);
}

public function testAggregateWithMultiplePilelineOperatorsAsArguments()
{
$collection = $this->getCollection();

$this->prepareData();

try {
$result = $collection->aggregate(
[
'$group' => [
'_id' => '$foo',
'count' => [ '$sum' => 1 ],
],
],
[
'$sort' => ['_id' => 1]
]
);
} catch (\MongoResultException $ex) {
$msg = 'MongoCollection::aggregate ( array $op [, array $op [, array $... ]] ) should accept variable amount of pipeline operators as argument'
. "\n"
. $ex;
$this->fail($msg);
}

$this->assertInternalType('array', $result);
$this->assertArrayHasKey('result', $result);

$this->assertEquals([
['_id' => 'bar', 'count' => 2],
['_id' => 'foo', 'count' => 1],
], $result['result']);
}

public function testAggregateInvalidPipeline()
{
$collection = $this->getCollection();
Expand Down

0 comments on commit 8806ab0

Please sign in to comment.