Skip to content

Commit

Permalink
Merge pull request #85 from alcaeus/count-legacy-arguments
Browse files Browse the repository at this point in the history
Allow calling count with limit and skip parameters
  • Loading branch information
alcaeus committed Mar 29, 2016
2 parents 5bdafb9 + 5aa730f commit d61d681
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,23 @@ public function getIndexInfo()
* @param array $options
* @return int Returns the number of documents matching the query.
*/
public function count($query = [], array $options = [])
public function count($query = [], $options = [])
{
try {
// Handle legacy mode - limit and skip as second and third parameters, respectively
if (! is_array($options)) {
$limit = $options;
$options = [];

if ($limit !== null) {
$options['limit'] = (int) $limit;
}

if (func_num_args() > 2) {
$options['skip'] = (int) func_get_args()[2];
}
}

return $this->collection->count(TypeConverter::fromLegacy($query), $options);
} catch (\MongoDB\Driver\Exception\Exception $e) {
throw ExceptionConverter::toLegacy($e);
Expand Down
54 changes: 54 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,60 @@ public function testCount()
$this->assertSame(2, $collection->count(['foo' => 'bar']));
}

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

$this->assertSame(2, $collection->count([], ['limit' => 2]));
$this->assertSame(1, $collection->count(['foo' => 'bar'], ['limit' => 1]));
}

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

$this->assertSame(2, $collection->count([], 2));
$this->assertSame(1, $collection->count(['foo' => 'bar'], 1));
}

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

$this->assertSame(2, $collection->count([], ['skip' => 1]));
$this->assertSame(1, $collection->count(['foo' => 'bar'], ['skip' => 1]));
}

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

$this->assertSame(2, $collection->count([], null, 1));
$this->assertSame(1, $collection->count(['foo' => 'bar'], null, 1));
}

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

$this->assertSame(2, $collection->count([], ['skip' => 1, 'limit' => 2]));
$this->assertSame(1, $collection->count([], ['skip' => 1, 'limit' => 1]));
}

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

$this->assertSame(2, $collection->count([], 2, 1));
$this->assertSame(1, $collection->count([], 1, 1));
}

public function testCountTimeout()
{
$this->failMaxTimeMS();
Expand Down

0 comments on commit d61d681

Please sign in to comment.