From 8eadb9bc8dd3163da51463dd07a9a5f910f23be7 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 13 Nov 2023 15:45:33 +0330 Subject: [PATCH 1/7] New ping method created; --- src/Connection.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Connection.php b/src/Connection.php index d802e83f6..419ee404a 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -9,6 +9,10 @@ use InvalidArgumentException; use MongoDB\Client; use MongoDB\Database; +use MongoDB\Driver\Exception\AuthenticationException; +use MongoDB\Driver\Exception\ConnectionException; +use MongoDB\Driver\Exception\RuntimeException; +use MongoDB\Driver\ReadPreference; use MongoDB\Laravel\Concerns\ManagesTransactions; use Throwable; @@ -189,6 +193,21 @@ protected function createConnection(string $dsn, array $config, array $options): return new Client($dsn, $options, $driverOptions); } + /** + * Check the connection to the defined MongoDB host + * + * @return void + * + * @throws ConnectionException if connection to the server fails (for reasons other than authentication). + * @throws AuthenticationException if authentication is needed and fails. + * @throws RuntimeException if a server matching the read preference could not be found. + * @throws \MongoDB\Driver\Exception\InvalidArgumentException on argument parsing errors. + */ + public function ping(): void + { + $this->getMongoClient()->getManager()->selectServer(new ReadPreference(ReadPreference::PRIMARY_PREFERRED)); + } + /** @inheritdoc */ public function disconnect() { From 7aee2be20dee90db72b06a7d514e88b421572176 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 13 Nov 2023 15:45:44 +0330 Subject: [PATCH 2/7] Add test for ping method; --- tests/ConnectionTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index b46168df8..9368d2763 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -9,6 +9,7 @@ use InvalidArgumentException; use MongoDB\Client; use MongoDB\Database; +use MongoDB\Driver\Exception\ConnectionTimeoutException; use MongoDB\Laravel\Collection; use MongoDB\Laravel\Connection; use MongoDB\Laravel\Query\Builder; @@ -231,4 +232,29 @@ public function testDriverName() $driver = DB::connection('mongodb')->getDriverName(); $this->assertEquals('mongodb', $driver); } + + public function testPingMethod() + { + $config = [ + 'name' => 'mongodb', + 'driver' => 'mongodb', + 'dsn' => 'mongodb://mongodb/', + 'database' => 'unittest', + 'options' => [ + 'connectTimeoutMS' => 100, + 'serverSelectionTimeoutMS' => 250, + ], + ]; + + $instance = new Connection($config); + $instance->ping(); + + $this->expectException(ConnectionTimeoutException::class); + $this->expectExceptionMessage("No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'wrong-host']"); + + $config['dsn'] = 'mongodb://wrong-host/'; + + $instance = new Connection($config); + $instance->ping(); + } } From 6d561dbcd1d75d6e45cbd87de53170e8a84a2cb0 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 13 Nov 2023 16:12:02 +0330 Subject: [PATCH 3/7] Update ConnectionTest.php --- tests/ConnectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 9368d2763..c151dc09b 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -238,7 +238,7 @@ public function testPingMethod() $config = [ 'name' => 'mongodb', 'driver' => 'mongodb', - 'dsn' => 'mongodb://mongodb/', + 'dsn' => env('MONGODB_URI','mongodb://mongodb/'), 'database' => 'unittest', 'options' => [ 'connectTimeoutMS' => 100, From 57edbf55b0d2e3208ef6703d45807455d83a6937 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 13 Nov 2023 16:12:35 +0330 Subject: [PATCH 4/7] Fix CS; --- tests/ConnectionTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index c151dc09b..d6e601732 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -15,6 +15,7 @@ use MongoDB\Laravel\Query\Builder; use MongoDB\Laravel\Schema\Builder as SchemaBuilder; +use function env; use function spl_object_hash; class ConnectionTest extends TestCase @@ -238,7 +239,7 @@ public function testPingMethod() $config = [ 'name' => 'mongodb', 'driver' => 'mongodb', - 'dsn' => env('MONGODB_URI','mongodb://mongodb/'), + 'dsn' => env('MONGODB_URI', 'mongodb://mongodb/'), 'database' => 'unittest', 'options' => [ 'connectTimeoutMS' => 100, From db3049a14168f4ea7031f2abd5b04931c9a59487 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 13 Nov 2023 16:25:45 +0330 Subject: [PATCH 5/7] Update Connection.php --- src/Connection.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 419ee404a..dfbff644f 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -196,12 +196,9 @@ protected function createConnection(string $dsn, array $config, array $options): /** * Check the connection to the defined MongoDB host * - * @return void - * * @throws ConnectionException if connection to the server fails (for reasons other than authentication). * @throws AuthenticationException if authentication is needed and fails. * @throws RuntimeException if a server matching the read preference could not be found. - * @throws \MongoDB\Driver\Exception\InvalidArgumentException on argument parsing errors. */ public function ping(): void { From 2d81d6803272f3933d6b611921cdb4c613779c9d Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 13 Nov 2023 16:56:40 +0330 Subject: [PATCH 6/7] PHPDoc updated; --- src/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index dfbff644f..a859bfa63 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -194,7 +194,7 @@ protected function createConnection(string $dsn, array $config, array $options): } /** - * Check the connection to the defined MongoDB host + * Check the connection to the MongoDB server * * @throws ConnectionException if connection to the server fails (for reasons other than authentication). * @throws AuthenticationException if authentication is needed and fails. From 1311b0df0f6ade50da7a09e6d23648be730d89c4 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 13 Nov 2023 16:57:07 +0330 Subject: [PATCH 7/7] Default dsn updated; --- tests/ConnectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index d6e601732..262c4cafc 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -239,7 +239,7 @@ public function testPingMethod() $config = [ 'name' => 'mongodb', 'driver' => 'mongodb', - 'dsn' => env('MONGODB_URI', 'mongodb://mongodb/'), + 'dsn' => env('MONGODB_URI', 'mongodb://127.0.0.1/'), 'database' => 'unittest', 'options' => [ 'connectTimeoutMS' => 100,