Skip to content

Commit ddde97b

Browse files
committed
CreateIndexes can require that $indexes be a list
1 parent 6af9244 commit ddde97b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Operation/CreateIndexes.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ 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)) {
4551
throw new InvalidArgumentTypeException(sprintf('$index[%d]', $i), $index, 'array');
4652
}
@@ -50,6 +56,8 @@ public function __construct($databaseName, $collectionName, array $indexes)
5056
}
5157

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

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

tests/Operation/CreateIndexesTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ public function testCreateIndexesRequiresAtLeastOneIndex()
1616
}
1717

1818
/**
19-
* @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
2029
* @dataProvider provideInvalidIndexSpecificationTypes
2130
*/
2231
public function testCreateIndexesRequiresIndexSpecificationsToBeAnArray($index)

0 commit comments

Comments
 (0)