Skip to content

Commit

Permalink
Ditch extract, update tests and fixes #1012
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Oct 31, 2016
1 parent fa5b115 commit d25ea54
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
22 changes: 10 additions & 12 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function getMongoClient()
* @param string $dsn
* @param array $config
* @param array $options
* @return MongoDB
* @return \MongoDB\Client
*/
protected function createConnection($dsn, array $config, array $options)
{
Expand Down Expand Up @@ -165,27 +165,25 @@ public function disconnect()
*/
protected function getDsn(array $config)
{
// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the MongoDB and return them back for use.
extract($config);

// Check if the user passed a complete dsn to the configuration.
if (! empty($dsn)) {
return $dsn;
if (! empty($config['dsn'])) {
return $config['dsn'];
}

// Treat host option as array of hosts
$hosts = is_array($host) ? $host : [$host];
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];

foreach ($hosts as &$host) {
// Check if we need to add a port to the host
if (strpos($host, ':') === false and isset($port)) {
$host = "{$host}:{$port}";
if (strpos($host, ':') === false && ! empty($config['port'])) {
$host = $host . ':' . $config['port'];
}
}

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

return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,17 @@ public function getTable()
*/
public function getAttribute($key)
{
// Check if the key is an array dot notation.
if ($key and str_contains($key, '.') and array_has($this->attributes, $key)) {
if (! $key) {
return;
}

// Dot notation support.
if (str_contains($key, '.') and array_has($this->attributes, $key)) {
return $this->getAttributeValue($key);
}

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

Expand Down
22 changes: 8 additions & 14 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,27 @@ public function testAuth()
{
Config::set('database.connections.mongodb.username', 'foo');
Config::set('database.connections.mongodb.password', 'bar');
$host = Config::get('database.connections.mongodb.host');
$port = Config::get('database.connections.mongodb.port', 27017);
$database = Config::get('database.connections.mongodb.database');
Config::set('database.connections.mongodb.options.database', 'custom');

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

public function testCustomPort()
public function testCustomHostAndPort()
{
$port = 27000;
Config::set('database.connections.mongodb.port', $port);
$host = Config::get('database.connections.mongodb.host');
$database = Config::get('database.connections.mongodb.database');
Config::set('database.connections.mongodb.host', 'db1');
Config::set('database.connections.mongodb.port', 27000);

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

public function testHostWithPorts()
{
$hosts = ['localhost:27001', 'localhost:27002'];
Config::set('database.connections.mongodb.port', 27000);
Config::set('database.connections.mongodb.host', ['localhost:27001', 'localhost:27002']);
$database = Config::get('database.connections.mongodb.database');
Config::set('database.connections.mongodb.host', ['db1:27001', 'db2:27002', 'db3:27000']);

// $this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
$connection = DB::connection('mongodb');
$this->assertEquals('mongodb://db1:27001,db2:27002,db3:27000', (string) $connection->getMongoClient());
}
}
4 changes: 2 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ public function testSubdocumentAggregate()
public function testSubdocumentArrayAggregate()
{
DB::collection('items')->insert([
['name' => 'knife', 'amount' => [['hidden' => 10, 'found' => 3],['hidden' => 5, 'found' => 2]]],
['name' => 'fork', 'amount' => [['hidden' => 35, 'found' => 12],['hidden' => 7, 'found' => 17],['hidden' => 1, 'found' => 19]]],
['name' => 'knife', 'amount' => [['hidden' => 10, 'found' => 3], ['hidden' => 5, 'found' => 2]]],
['name' => 'fork', 'amount' => [['hidden' => 35, 'found' => 12], ['hidden' => 7, 'found' => 17], ['hidden' => 1, 'found' => 19]]],
['name' => 'spoon', 'amount' => [['hidden' => 14, 'found' => 21]]],
['name' => 'teaspoon', 'amount' => []],
]);
Expand Down

0 comments on commit d25ea54

Please sign in to comment.