Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for invalid configuration #2975

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.

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

## [4.3.0] - 2024-04-26

Expand Down
14 changes: 11 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public function disconnect()

/**
* Determine if the given configuration array has a dsn string.
*
* @deprecated
*/
protected function hasDsnString(array $config): bool
{
Expand Down Expand Up @@ -261,9 +263,15 @@ protected function getHostDsn(array $config): string
*/
protected function getDsn(array $config): string
{
return $this->hasDsnString($config)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function hasDsnString could be removed or deprecated (since it is protected).

? $this->getDsnString($config)
: $this->getHostDsn($config);
if (! empty($config['dsn'])) {
return $this->getDsnString($config);
}

if (! empty($config['host'])) {
return $this->getHostDsn($config);
}

throw new InvalidArgumentException('MongoDB connection configuration requires "dsn" or "host" key.');
}

/** @inheritdoc */
Expand Down
8 changes: 8 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public function testConnectionWithoutConfiguredDatabase(): void
new Connection(['dsn' => 'mongodb://some-host']);
}

public function testConnectionWithoutConfiguredDsnOrHost(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('MongoDB connection configuration requires "dsn" or "host" key.');

new Connection(['database' => 'hello']);
}

public function testCollection()
{
$collection = DB::connection('mongodb')->getCollection('unittest');
Expand Down
Loading