Commit 7239ab74 authored by Jeremy Mikola's avatar Jeremy Mikola

Use InvalidArgumentTypeException for index option validation

parent 3b128d3a
......@@ -4,7 +4,7 @@ namespace MongoDB\Model;
use MongoDB\BSON\Serializable;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Exception\InvalidArgumentTypeException;
/**
* Index input model class.
......@@ -32,12 +32,12 @@ class IndexInput implements Serializable
}
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');
}
}
......@@ -46,7 +46,7 @@ class IndexInput implements Serializable
}
if ( ! is_string($index['ns'])) {
throw new UnexpectedTypeException($index['ns'], 'string');
throw new InvalidArgumentTypeException('"ns" option', $index['ns'], 'string');
}
if ( ! isset($index['name'])) {
......@@ -54,7 +54,7 @@ class IndexInput implements Serializable
}
if ( ! is_string($index['name'])) {
throw new UnexpectedTypeException($index['name'], 'string');
throw new InvalidArgumentTypeException('"name" option', $index['name'], 'string');
}
$this->index = $index;
......
......@@ -7,7 +7,7 @@ use MongoDB\Driver\Server;
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;
/**
......@@ -42,7 +42,7 @@ class CreateIndexes implements Executable
foreach ($indexes as $index) {
if ( ! is_array($index)) {
throw new UnexpectedTypeException($index, 'array');
throw new InvalidArgumentTypeException(sprintf('$index[%d]', $i), $index, 'array');
}
if ( ! isset($index['ns'])) {
......
......@@ -4,6 +4,7 @@ namespace MongoDB\Tests;
use MongoDB\Model\IndexInput;
use MongoDB\Tests\TestCase;
use stdClass;
class IndexInputTest extends TestCase
{
......@@ -16,7 +17,7 @@ class IndexInputTest extends TestCase
}
/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
*/
public function testConstructorShouldRequireKeyToBeArrayOrObject()
{
......@@ -24,11 +25,17 @@ class IndexInputTest extends TestCase
}
/**
* @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]);
}
/**
......@@ -40,7 +47,7 @@ class IndexInputTest extends TestCase
}
/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
*/
public function testConstructorShouldRequireNamespaceToBeString()
{
......@@ -48,7 +55,7 @@ class IndexInputTest extends TestCase
}
/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
*/
public function testConstructorShouldRequireNameToBeString()
{
......
......@@ -8,6 +8,7 @@ class CreateIndexesTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @expectedExceptionMessage $indexes is empty
*/
public function testCreateIndexesRequiresAtLeastOneIndex()
{
......
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