Skip to content

Commit d25ea54

Browse files
committed
Ditch extract, update tests and fixes #1012
1 parent fa5b115 commit d25ea54

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

src/Jenssegers/Mongodb/Connection.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function getMongoClient()
127127
* @param string $dsn
128128
* @param array $config
129129
* @param array $options
130-
* @return MongoDB
130+
* @return \MongoDB\Client
131131
*/
132132
protected function createConnection($dsn, array $config, array $options)
133133
{
@@ -165,27 +165,25 @@ public function disconnect()
165165
*/
166166
protected function getDsn(array $config)
167167
{
168-
// First we will create the basic DSN setup as well as the port if it is in
169-
// in the configuration options. This will give us the basic DSN we will
170-
// need to establish the MongoDB and return them back for use.
171-
extract($config);
172-
173168
// Check if the user passed a complete dsn to the configuration.
174-
if (! empty($dsn)) {
175-
return $dsn;
169+
if (! empty($config['dsn'])) {
170+
return $config['dsn'];
176171
}
177172

178173
// Treat host option as array of hosts
179-
$hosts = is_array($host) ? $host : [$host];
174+
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];
180175

181176
foreach ($hosts as &$host) {
182177
// Check if we need to add a port to the host
183-
if (strpos($host, ':') === false and isset($port)) {
184-
$host = "{$host}:{$port}";
178+
if (strpos($host, ':') === false && ! empty($config['port'])) {
179+
$host = $host . ':' . $config['port'];
185180
}
186181
}
187182

188-
return "mongodb://" . implode(',', $hosts) . ($database ? "/{$database}" : '');
183+
// Check if we want to authenticate against a specific database.
184+
$auth_database = isset($config['options']) && ! empty($config['options']['database']) ? $config['options']['database'] : null;
185+
186+
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
189187
}
190188

191189
/**

src/Jenssegers/Mongodb/Eloquent/Model.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,17 @@ public function getTable()
212212
*/
213213
public function getAttribute($key)
214214
{
215-
// Check if the key is an array dot notation.
216-
if ($key and str_contains($key, '.') and array_has($this->attributes, $key)) {
215+
if (! $key) {
216+
return;
217+
}
218+
219+
// Dot notation support.
220+
if (str_contains($key, '.') and array_has($this->attributes, $key)) {
217221
return $this->getAttributeValue($key);
218222
}
219223

220224
// This checks for embedded relation support.
221-
if (method_exists($this, $key) && ! method_exists(self::class, $key)) {
225+
if (method_exists($this, $key) and ! method_exists(self::class, $key)) {
222226
return $this->getRelationValue($key);
223227
}
224228

tests/ConnectionTest.php

+8-14
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,27 @@ public function testAuth()
100100
{
101101
Config::set('database.connections.mongodb.username', 'foo');
102102
Config::set('database.connections.mongodb.password', 'bar');
103-
$host = Config::get('database.connections.mongodb.host');
104-
$port = Config::get('database.connections.mongodb.port', 27017);
105-
$database = Config::get('database.connections.mongodb.database');
103+
Config::set('database.connections.mongodb.options.database', 'custom');
106104

107-
// $this->setExpectedExceptionRegExp('MongoConnectionException', "/Failed to connect to: $host:$port: Authentication failed on database '$database' with username 'foo': auth fail/");
108105
$connection = DB::connection('mongodb');
106+
$this->assertEquals('mongodb://127.0.0.1/custom', (string) $connection->getMongoClient());
109107
}
110108

111-
public function testCustomPort()
109+
public function testCustomHostAndPort()
112110
{
113-
$port = 27000;
114-
Config::set('database.connections.mongodb.port', $port);
115-
$host = Config::get('database.connections.mongodb.host');
116-
$database = Config::get('database.connections.mongodb.database');
111+
Config::set('database.connections.mongodb.host', 'db1');
112+
Config::set('database.connections.mongodb.port', 27000);
117113

118-
// $this->setExpectedException('MongoConnectionException', "Failed to connect to: $host:$port: Connection refused");
119114
$connection = DB::connection('mongodb');
115+
$this->assertEquals("mongodb://db1:27000", (string) $connection->getMongoClient());
120116
}
121117

122118
public function testHostWithPorts()
123119
{
124-
$hosts = ['localhost:27001', 'localhost:27002'];
125120
Config::set('database.connections.mongodb.port', 27000);
126-
Config::set('database.connections.mongodb.host', ['localhost:27001', 'localhost:27002']);
127-
$database = Config::get('database.connections.mongodb.database');
121+
Config::set('database.connections.mongodb.host', ['db1:27001', 'db2:27002', 'db3:27000']);
128122

129-
// $this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
130123
$connection = DB::connection('mongodb');
124+
$this->assertEquals('mongodb://db1:27001,db2:27002,db3:27000', (string) $connection->getMongoClient());
131125
}
132126
}

tests/QueryBuilderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ public function testSubdocumentAggregate()
422422
public function testSubdocumentArrayAggregate()
423423
{
424424
DB::collection('items')->insert([
425-
['name' => 'knife', 'amount' => [['hidden' => 10, 'found' => 3],['hidden' => 5, 'found' => 2]]],
426-
['name' => 'fork', 'amount' => [['hidden' => 35, 'found' => 12],['hidden' => 7, 'found' => 17],['hidden' => 1, 'found' => 19]]],
425+
['name' => 'knife', 'amount' => [['hidden' => 10, 'found' => 3], ['hidden' => 5, 'found' => 2]]],
426+
['name' => 'fork', 'amount' => [['hidden' => 35, 'found' => 12], ['hidden' => 7, 'found' => 17], ['hidden' => 1, 'found' => 19]]],
427427
['name' => 'spoon', 'amount' => [['hidden' => 14, 'found' => 21]]],
428428
['name' => 'teaspoon', 'amount' => []],
429429
]);

0 commit comments

Comments
 (0)