Permalink
Browse files

[Collection] Prepare reduce/finalize args for group queries

MongoCollection::group() will internally convert a string "reduce" argument to a MongoCode object, but it won't do it for the "finalize" option. Explicitly prepare both as we do in the map/reduce method.

Also, avoid calling MongoCollection::group() with an empty array as the options parameter, since this will trigger an E_DEPRECATED notice in version 1.2.11+ of the driver. That notice may be removed in version 1.3, but we should leave this in place until the deprecation notices are removed entirely in 2.0.
  • Loading branch information...
1 parent 6f41298 commit f68463e924242f059b2da00c3062bcd266cc4914 @jmikola jmikola committed Jul 16, 2012
Showing with 39 additions and 4 deletions.
  1. +16 −1 lib/Doctrine/MongoDB/Collection.php
  2. +23 −3 tests/Doctrine/MongoDB/Tests/CollectionTest.php
View
17 lib/Doctrine/MongoDB/Collection.php
@@ -539,9 +539,24 @@ public function group($keys, array $initial, $reduce, array $options = array())
protected function doGroup($keys, array $initial, $reduce, array $options)
{
+ if (is_string($reduce)) {
+ $reduce = new \MongoCode($reduce);
+ }
+
+ if (isset($options['finalize']) && is_string($options['finalize'])) {
+ $options['finalize'] = new \MongoCode($options['finalize']);
+ }
+
$collection = $this;
$result = $this->retry(function() use ($collection, $keys, $initial, $reduce, $options) {
- return $collection->getMongoCollection()->group($keys, $initial, $reduce, $options);
+ /* Version 1.2.11+ of the driver yields an E_DEPRECATED notice if an
+ * empty array is passed to MongoCollection::group(), as it assumes
+ * an it is the "condition" option's value being passed instead of
+ * a well-formed options array (the actual deprecated behavior).
+ */
+ return empty($options)
+ ? $collection->getMongoCollection()->group($keys, $initial, $reduce)
+ : $collection->getMongoCollection()->group($keys, $initial, $reduce, $options);
});
return new ArrayIterator($result);
}
View
26 tests/Doctrine/MongoDB/Tests/CollectionTest.php
@@ -312,18 +312,38 @@ public function testGetName()
$this->assertEquals(true, $result);
}
- public function testGroup()
+ public function testGroupWithNonEmptyOptionsArray()
+ {
+ $expectedOptions = array(
+ 'condition' => array(),
+ 'finalize' => new \MongoCode(''),
+ );
+
+ $mockConnection = $this->getMockConnection();
+ $mongoCollection = $this->getMockMongoCollection();
+ $mongoCollection->expects($this->once())
+ ->method('group')
+ ->with(array(), array(), $this->isInstanceOf('MongoCode'), $this->equalTo($expectedOptions))
+ ->will($this->returnValue(array()));
+
+ $mockDatabase = $this->getMockDatabase();
+ $coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
+ $result = $coll->group(array(), array(), '', array('condition' => array(), 'finalize' => ''));
+ $this->assertEquals(new \Doctrine\MongoDB\ArrayIterator(array()), $result);
+ }
+
+ public function testGroupWithEmptyOptionsArray()
{
$mockConnection = $this->getMockConnection();
$mongoCollection = $this->getMockMongoCollection();
$mongoCollection->expects($this->once())
->method('group')
- ->with(array(), array(), '', array())
+ ->with(array(), array(), $this->isInstanceOf('MongoCode'))
->will($this->returnValue(array()));
$mockDatabase = $this->getMockDatabase();
$coll = $this->getTestCollection($mockConnection, $mongoCollection, $mockDatabase);
- $result = $coll->group(array(), array(), '', array());
+ $result = $coll->group(array(), array(), '');
$this->assertEquals(new \Doctrine\MongoDB\ArrayIterator(array()), $result);
}

0 comments on commit f68463e

Please sign in to comment.