Skip to content

Operation tests #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ install:
- sudo apt-get -y install gdb

before_script:
- phpenv config-rm xdebug.ini
- if dpkg --compare-versions ${SERVER_VERSION} le "2.4"; then export SERVER_SERVICE=mongodb; else export SERVER_SERVICE=mongod; fi
- if ! nc -z localhost 27017; then sudo service ${SERVER_SERVICE} start; fi
- mongod --version
Expand Down
24 changes: 12 additions & 12 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ public function bulkWrite(array $operations, array $options = array())
* Gets the number of documents matching the filter.
*
* @see Count::__construct() for supported options
* @param array $filter Query by which to filter documents
* @param array $options Command options
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
* @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);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
Expand Down Expand Up @@ -228,11 +228,11 @@ public function deleteOne($filter, array $options = array())
*
* @see Distinct::__construct() for supported options
* @param string $fieldName Field for which to return distinct values
* @param array $filter Query by which to filter documents
* @param array $options Command options
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
* @return mixed[]
*/
public function distinct($fieldName, array $filter = array(), array $options = array())
public function distinct($fieldName, $filter = array(), array $options = array())
{
$operation = new Distinct($this->dbname, $this->collname, $fieldName, $filter, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
Expand Down Expand Up @@ -292,11 +292,11 @@ public function dropIndexes()
*
* @see Find::__construct() for supported options
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
* @param array $filter Query by which to filter documents
* @param array $options Additional options
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
* @return Cursor
*/
public function find(array $filter = array(), array $options = array())
public function find($filter = array(), array $options = array())
{
$operation = new Find($this->dbname, $this->collname, $filter, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
Expand All @@ -309,11 +309,11 @@ public function find(array $filter = array(), array $options = array())
*
* @see FindOne::__construct() for supported options
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
* @param array $filter The find query to execute
* @param array $options Additional options
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
* @return object|null
*/
public function findOne(array $filter = array(), array $options = array())
public function findOne($filter = array(), array $options = array())
{
$operation = new FindOne($this->dbname, $this->collname, $filter, $options);
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
Expand Down
32 changes: 18 additions & 14 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ class Aggregate implements Executable
*/
public function __construct($databaseName, $collectionName, array $pipeline, array $options = array())
{
if (empty($pipeline)) {
throw new InvalidArgumentException('$pipeline is empty');
}

$expectedIndex = 0;

foreach ($pipeline as $i => $operation) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
}

if ( ! is_array($operation) && ! is_object($operation)) {
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
}

$expectedIndex += 1;
}

$options += array(
'allowDiskUse' => false,
'useCursor' => true,
Expand All @@ -84,20 +102,6 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
throw new InvalidArgumentException('"batchSize" option should not be used if "useCursor" is false');
}

$expectedIndex = 0;

foreach ($pipeline as $i => $op) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
}

if ( ! is_array($op) && ! is_object($op)) {
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $op, 'array or object');
}

$expectedIndex += 1;
}

$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->pipeline = $pipeline;
Expand Down
6 changes: 5 additions & 1 deletion src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ class Count implements Executable
* @param array $options Command options
* @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 (is_array($options['hint']) || is_object($options['hint'])) {
$options['hint'] = \MongoDB\generate_index_name($options['hint']);
Expand Down
6 changes: 5 additions & 1 deletion src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ class Distinct implements Executable
* @param array $options Command options
* @throws InvalidArgumentException
*/
public function __construct($databaseName, $collectionName, $fieldName, array $filter = array(), array $options = array())
public function __construct($databaseName, $collectionName, $fieldName, $filter = array(), array $options = array())
{
if ( ! is_array($filter) && ! is_object($filter)) {
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
}

if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
Expand Down
72 changes: 72 additions & 0 deletions tests/Operation/AggregateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Operation\Aggregate;

class AggregateTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage $pipeline is empty
*/
public function testConstructorPipelineArgumentMustNotBeEmpty()
{
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), array());
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage $pipeline is not a list (unexpected index: "1")
*/
public function testConstructorPipelineArgumentMustBeAList()
{
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), array(1 => array('$match' => array('x' => 1))));
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), array(array('$match' => array('x' => 1))), $options);
}

public function provideInvalidConstructorOptions()
{
$options = array();

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = array('allowDiskUse' => $value);
}

foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = array('batchSize' => $value);
}

foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = array('maxTimeMS' => $value);
}

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = array('useCursor' => $value);
}

return $options;
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage "batchSize" option should not be used if "useCursor" is false
*/
public function testConstructorBatchSizeOptionRequiresUseCursor()
{
new Aggregate(
$this->getDatabaseName(),
$this->getCollectionName(),
array(array('$match' => array('x' => 1))),
array('batchSize' => 100, 'useCursor' => false)
);
}
}
Loading