Skip to content

Commit f8a18a2

Browse files
committed
PHPLIB-71: Collection drop methods
1 parent f1b1cd4 commit f8a18a2

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/Collection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,15 @@ public function distinct($fieldName, array $filter = array(), array $options = a
334334
/**
335335
* Drop this collection.
336336
*
337+
* @see http://docs.mongodb.org/manual/reference/command/drop/
337338
* @return Result
338339
*/
339340
public function drop()
340341
{
341-
// TODO
342+
$command = new Command(array('drop' => $this->collname));
343+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
344+
345+
return $this->manager->executeCommand($this->dbname, $command, $readPreference);
342346
}
343347

344348
/**

src/Database.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ public function drop()
6666
/**
6767
* Drop a collection within this database.
6868
*
69+
* @see http://docs.mongodb.org/manual/reference/command/drop/
6970
* @param string $collectionName
7071
* @return Result
7172
*/
7273
public function dropCollection($collectionName)
7374
{
74-
// TODO
75+
$collectionName = (string) $collectionName;
76+
$command = new Command(array('drop' => $collectionName));
77+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
78+
79+
return $this->manager->executeCommand($this->databaseName, $command, $readPreference);
7580
}
7681

7782
/**

tests/CollectionFunctionalTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public function setUp()
1717
$this->collection->deleteMany(array());
1818
}
1919

20+
public function testDrop()
21+
{
22+
$writeResult = $this->collection->insertOne(array('x' => 1));
23+
$this->assertEquals(1, $writeResult->getInsertedCount());
24+
25+
$commandResult = $this->collection->drop();
26+
$this->assertCommandSucceeded($commandResult);
27+
$this->assertCollectionCount($this->getNamespace(), 0);
28+
}
29+
2030
function testInsertAndRetrieve()
2131
{
2232
$generator = new FixtureGenerator();

tests/DatabaseFunctionalTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,15 @@ public function testDrop()
2020
$this->assertCommandSucceeded($commandResult);
2121
$this->assertCollectionCount($this->getNamespace(), 0);
2222
}
23+
24+
public function testDropCollection()
25+
{
26+
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
27+
$this->assertEquals(1, $writeResult->getInsertedCount());
28+
29+
$database = new Database($this->manager, $this->getDatabaseName());
30+
$commandResult = $database->dropCollection($this->getCollectionName());
31+
$this->assertCommandSucceeded($commandResult);
32+
$this->assertCollectionCount($this->getNamespace(), 0);
33+
}
2334
}

0 commit comments

Comments
 (0)