forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionTest.php
261 lines (219 loc) · 8.23 KB
/
ConnectionTest.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
declare(strict_types=1);
namespace MongoDB\Laravel\Tests;
use Generator;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use MongoDB\Client;
use MongoDB\Database;
use MongoDB\Driver\Exception\ConnectionTimeoutException;
use MongoDB\Laravel\Collection;
use MongoDB\Laravel\Connection;
use MongoDB\Laravel\Query\Builder;
use MongoDB\Laravel\Schema\Builder as SchemaBuilder;
use function env;
use function spl_object_hash;
class ConnectionTest extends TestCase
{
public function testConnection()
{
$connection = DB::connection('mongodb');
$this->assertInstanceOf(Connection::class, $connection);
}
public function testReconnect()
{
$c1 = DB::connection('mongodb');
$c2 = DB::connection('mongodb');
$this->assertEquals(spl_object_hash($c1), spl_object_hash($c2));
$c1 = DB::connection('mongodb');
DB::purge('mongodb');
$c2 = DB::connection('mongodb');
$this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2));
}
public function testDb()
{
$connection = DB::connection('mongodb');
$this->assertInstanceOf(Database::class, $connection->getMongoDB());
$this->assertInstanceOf(Client::class, $connection->getMongoClient());
}
public static function dataConnectionConfig(): Generator
{
yield 'Single host' => [
'expectedUri' => 'mongodb://some-host',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => 'some-host',
'database' => 'tests',
],
];
yield 'Host and port' => [
'expectedUri' => 'mongodb://some-host:12345',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => 'some-host',
'port' => 12345,
'database' => 'tests',
],
];
yield 'IPv4' => [
'expectedUri' => 'mongodb://1.2.3.4',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '1.2.3.4',
'database' => 'tests',
],
];
yield 'IPv4 and port' => [
'expectedUri' => 'mongodb://1.2.3.4:1234',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '1.2.3.4',
'port' => 1234,
'database' => 'tests',
],
];
yield 'IPv6' => [
'expectedUri' => 'mongodb://[2001:db8:3333:4444:5555:6666:7777:8888]',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '2001:db8:3333:4444:5555:6666:7777:8888',
'database' => 'tests',
],
];
yield 'IPv6 and port' => [
'expectedUri' => 'mongodb://[2001:db8:3333:4444:5555:6666:7777:8888]:1234',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '2001:db8:3333:4444:5555:6666:7777:8888',
'port' => 1234,
'database' => 'tests',
],
];
yield 'multiple IPv6' => [
'expectedUri' => 'mongodb://[::1],[2001:db8::1:0:0:1]',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => ['::1', '2001:db8::1:0:0:1'],
'port' => null,
'database' => 'tests',
],
];
yield 'Port in host name takes precedence' => [
'expectedUri' => 'mongodb://some-host:12345',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => 'some-host:12345',
'port' => 54321,
'database' => 'tests',
],
];
yield 'Multiple hosts' => [
'expectedUri' => 'mongodb://host-1,host-2',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => ['host-1', 'host-2'],
'database' => 'tests',
],
];
yield 'Multiple hosts with same port' => [
'expectedUri' => 'mongodb://host-1:12345,host-2:12345',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => ['host-1', 'host-2'],
'port' => 12345,
'database' => 'tests',
],
];
yield 'Multiple hosts with port' => [
'expectedUri' => 'mongodb://host-1:12345,host-2:54321',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => ['host-1:12345', 'host-2:54321'],
'database' => 'tests',
],
];
yield 'DSN takes precedence over host/port config' => [
'expectedUri' => 'mongodb://some-host:12345/auth-database',
'expectedDatabaseName' => 'tests',
'config' => [
'dsn' => 'mongodb://some-host:12345/auth-database',
'host' => 'wrong-host',
'port' => 54321,
'database' => 'tests',
],
];
yield 'Database is extracted from DSN if not specified' => [
'expectedUri' => 'mongodb://some-host:12345/tests',
'expectedDatabaseName' => 'tests',
'config' => ['dsn' => 'mongodb://some-host:12345/tests'],
];
}
/** @dataProvider dataConnectionConfig */
public function testConnectionConfig(string $expectedUri, string $expectedDatabaseName, array $config): void
{
$connection = new Connection($config);
$client = $connection->getMongoClient();
$this->assertSame($expectedUri, (string) $client);
$this->assertSame($expectedDatabaseName, $connection->getMongoDB()->getDatabaseName());
}
public function testConnectionWithoutConfiguredDatabase(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Database is not properly configured.');
new Connection(['dsn' => 'mongodb://some-host']);
}
public function testCollection()
{
$collection = DB::connection('mongodb')->getCollection('unittest');
$this->assertInstanceOf(Collection::class, $collection);
$collection = DB::connection('mongodb')->collection('unittests');
$this->assertInstanceOf(Builder::class, $collection);
$collection = DB::connection('mongodb')->table('unittests');
$this->assertInstanceOf(Builder::class, $collection);
}
public function testQueryLog()
{
DB::enableQueryLog();
$this->assertCount(0, DB::getQueryLog());
DB::collection('items')->get();
$this->assertCount(1, DB::getQueryLog());
DB::collection('items')->insert(['name' => 'test']);
$this->assertCount(2, DB::getQueryLog());
DB::collection('items')->count();
$this->assertCount(3, DB::getQueryLog());
DB::collection('items')->where('name', 'test')->update(['name' => 'test']);
$this->assertCount(4, DB::getQueryLog());
DB::collection('items')->where('name', 'test')->delete();
$this->assertCount(5, DB::getQueryLog());
}
public function testSchemaBuilder()
{
$schema = DB::connection('mongodb')->getSchemaBuilder();
$this->assertInstanceOf(SchemaBuilder::class, $schema);
}
public function testDriverName()
{
$driver = DB::connection('mongodb')->getDriverName();
$this->assertEquals('mongodb', $driver);
}
public function testPingMethod()
{
$config = [
'name' => 'mongodb',
'driver' => 'mongodb',
'dsn' => env('MONGODB_URI', 'mongodb://127.0.0.1/'),
'database' => 'unittest',
'options' => [
'connectTimeoutMS' => 100,
'serverSelectionTimeoutMS' => 250,
],
];
$instance = new Connection($config);
$instance->ping();
$this->expectException(ConnectionTimeoutException::class);
$this->expectExceptionMessage("No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'wrong-host']");
$config['dsn'] = 'mongodb://wrong-host/';
$instance = new Connection($config);
$instance->ping();
}
}