diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 9f217f8c8..2ec6ed7d8 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -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) { @@ -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 : ''); } /** diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index 82d5156ca..ce4003dec 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -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); } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index fb311c91f..772148e90 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -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()); } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 9d45b17ba..988667d09 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -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' => []], ]);