Skip to content

Commit 10ea91c

Browse files
committed
PHPLIB-65: Database drop methods
1 parent b85b91f commit 10ea91c

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

src/Client.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace MongoDB;
44

5-
use MongoDB\Collection;
6-
use MongoDB\Database;
5+
use MongoDB\Driver\Command;
76
use MongoDB\Driver\Manager;
87
use MongoDB\Driver\ReadPreference;
98
use MongoDB\Driver\Result;
@@ -35,12 +34,17 @@ public function __construct($uri, array $options = array(), array $driverOptions
3534
/**
3635
* Drop a database.
3736
*
37+
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
3838
* @param string $databaseName
3939
* @return Result
4040
*/
4141
public function dropDatabase($databaseName)
4242
{
43-
// TODO
43+
$databaseName = (string) $databaseName;
44+
$command = new Command(array('dropDatabase' => 1));
45+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
46+
47+
return $this->manager->executeCommand($databaseName, $command, $readPreference);
4448
}
4549

4650
/**

src/Database.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB;
44

55
use MongoDB\Collection;
6+
use MongoDB\Driver\Command;
67
use MongoDB\Driver\Manager;
78
use MongoDB\Driver\ReadPreference;
89
use MongoDB\Driver\Result;
@@ -51,11 +52,15 @@ public function createCollection($collectionName, array $options = array())
5152
/**
5253
* Drop this database.
5354
*
55+
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
5456
* @return Result
5557
*/
5658
public function drop()
5759
{
58-
// TODO
60+
$command = new Command(array('dropDatabase' => 1));
61+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
62+
63+
return $this->manager->executeCommand($this->databaseName, $command, $readPreference);
5964
}
6065

6166
/**

tests/ClientFunctionalTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Client;
6+
7+
/**
8+
* Functional tests for the Client class.
9+
*/
10+
class ClientFunctionalTest extends FunctionalTestCase
11+
{
12+
public function testDropDatabase()
13+
{
14+
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
15+
$this->assertEquals(1, $writeResult->getInsertedCount());
16+
17+
$client = new Client($this->getUri());
18+
$commandResult = $client->dropDatabase($this->getDatabaseName());
19+
$this->assertCommandSucceeded($commandResult);
20+
$this->assertCollectionCount($this->getNamespace(), 0);
21+
}
22+
}

tests/DatabaseFunctionalTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Client;
6+
use MongoDB\Database;
7+
8+
/**
9+
* Functional tests for the Database class.
10+
*/
11+
class DatabaseFunctionalTest extends FunctionalTestCase
12+
{
13+
public function testDrop()
14+
{
15+
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
16+
$this->assertEquals(1, $writeResult->getInsertedCount());
17+
18+
$database = new Database($this->manager, $this->getDatabaseName());
19+
$commandResult = $database->drop();
20+
$this->assertCommandSucceeded($commandResult);
21+
$this->assertCollectionCount($this->getNamespace(), 0);
22+
}
23+
}

tests/FunctionalTestCase.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace MongoDB\Tests;
44

5+
use MongoDB\Driver\Command;
56
use MongoDB\Driver\Manager;
7+
use MongoDB\Driver\Result;
68

79
abstract class FunctionalTestCase extends TestCase
810
{
@@ -12,4 +14,22 @@ public function setUp()
1214
{
1315
$this->manager = new Manager($this->getUri());
1416
}
17+
18+
public function assertCollectionCount($namespace, $count)
19+
{
20+
list($databaseName, $collectionName) = explode('.', $namespace, 2);
21+
22+
$result = $this->manager->executeCommand($databaseName, new Command(array('count' => $collectionName)));
23+
24+
$document = $result->toArray();
25+
$this->assertArrayHasKey('n', $document);
26+
$this->assertEquals($count, $document['n']);
27+
}
28+
29+
public function assertCommandSucceeded(Result $result)
30+
{
31+
$document = $result->toArray();
32+
$this->assertArrayHasKey('ok', $document);
33+
$this->assertEquals(1, $document['ok']);
34+
}
1535
}

0 commit comments

Comments
 (0)