Skip to content

Commit 3130ae3

Browse files
committed
Merge pull request #34
2 parents 3961ce2 + 14667e3 commit 3130ae3

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

src/Operation/DropCollection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
class DropCollection implements Executable
1919
{
20+
private static $errorMessageNamespaceNotFound = 'ns not found';
2021
private $databaseName;
2122
private $collectionName;
2223

@@ -44,7 +45,11 @@ public function execute(Server $server)
4445
try {
4546
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
4647
} catch (DriverRuntimeException $e) {
47-
if ($e->getMessage() === 'ns not found') {
48+
/* The server may return an error if the collection does not exist.
49+
* Check for an error message (unfortunately, there isn't a code)
50+
* and NOP instead of throwing.
51+
*/
52+
if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
4853
return (object) ['ok' => 0, 'errmsg' => 'ns not found'];
4954
}
5055

src/Operation/ListIndexes.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Query;
77
use MongoDB\Driver\Server;
8+
use MongoDB\Driver\Exception\RuntimeException;
89
use MongoDB\Model\IndexInfoIterator;
910
use MongoDB\Model\IndexInfoIteratorIterator;
11+
use EmptyIterator;
1012

1113
/**
1214
* Operation for the listIndexes command.
@@ -17,6 +19,8 @@
1719
*/
1820
class ListIndexes implements Executable
1921
{
22+
private static $errorCodeDatabaseNotFound = 60;
23+
private static $errorCodeNamespaceNotFound = 26;
2024
private static $wireVersionForCommand = 3;
2125

2226
private $databaseName;
@@ -75,7 +79,20 @@ private function executeCommand(Server $server)
7579
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
7680
}
7781

78-
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
82+
try {
83+
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
84+
} catch (RuntimeException $e) {
85+
/* The server may return an error if the collection does not exist.
86+
* Check for possible error codes (see: SERVER-20463) and return an
87+
* empty iterator instead of throwing.
88+
*/
89+
if ($e->getCode() === self::$errorCodeNamespaceNotFound || $e->getCode() === self::$errorCodeDatabaseNotFound) {
90+
return new IndexInfoIteratorIterator(new EmptyIterator);
91+
}
92+
93+
throw $e;
94+
}
95+
7996
$cursor->setTypeMap(array('root' => 'array', 'document' => 'array'));
8097

8198
return new IndexInfoIteratorIterator($cursor);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Driver\Server;
6+
use MongoDB\Operation\DropDatabase;
7+
use MongoDB\Operation\ListCollections;
8+
9+
class ListCollectionsFunctionalTest extends FunctionalTestCase
10+
{
11+
public function testListCollectionsForNewlyCreatedDatabase()
12+
{
13+
$server = $this->getPrimaryServer();
14+
15+
$operation = new DropDatabase($this->getDatabaseName());
16+
$operation->execute($server);
17+
18+
$writeResult = $this->manager->executeInsert($this->getNamespace(), ['x' => 1]);
19+
$this->assertEquals(1, $writeResult->getInsertedCount());
20+
21+
$operation = new ListCollections($this->getDatabaseName(), ['filter' => ['name' => $this->getCollectionName()]]);
22+
// Convert the CollectionInfoIterator to an array since we cannot rewind its cursor
23+
$collections = iterator_to_array($operation->execute($server));
24+
25+
$this->assertCount(1, $collections);
26+
27+
foreach ($collections as $collection) {
28+
$this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
29+
$this->assertEquals($this->getCollectionName(), $collection->getName());
30+
}
31+
}
32+
33+
public function testListCollectionsForNonexistentDatabase()
34+
{
35+
$server = $this->getPrimaryServer();
36+
37+
$operation = new DropDatabase($this->getDatabaseName());
38+
$operation->execute($server);
39+
40+
$operation = new ListCollections($this->getDatabaseName());
41+
$collections = $operation->execute($server);
42+
43+
$this->assertCount(0, $collections);
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Driver\Server;
6+
use MongoDB\Operation\DropCollection;
7+
use MongoDB\Operation\ListIndexes;
8+
9+
class ListIndexesFunctionalTest extends FunctionalTestCase
10+
{
11+
public function testListIndexesForNewlyCreatedCollection()
12+
{
13+
$server = $this->getPrimaryServer();
14+
15+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
16+
$operation->execute($server);
17+
18+
$writeResult = $this->manager->executeInsert($this->getNamespace(), ['x' => 1]);
19+
$this->assertEquals(1, $writeResult->getInsertedCount());
20+
21+
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
22+
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
23+
$indexes = iterator_to_array($operation->execute($server));
24+
25+
$this->assertCount(1, $indexes);
26+
27+
foreach ($indexes as $index) {
28+
$this->assertInstanceOf('MongoDB\Model\IndexInfo', $index);
29+
$this->assertEquals(['_id' => 1], $index->getKey());
30+
}
31+
}
32+
33+
public function testListIndexesForNonexistentCollection()
34+
{
35+
$server = $this->getPrimaryServer();
36+
37+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
38+
$operation->execute($server);
39+
40+
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
41+
$indexes = $operation->execute($server);
42+
43+
$this->assertCount(0, $indexes);
44+
}
45+
}

0 commit comments

Comments
 (0)