1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace MongoDB\Tests;
use MongoDB\Client;
use MongoDB\Driver\Command;
use MongoDB\Model\DatabaseInfo;
/**
* Functional tests for the Client class.
*/
class ClientFunctionalTest extends FunctionalTestCase
{
private $client;
public function setUp()
{
parent::setUp();
$this->client = new Client($this->getUri());
$this->client->dropDatabase($this->getDatabaseName());
}
public function testDropDatabase()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$commandResult = $this->client->dropDatabase($this->getDatabaseName());
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
public function testListDatabases()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$databases = $this->client->listDatabases();
$this->assertInstanceOf('MongoDB\Model\DatabaseInfoIterator', $databases);
foreach ($databases as $database) {
$this->assertInstanceOf('MongoDB\Model\DatabaseInfo', $database);
}
$that = $this;
$this->assertDatabaseExists($this->getDatabaseName(), function(DatabaseInfo $info) use ($that) {
$that->assertFalse($info->isEmpty());
$that->assertGreaterThan(0, $info->getSizeOnDisk());
});
}
/**
* Asserts that a database with the given name exists on the server.
*
* An optional $callback may be provided, which should take a DatabaseInfo
* argument as its first and only parameter. If a DatabaseInfo matching
* the given name is found, it will be passed to the callback, which may
* perform additional assertions.
*
* @param callable $callback
*/
private function assertDatabaseExists($databaseName, $callback = null)
{
if ($callback !== null && ! is_callable($callback)) {
throw new InvalidArgumentException('$callback is not a callable');
}
$databases = $this->client->listDatabases();
$foundDatabase = null;
foreach ($databases as $database) {
if ($database->getName() === $databaseName) {
$foundDatabase = $database;
break;
}
}
$this->assertNotNull($foundDatabase, sprintf('Found %s database on the server', $databaseName));
if ($callback !== null) {
call_user_func($callback, $foundDatabase);
}
}
}