Commit d25ea54d authored by Jens Segers's avatar Jens Segers

Ditch extract, update tests and fixes #1012

parent fa5b1158
...@@ -127,7 +127,7 @@ class Connection extends \Illuminate\Database\Connection ...@@ -127,7 +127,7 @@ class Connection extends \Illuminate\Database\Connection
* @param string $dsn * @param string $dsn
* @param array $config * @param array $config
* @param array $options * @param array $options
* @return MongoDB * @return \MongoDB\Client
*/ */
protected function createConnection($dsn, array $config, array $options) protected function createConnection($dsn, array $config, array $options)
{ {
...@@ -165,27 +165,25 @@ class Connection extends \Illuminate\Database\Connection ...@@ -165,27 +165,25 @@ class Connection extends \Illuminate\Database\Connection
*/ */
protected function getDsn(array $config) 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. // Check if the user passed a complete dsn to the configuration.
if (! empty($dsn)) { if (! empty($config['dsn'])) {
return $dsn; return $config['dsn'];
} }
// Treat host option as array of hosts // 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) { foreach ($hosts as &$host) {
// Check if we need to add a port to the host // Check if we need to add a port to the host
if (strpos($host, ':') === false and isset($port)) { if (strpos($host, ':') === false && ! empty($config['port'])) {
$host = "{$host}:{$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 : '');
} }
/** /**
......
...@@ -212,13 +212,17 @@ abstract class Model extends BaseModel ...@@ -212,13 +212,17 @@ abstract class Model extends BaseModel
*/ */
public function getAttribute($key) public function getAttribute($key)
{ {
// Check if the key is an array dot notation. if (! $key) {
if ($key and str_contains($key, '.') and array_has($this->attributes, $key)) { return;
}
// Dot notation support.
if (str_contains($key, '.') and array_has($this->attributes, $key)) {
return $this->getAttributeValue($key); return $this->getAttributeValue($key);
} }
// This checks for embedded relation support. // 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); return $this->getRelationValue($key);
} }
......
...@@ -100,33 +100,27 @@ class ConnectionTest extends TestCase ...@@ -100,33 +100,27 @@ class ConnectionTest extends TestCase
{ {
Config::set('database.connections.mongodb.username', 'foo'); Config::set('database.connections.mongodb.username', 'foo');
Config::set('database.connections.mongodb.password', 'bar'); Config::set('database.connections.mongodb.password', 'bar');
$host = Config::get('database.connections.mongodb.host'); Config::set('database.connections.mongodb.options.database', 'custom');
$port = Config::get('database.connections.mongodb.port', 27017);
$database = Config::get('database.connections.mongodb.database');
// $this->setExpectedExceptionRegExp('MongoConnectionException', "/Failed to connect to: $host:$port: Authentication failed on database '$database' with username 'foo': auth fail/");
$connection = DB::connection('mongodb'); $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.host', 'db1');
Config::set('database.connections.mongodb.port', $port); Config::set('database.connections.mongodb.port', 27000);
$host = Config::get('database.connections.mongodb.host');
$database = Config::get('database.connections.mongodb.database');
// $this->setExpectedException('MongoConnectionException', "Failed to connect to: $host:$port: Connection refused");
$connection = DB::connection('mongodb'); $connection = DB::connection('mongodb');
$this->assertEquals("mongodb://db1:27000", (string) $connection->getMongoClient());
} }
public function testHostWithPorts() public function testHostWithPorts()
{ {
$hosts = ['localhost:27001', 'localhost:27002'];
Config::set('database.connections.mongodb.port', 27000); Config::set('database.connections.mongodb.port', 27000);
Config::set('database.connections.mongodb.host', ['localhost:27001', 'localhost:27002']); Config::set('database.connections.mongodb.host', ['db1:27001', 'db2:27002', 'db3:27000']);
$database = Config::get('database.connections.mongodb.database');
// $this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
$connection = DB::connection('mongodb'); $connection = DB::connection('mongodb');
$this->assertEquals('mongodb://db1:27001,db2:27002,db3:27000', (string) $connection->getMongoClient());
} }
} }
...@@ -422,8 +422,8 @@ class QueryBuilderTest extends TestCase ...@@ -422,8 +422,8 @@ class QueryBuilderTest extends TestCase
public function testSubdocumentArrayAggregate() public function testSubdocumentArrayAggregate()
{ {
DB::collection('items')->insert([ DB::collection('items')->insert([
['name' => 'knife', 'amount' => [['hidden' => 10, 'found' => 3],['hidden' => 5, 'found' => 2]]], ['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' => 'fork', 'amount' => [['hidden' => 35, 'found' => 12], ['hidden' => 7, 'found' => 17], ['hidden' => 1, 'found' => 19]]],
['name' => 'spoon', 'amount' => [['hidden' => 14, 'found' => 21]]], ['name' => 'spoon', 'amount' => [['hidden' => 14, 'found' => 21]]],
['name' => 'teaspoon', 'amount' => []], ['name' => 'teaspoon', 'amount' => []],
]); ]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment