Commit 107ece46 authored by Jeremy Mikola's avatar Jeremy Mikola

Unit tests for Count operation and allow array/object $filter

parent 0c8b44f5
...@@ -124,11 +124,11 @@ class Collection ...@@ -124,11 +124,11 @@ class Collection
* Gets the number of documents matching the filter. * Gets the number of documents matching the filter.
* *
* @see Count::__construct() for supported options * @see Count::__construct() for supported options
* @param array $filter Query by which to filter documents * @param array|object $filter Query by which to filter documents
* @param array $options Command options * @param array $options Command options
* @return integer * @return integer
*/ */
public function count(array $filter = array(), array $options = array()) public function count($filter = array(), array $options = array())
{ {
$operation = new Count($this->dbname, $this->collname, $filter, $options); $operation = new Count($this->dbname, $this->collname, $filter, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
......
...@@ -45,8 +45,12 @@ class Count implements Executable ...@@ -45,8 +45,12 @@ class Count implements Executable
* @param array $options Command options * @param array $options Command options
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function __construct($databaseName, $collectionName, array $filter = array(), array $options = array()) public function __construct($databaseName, $collectionName, $filter = array(), array $options = array())
{ {
if ( ! is_array($filter) && ! is_object($filter)) {
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
}
if (isset($options['hint'])) { if (isset($options['hint'])) {
if (is_array($options['hint']) || is_object($options['hint'])) { if (is_array($options['hint']) || is_object($options['hint'])) {
$options['hint'] = \MongoDB\generate_index_name($options['hint']); $options['hint'] = \MongoDB\generate_index_name($options['hint']);
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\Count;
class CountTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidDocumentValues
*/
public function testConstructorFilterArgumentTypeCheck($filter)
{
new Count($this->getDatabaseName(), $this->getCollectionName(), $filter);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new Count($this->getDatabaseName(), $this->getCollectionName(), array(), $options);
}
public function provideInvalidConstructorOptions()
{
$options = array();
foreach ($this->getInvalidHintValues() as $value) {
$options[][] = array('hint' => $value);
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = array('limit' => $value);
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = array('maxTimeMS' => $value);
}
foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = array('skip' => $value);
}
return $options;
}
private function getInvalidHintValues()
{
return array(123, 3.14, true);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment