Commit 3130ae38 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #34

parents 3961ce21 14667e36
......@@ -17,6 +17,7 @@ use MongoDB\Exception\RuntimeException;
*/
class DropCollection implements Executable
{
private static $errorMessageNamespaceNotFound = 'ns not found';
private $databaseName;
private $collectionName;
......@@ -44,7 +45,11 @@ class DropCollection implements Executable
try {
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
} catch (DriverRuntimeException $e) {
if ($e->getMessage() === 'ns not found') {
/* The server may return an error if the collection does not exist.
* Check for an error message (unfortunately, there isn't a code)
* and NOP instead of throwing.
*/
if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
return (object) ['ok' => 0, 'errmsg' => 'ns not found'];
}
......
......@@ -5,8 +5,10 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Query;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator;
use EmptyIterator;
/**
* Operation for the listIndexes command.
......@@ -17,6 +19,8 @@ use MongoDB\Model\IndexInfoIteratorIterator;
*/
class ListIndexes implements Executable
{
private static $errorCodeDatabaseNotFound = 60;
private static $errorCodeNamespaceNotFound = 26;
private static $wireVersionForCommand = 3;
private $databaseName;
......@@ -75,7 +79,20 @@ class ListIndexes implements Executable
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
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'));
return new IndexInfoIteratorIterator($cursor);
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\Server;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListCollections;
class ListCollectionsFunctionalTest extends FunctionalTestCase
{
public function testListCollectionsForNewlyCreatedDatabase()
{
$server = $this->getPrimaryServer();
$operation = new DropDatabase($this->getDatabaseName());
$operation->execute($server);
$writeResult = $this->manager->executeInsert($this->getNamespace(), ['x' => 1]);
$this->assertEquals(1, $writeResult->getInsertedCount());
$operation = new ListCollections($this->getDatabaseName(), ['filter' => ['name' => $this->getCollectionName()]]);
// Convert the CollectionInfoIterator to an array since we cannot rewind its cursor
$collections = iterator_to_array($operation->execute($server));
$this->assertCount(1, $collections);
foreach ($collections as $collection) {
$this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
$this->assertEquals($this->getCollectionName(), $collection->getName());
}
}
public function testListCollectionsForNonexistentDatabase()
{
$server = $this->getPrimaryServer();
$operation = new DropDatabase($this->getDatabaseName());
$operation->execute($server);
$operation = new ListCollections($this->getDatabaseName());
$collections = $operation->execute($server);
$this->assertCount(0, $collections);
}
}
<?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