Commit aee5a0c6 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-123: Do not throw when listing indexes on nonexistent collection

parent 3961ce21
...@@ -5,8 +5,10 @@ namespace MongoDB\Operation; ...@@ -5,8 +5,10 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command; use MongoDB\Driver\Command;
use MongoDB\Driver\Query; use MongoDB\Driver\Query;
use MongoDB\Driver\Server; use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Model\IndexInfoIterator; use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator; use MongoDB\Model\IndexInfoIteratorIterator;
use EmptyIterator;
/** /**
* Operation for the listIndexes command. * Operation for the listIndexes command.
...@@ -17,6 +19,8 @@ use MongoDB\Model\IndexInfoIteratorIterator; ...@@ -17,6 +19,8 @@ use MongoDB\Model\IndexInfoIteratorIterator;
*/ */
class ListIndexes implements Executable class ListIndexes implements Executable
{ {
private static $errorCodeDatabaseNotFound = 60;
private static $errorCodeNamespaceNotFound = 26;
private static $wireVersionForCommand = 3; private static $wireVersionForCommand = 3;
private $databaseName; private $databaseName;
...@@ -75,7 +79,20 @@ class ListIndexes implements Executable ...@@ -75,7 +79,20 @@ class ListIndexes implements Executable
$cmd['maxTimeMS'] = $this->options['maxTimeMS']; $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
} }
$cursor = $server->executeCommand($this->databaseName, new Command($cmd)); try {
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
} catch (RuntimeException $e) {
/* The server may return an error if the collection does not exist.
* Check for possible error codes (see: SERVER-20463) and return an
* empty iterator instead of throwing.
*/
if ($e->getCode() === self::$errorCodeNamespaceNotFound || $e->getCode() === self::$errorCodeDatabaseNotFound) {
return new IndexInfoIteratorIterator(new EmptyIterator);
}
throw $e;
}
$cursor->setTypeMap(array('root' => 'array', 'document' => 'array')); $cursor->setTypeMap(array('root' => 'array', 'document' => 'array'));
return new IndexInfoIteratorIterator($cursor); return new IndexInfoIteratorIterator($cursor);
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\Server;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\ListIndexes;
class ListIndexesFunctionalTest extends FunctionalTestCase
{
public function testListIndexesForNewlyCreatedCollection()
{
$server = $this->getPrimaryServer();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);
$writeResult = $this->manager->executeInsert($this->getNamespace(), ['x' => 1]);
$this->assertEquals(1, $writeResult->getInsertedCount());
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
$indexes = iterator_to_array($operation->execute($server));
$this->assertCount(1, $indexes);
foreach ($indexes as $index) {
$this->assertInstanceOf('MongoDB\Model\IndexInfo', $index);
$this->assertEquals(['_id' => 1], $index->getKey());
}
}
public function testListIndexesForNonexistentCollection()
{
$server = $this->getPrimaryServer();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($server);
$this->assertCount(0, $indexes);
}
}
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