Skip to content

Commit 8c832c4

Browse files
committed
Improve error message for invalid configuration
1 parent 390c9ec commit 8c832c4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Connection.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,15 @@ protected function getHostDsn(array $config): string
261261
*/
262262
protected function getDsn(array $config): string
263263
{
264-
return $this->hasDsnString($config)
265-
? $this->getDsnString($config)
266-
: $this->getHostDsn($config);
264+
if (! empty($config['dsn'])) {
265+
return $this->getDsnString($config);
266+
}
267+
268+
if (! empty($config['host'])) {
269+
return $this->getHostDsn($config);
270+
}
271+
272+
throw new InvalidArgumentException('MongoDB connection configuration requires "dsn" or "host" key.');
267273
}
268274

269275
/** @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)