Skip to content

Commit 7b8f0a1

Browse files
authored
Improve error message for invalid configuration (#2975)
* Improve error message for invalid configuration * Deprecate Connection::hasDsnString
1 parent 7545177 commit 7b8f0a1

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
* Fix memory leak when filling nested fields using dot notation by @GromNaN in [#2962](https://github.com/mongodb/laravel-mongodb/pull/2962)
77
* Fix PHP error when accessing the connection after disconnect by @SanderMuller in [#2967](https://github.com/mongodb/laravel-mongodb/pull/2967)
8+
* Improve error message for invalid configuration by @GromNaN in [#2975](https://github.com/mongodb/laravel-mongodb/pull/2975)
89

910
## [4.3.0] - 2024-04-26
1011

src/Connection.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ public function disconnect()
213213

214214
/**
215215
* Determine if the given configuration array has a dsn string.
216+
*
217+
* @deprecated
216218
*/
217219
protected function hasDsnString(array $config): bool
218220
{
@@ -261,9 +263,15 @@ protected function getHostDsn(array $config): string
261263
*/
262264
protected function getDsn(array $config): string
263265
{
264-
return $this->hasDsnString($config)
265-
? $this->getDsnString($config)
266-
: $this->getHostDsn($config);
266+
if (! empty($config['dsn'])) {
267+
return $this->getDsnString($config);
268+
}
269+
270+
if (! empty($config['host'])) {
271+
return $this->getHostDsn($config);
272+
}
273+
274+
throw new InvalidArgumentException('MongoDB connection configuration requires "dsn" or "host" key.');
267275
}
268276

269277
/** @inheritdoc */

tests/ConnectionTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ public function testConnectionWithoutConfiguredDatabase(): void
204204
new Connection(['dsn' => 'mongodb://some-host']);
205205
}
206206

207+
public function testConnectionWithoutConfiguredDsnOrHost(): void
208+
{
209+
$this->expectException(InvalidArgumentException::class);
210+
$this->expectExceptionMessage('MongoDB connection configuration requires "dsn" or "host" key.');
211+
212+
new Connection(['database' => 'hello']);
213+
}
214+
207215
public function testCollection()
208216
{
209217
$collection = DB::connection('mongodb')->getCollection('unittest');

0 commit comments

Comments
 (0)