@@ -37,11 +37,93 @@ public function testDb()
37
37
$ this ->assertInstanceOf (Client::class, $ connection ->getMongoClient ());
38
38
}
39
39
40
- public function testDsnDb ()
40
+ public function dataConnectionConfig (): Generator
41
41
{
42
- $ connection = DB ::connection ('dsn_mongodb_db ' );
43
- $ this ->assertInstanceOf (Database::class, $ connection ->getMongoDB ());
44
- $ this ->assertInstanceOf (Client::class, $ connection ->getMongoClient ());
42
+ yield 'Single host ' => [
43
+ 'expectedUri ' => 'mongodb://some-host ' ,
44
+ 'expectedDatabaseName ' => 'tests ' ,
45
+ 'config ' => [
46
+ 'host ' => 'some-host ' ,
47
+ 'database ' => 'tests ' ,
48
+ ],
49
+ ];
50
+
51
+ yield 'Host and port ' => [
52
+ 'expectedUri ' => 'mongodb://some-host:12345 ' ,
53
+ 'expectedDatabaseName ' => 'tests ' ,
54
+ 'config ' => [
55
+ 'host ' => 'some-host ' ,
56
+ 'port ' => 12345 ,
57
+ 'database ' => 'tests ' ,
58
+ ],
59
+ ];
60
+
61
+ yield 'Port in host name takes precedence ' => [
62
+ 'expectedUri ' => 'mongodb://some-host:12345 ' ,
63
+ 'expectedDatabaseName ' => 'tests ' ,
64
+ 'config ' => [
65
+ 'host ' => 'some-host:12345 ' ,
66
+ 'port ' => 54321 ,
67
+ 'database ' => 'tests ' ,
68
+ ],
69
+ ];
70
+
71
+ yield 'Multiple hosts ' => [
72
+ 'expectedUri ' => 'mongodb://host-1,host-2 ' ,
73
+ 'expectedDatabaseName ' => 'tests ' ,
74
+ 'config ' => [
75
+ 'host ' => ['host-1 ' , 'host-2 ' ],
76
+ 'database ' => 'tests ' ,
77
+ ],
78
+ ];
79
+
80
+ yield 'Multiple hosts with same port ' => [
81
+ 'expectedUri ' => 'mongodb://host-1:12345,host-2:12345 ' ,
82
+ 'expectedDatabaseName ' => 'tests ' ,
83
+ 'config ' => [
84
+ 'host ' => ['host-1 ' , 'host-2 ' ],
85
+ 'port ' => 12345 ,
86
+ 'database ' => 'tests ' ,
87
+ ],
88
+ ];
89
+
90
+ yield 'Multiple hosts with port ' => [
91
+ 'expectedUri ' => 'mongodb://host-1:12345,host-2:54321 ' ,
92
+ 'expectedDatabaseName ' => 'tests ' ,
93
+ 'config ' => [
94
+ 'host ' => ['host-1:12345 ' , 'host-2:54321 ' ],
95
+ 'database ' => 'tests ' ,
96
+ ],
97
+ ];
98
+
99
+ yield 'DSN takes precedence over host/port config ' => [
100
+ 'expectedUri ' => 'mongodb://some-host:12345/auth-database ' ,
101
+ 'expectedDatabaseName ' => 'tests ' ,
102
+ 'config ' => [
103
+ 'dsn ' => 'mongodb://some-host:12345/auth-database ' ,
104
+ 'host ' => 'wrong-host ' ,
105
+ 'port ' => 54321 ,
106
+ 'database ' => 'tests ' ,
107
+ ],
108
+ ];
109
+ }
110
+
111
+ /** @dataProvider dataConnectionConfig */
112
+ public function testConnectionConfig (string $ expectedUri , string $ expectedDatabaseName , array $ config ): void
113
+ {
114
+ $ connection = new Connection ($ config );
115
+ $ client = $ connection ->getMongoClient ();
116
+
117
+ $ this ->assertSame ($ expectedUri , (string ) $ client );
118
+ $ this ->assertSame ($ expectedDatabaseName , $ connection ->getMongoDB ()->getDatabaseName ());
119
+ }
120
+
121
+ public function testConnectionWithoutConfiguredDatabase (): void
122
+ {
123
+ $ this ->expectException (InvalidArgumentException::class);
124
+ $ this ->expectExceptionMessage ('Database is not properly configured. ' );
125
+
126
+ new Connection (['dsn ' => 'mongodb://some-host ' ]);
45
127
}
46
128
47
129
public function testCollection ()
@@ -89,33 +171,4 @@ public function testDriverName()
89
171
$ driver = DB ::connection ('mongodb ' )->getDriverName ();
90
172
$ this ->assertEquals ('mongodb ' , $ driver );
91
173
}
92
-
93
- public function testAuth ()
94
- {
95
- $ host = Config::get ('database.connections.mongodb.host ' );
96
- Config::set ('database.connections.mongodb.username ' , 'foo ' );
97
- Config::set ('database.connections.mongodb.password ' , 'bar ' );
98
- Config::set ('database.connections.mongodb.options.database ' , 'custom ' );
99
-
100
- $ connection = DB ::connection ('mongodb ' );
101
- $ this ->assertEquals ('mongodb:// ' .$ host .'/custom ' , (string ) $ connection ->getMongoClient ());
102
- }
103
-
104
- public function testCustomHostAndPort ()
105
- {
106
- Config::set ('database.connections.mongodb.host ' , 'db1 ' );
107
- Config::set ('database.connections.mongodb.port ' , 27000 );
108
-
109
- $ connection = DB ::connection ('mongodb ' );
110
- $ this ->assertEquals ('mongodb://db1:27000 ' , (string ) $ connection ->getMongoClient ());
111
- }
112
-
113
- public function testHostWithPorts ()
114
- {
115
- Config::set ('database.connections.mongodb.port ' , 27000 );
116
- Config::set ('database.connections.mongodb.host ' , ['db1:27001 ' , 'db2:27002 ' , 'db3:27000 ' ]);
117
-
118
- $ connection = DB ::connection ('mongodb ' );
119
- $ this ->assertEquals ('mongodb://db1:27001,db2:27002,db3:27000 ' , (string ) $ connection ->getMongoClient ());
120
- }
121
174
}
0 commit comments