Skip to content

Refactor option validation for CreateIndexes and IndexInput #71

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 2 commits into from
Dec 23, 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
12 changes: 6 additions & 6 deletions src/Model/IndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use MongoDB\BSON\Serializable;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Exception\InvalidArgumentTypeException;

/**
* Index input model class.
Expand Down Expand Up @@ -32,12 +32,12 @@ public function __construct(array $index)
}

if ( ! is_array($index['key']) && ! is_object($index['key'])) {
throw new UnexpectedTypeException($index['key'], 'array or object');
throw new InvalidArgumentTypeException('"key" option', $index['key'], 'array or object');
}

foreach ($index['key'] as $order) {
foreach ($index['key'] as $fieldName => $order) {
if ( ! is_int($order) && ! is_float($order) && ! is_string($order)) {
throw new UnexpectedTypeException($order, 'numeric or string');
throw new InvalidArgumentTypeException(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
}
}

Expand All @@ -46,15 +46,15 @@ public function __construct(array $index)
}

if ( ! is_string($index['ns'])) {
throw new UnexpectedTypeException($index['ns'], 'string');
throw new InvalidArgumentTypeException('"ns" option', $index['ns'], 'string');
}

if ( ! isset($index['name'])) {
$index['name'] = \MongoDB\generate_index_name($index['key']);
}

if ( ! is_string($index['name'])) {
throw new UnexpectedTypeException($index['name'], 'string');
throw new InvalidArgumentTypeException('"name" option', $index['name'], 'string');
}

$this->index = $index;
Expand Down
14 changes: 11 additions & 3 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Model\IndexInput;

/**
Expand Down Expand Up @@ -40,16 +40,24 @@ public function __construct($databaseName, $collectionName, array $indexes)
throw new InvalidArgumentException('$indexes is empty');
}

foreach ($indexes as $index) {
$expectedIndex = 0;

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

if ( ! is_array($index)) {
throw new UnexpectedTypeException($index, 'array');
throw new InvalidArgumentTypeException(sprintf('$index[%d]', $i), $index, 'array');
}

if ( ! isset($index['ns'])) {
$index['ns'] = $databaseName . '.' . $collectionName;
}

$this->indexes[] = new IndexInput($index);

$expectedIndex += 1;
}

$this->databaseName = (string) $databaseName;
Expand Down
19 changes: 13 additions & 6 deletions tests/Model/IndexInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MongoDB\Model\IndexInput;
use MongoDB\Tests\TestCase;
use stdClass;

class IndexInputTest extends TestCase
{
Expand All @@ -16,19 +17,25 @@ public function testConstructorShouldRequireKey()
}

/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
*/
public function testConstructorShouldRequireKeyToBeArrayOrObject()
{
new IndexInput(['key' => 'foo']);
}

/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidFieldOrderValues
*/
public function testConstructorShouldRequireKeyOrderToBeScalar()
public function testConstructorShouldRequireKeyFieldOrderToBeNumericOrString($order)
{
new IndexInput(['key' => ['x' => []]]);
new IndexInput(['key' => ['x' => $order]]);
}

public function provideInvalidFieldOrderValues()
{
return $this->wrapValuesForDataProvider([true, [], new stdClass]);
}

/**
Expand All @@ -40,15 +47,15 @@ public function testConstructorShouldRequireNamespace()
}

/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
*/
public function testConstructorShouldRequireNamespaceToBeString()
{
new IndexInput(['key' => ['x' => 1], 'ns' => 1]);
}

/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
*/
public function testConstructorShouldRequireNameToBeString()
{
Expand Down
12 changes: 11 additions & 1 deletion tests/Operation/CreateIndexesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ class CreateIndexesTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage $indexes is empty
*/
public function testCreateIndexesRequiresAtLeastOneIndex()
{
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), []);
}

/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage $indexes is not a list (unexpected index: "1")
*/
public function testConstructorIndexesArgumentMustBeAList()
{
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [1 => ['key' => ['x' => 1]]]);
}

/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidIndexSpecificationTypes
*/
public function testCreateIndexesRequiresIndexSpecificationsToBeAnArray($index)
Expand Down