Skip to content

Commit 84b82c4

Browse files
committed
Merge pull request #71
2 parents 3b128d3 + 6b95016 commit 84b82c4

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
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: 11 additions & 3 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
/**
@@ -40,16 +40,24 @@ public function __construct($databaseName, $collectionName, array $indexes)
4040
throw new InvalidArgumentException('$indexes is empty');
4141
}
4242

43-
foreach ($indexes as $index) {
43+
$expectedIndex = 0;
44+
45+
foreach ($indexes as $i => $index) {
46+
if ($i !== $expectedIndex) {
47+
throw new InvalidArgumentException(sprintf('$indexes is not a list (unexpected index: "%s")', $i));
48+
}
49+
4450
if ( ! is_array($index)) {
45-
throw new UnexpectedTypeException($index, 'array');
51+
throw new InvalidArgumentTypeException(sprintf('$index[%d]', $i), $index, 'array');
4652
}
4753

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

5258
$this->indexes[] = new IndexInput($index);
59+
60+
$expectedIndex += 1;
5361
}
5462

5563
$this->databaseName = (string) $databaseName;

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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ class CreateIndexesTest extends TestCase
88
{
99
/**
1010
* @expectedException MongoDB\Exception\InvalidArgumentException
11+
* @expectedExceptionMessage $indexes is empty
1112
*/
1213
public function testCreateIndexesRequiresAtLeastOneIndex()
1314
{
1415
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), []);
1516
}
1617

1718
/**
18-
* @expectedException MongoDB\Exception\UnexpectedTypeException
19+
* @expectedException MongoDB\Exception\InvalidArgumentException
20+
* @expectedExceptionMessage $indexes is not a list (unexpected index: "1")
21+
*/
22+
public function testConstructorIndexesArgumentMustBeAList()
23+
{
24+
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [1 => ['key' => ['x' => 1]]]);
25+
}
26+
27+
/**
28+
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
1929
* @dataProvider provideInvalidIndexSpecificationTypes
2030
*/
2131
public function testCreateIndexesRequiresIndexSpecificationsToBeAnArray($index)

0 commit comments

Comments
 (0)