Skip to content

Commit 66048a4

Browse files
committed
PHPLIB-69: Do not allow empty index name for dropIndex()
1 parent 60c0cb2 commit 66048a4

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Collection.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,16 @@ public function drop()
401401
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
402402
* @param string $indexName
403403
* @return Cursor
404-
* @throws InvalidArgumentException if "*" is specified, since dropIndexes()
405-
* should be used to drop multiple indexes
404+
* @throws InvalidArgumentException if $indexName is an empty string or "*"
406405
*/
407406
public function dropIndex($indexName)
408407
{
409408
$indexName = (string) $indexName;
410409

410+
if ($indexName === '') {
411+
throw new InvalidArgumentException('Index name cannot be empty');
412+
}
413+
411414
if ($indexName === '*') {
412415
throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes');
413416
}

tests/CollectionFunctionalTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public function testDropIndex()
150150
}
151151
}
152152

153+
/**
154+
* @expectedException MongoDB\Exception\InvalidArgumentException
155+
*/
156+
public function testDropIndexShouldNotAllowEmptyIndexName()
157+
{
158+
$this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
159+
$this->assertIndexExists('x_1');
160+
$this->collection->dropIndex('');
161+
}
162+
153163
/**
154164
* @expectedException MongoDB\Exception\InvalidArgumentException
155165
*/

0 commit comments

Comments
 (0)