Skip to content

Commit 7239ab7

Browse files
committed
Use InvalidArgumentTypeException for index option validation
1 parent 3b128d3 commit 7239ab7

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/Model/IndexInput.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use MongoDB\BSON\Serializable;
66
use MongoDB\Exception\InvalidArgumentException;
7-
use MongoDB\Exception\UnexpectedTypeException;
7+
use MongoDB\Exception\InvalidArgumentTypeException;
88

99
/**
1010
* Index input model class.
@@ -32,12 +32,12 @@ public function __construct(array $index)
3232
}
3333

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

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

@@ -46,15 +46,15 @@ public function __construct(array $index)
4646
}
4747

4848
if ( ! is_string($index['ns'])) {
49-
throw new UnexpectedTypeException($index['ns'], 'string');
49+
throw new InvalidArgumentTypeException('"ns" option', $index['ns'], 'string');
5050
}
5151

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

5656
if ( ! is_string($index['name'])) {
57-
throw new UnexpectedTypeException($index['name'], 'string');
57+
throw new InvalidArgumentTypeException('"name" option', $index['name'], 'string');
5858
}
5959

6060
$this->index = $index;

src/Operation/CreateIndexes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use MongoDB\Driver\BulkWrite as Bulk;
88
use MongoDB\Driver\WriteConcern;
99
use MongoDB\Exception\InvalidArgumentException;
10-
use MongoDB\Exception\UnexpectedTypeException;
10+
use MongoDB\Exception\InvalidArgumentTypeException;
1111
use MongoDB\Model\IndexInput;
1212

1313
/**
@@ -42,7 +42,7 @@ public function __construct($databaseName, $collectionName, array $indexes)
4242

4343
foreach ($indexes as $index) {
4444
if ( ! is_array($index)) {
45-
throw new UnexpectedTypeException($index, 'array');
45+
throw new InvalidArgumentTypeException(sprintf('$index[%d]', $i), $index, 'array');
4646
}
4747

4848
if ( ! isset($index['ns'])) {

tests/Model/IndexInputTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Model\IndexInput;
66
use MongoDB\Tests\TestCase;
7+
use stdClass;
78

89
class IndexInputTest extends TestCase
910
{
@@ -16,19 +17,25 @@ public function testConstructorShouldRequireKey()
1617
}
1718

1819
/**
19-
* @expectedException MongoDB\Exception\UnexpectedTypeException
20+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
2021
*/
2122
public function testConstructorShouldRequireKeyToBeArrayOrObject()
2223
{
2324
new IndexInput(['key' => 'foo']);
2425
}
2526

2627
/**
27-
* @expectedException MongoDB\Exception\UnexpectedTypeException
28+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
29+
* @dataProvider provideInvalidFieldOrderValues
2830
*/
29-
public function testConstructorShouldRequireKeyOrderToBeScalar()
31+
public function testConstructorShouldRequireKeyFieldOrderToBeNumericOrString($order)
3032
{
31-
new IndexInput(['key' => ['x' => []]]);
33+
new IndexInput(['key' => ['x' => $order]]);
34+
}
35+
36+
public function provideInvalidFieldOrderValues()
37+
{
38+
return $this->wrapValuesForDataProvider([true, [], new stdClass]);
3239
}
3340

3441
/**
@@ -40,15 +47,15 @@ public function testConstructorShouldRequireNamespace()
4047
}
4148

4249
/**
43-
* @expectedException MongoDB\Exception\UnexpectedTypeException
50+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
4451
*/
4552
public function testConstructorShouldRequireNamespaceToBeString()
4653
{
4754
new IndexInput(['key' => ['x' => 1], 'ns' => 1]);
4855
}
4956

5057
/**
51-
* @expectedException MongoDB\Exception\UnexpectedTypeException
58+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
5259
*/
5360
public function testConstructorShouldRequireNameToBeString()
5461
{

tests/Operation/CreateIndexesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CreateIndexesTest extends TestCase
88
{
99
/**
1010
* @expectedException MongoDB\Exception\InvalidArgumentException
11+
* @expectedExceptionMessage $indexes is empty
1112
*/
1213
public function testCreateIndexesRequiresAtLeastOneIndex()
1314
{

0 commit comments

Comments
 (0)