diff --git a/.github/labeler.yml b/.github/labeler.yml new file mode 100644 index 000000000..d0a7bb123 --- /dev/null +++ b/.github/labeler.yml @@ -0,0 +1,7 @@ +# https://github.com/actions/labeler +docs: + - changed-files: + - any-glob-to-any-file: 'docs/**' +github: + - changed-files: + - any-glob-to-any-file: '.github/**' diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml new file mode 100644 index 000000000..52474c6a6 --- /dev/null +++ b/.github/workflows/labeler.yml @@ -0,0 +1,12 @@ +name: "Pull Request Labeler" +on: + - pull_request_target + +jobs: + labeler: + permissions: + contents: read + pull-requests: write + runs-on: ubuntu-latest + steps: + - uses: actions/labeler@v5 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2400d82cc..ad748ef07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,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 diff --git a/src/Connection.php b/src/Connection.php index aea6238ea..2ce5324ee 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -215,6 +215,8 @@ public function disconnect() /** * Determine if the given configuration array has a dsn string. + * + * @deprecated */ protected function hasDsnString(array $config): bool { @@ -263,9 +265,15 @@ protected function getHostDsn(array $config): string */ protected function getDsn(array $config): string { - return $this->hasDsnString($config) - ? $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 */ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 383e47fe5..83097973b 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -206,6 +206,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');