-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathMongoClientTest.php
118 lines (97 loc) · 3.51 KB
/
MongoClientTest.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
use Alcaeus\MongoDbAdapter\Tests\TestCase;
/**
* @author alcaeus <alcaeus@alcaeus.org>
*/
class MongoClientTest extends TestCase
{
public function testSerialize()
{
$this->assertInternalType('string', serialize($this->getClient()));
}
public function testGetDb()
{
$client = $this->getClient();
$db = $client->selectDB('mongo-php-adapter');
$this->assertInstanceOf('\MongoDB', $db);
$this->assertSame('mongo-php-adapter', (string) $db);
}
public function testSelectDBWithEmptyName()
{
$this->setExpectedException('Exception', 'Database name cannot be empty');
$this->getClient()->selectDB('');
}
public function testSelectDBWithInvalidName()
{
$this->setExpectedException('Exception', 'Database name contains invalid characters');
$this->getClient()->selectDB('/');
}
public function testGetDbProperty()
{
$client = $this->getClient();
$db = $client->{'mongo-php-adapter'};
$this->assertInstanceOf('\MongoDB', $db);
$this->assertSame('mongo-php-adapter', (string) $db);
}
public function testGetCollection()
{
$client = $this->getClient();
$collection = $client->selectCollection('mongo-php-adapter', 'test');
$this->assertInstanceOf('MongoCollection', $collection);
$this->assertSame('mongo-php-adapter.test', (string) $collection);
}
public function testGetHosts()
{
$client = $this->getClient();
$hosts = $client->getHosts();
$this->assertArraySubset(
[
'localhost:27017;-;.;' . getmypid() => [
'host' => 'localhost',
'port' => 27017,
'health' => 1,
'state' => 0,
],
],
$hosts
);
}
public function testReadPreference()
{
$client = $this->getClient();
$this->assertSame(['type' => \MongoClient::RP_PRIMARY], $client->getReadPreference());
$this->assertTrue($client->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
$this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $client->getReadPreference());
}
public function testWriteConcern()
{
$client = $this->getClient();
$this->assertTrue($client->setWriteConcern('majority', 100));
$this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
}
public function testListDBs()
{
$document = ['foo' => 'bar'];
$this->getCollection()->insert($document);
$databases = $this->getClient()->listDBs();
$this->assertSame(1.0, $databases['ok']);
$this->assertArrayHasKey('totalSize', $databases);
$this->assertArrayHasKey('databases', $databases);
foreach ($databases['databases'] as $database) {
$this->assertArrayHasKey('name', $database);
$this->assertArrayHasKey('empty', $database);
$this->assertArrayHasKey('sizeOnDisk', $database);
if ($database['name'] == 'mongo-php-adapter') {
$this->assertFalse($database['empty']);
return;
}
}
$this->fail('Could not find mongo-php-adapter database in list');
}
public function testNoPrefixUri()
{
$client = $this->getClient(null, 'localhost');
$this->assertNotNull($client);
}
}