From ff0d072416efbf7ca953bae7215d8359bde58954 Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Mon, 6 Nov 2023 11:12:11 +0400 Subject: [PATCH] feat: add exception for invalid documentation_viewer configuration. --- ...nsupportedDocumentationViewerException.php | 13 ++++++++++++ src/Services/SwaggerService.php | 12 +++++++++++ tests/SwaggerServiceTest.php | 21 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/Exceptions/UnsupportedDocumentationViewerException.php diff --git a/src/Exceptions/UnsupportedDocumentationViewerException.php b/src/Exceptions/UnsupportedDocumentationViewerException.php new file mode 100644 index 0000000..169cf4f --- /dev/null +++ b/src/Exceptions/UnsupportedDocumentationViewerException.php @@ -0,0 +1,13 @@ + 'integer' ]; + protected $documentationViewers = [ + 'swagger', + 'elements', + 'rapidoc' + ]; + public function __construct(Container $container) { $this->openAPIValidator = app(SwaggerSpecValidator::class); @@ -94,6 +101,11 @@ protected function initConfig() if (version_compare($packageConfigs['config_version'], $version, '>')) { throw new LegacyConfigException(); } + + $documentationViewer = (string)Arr::get($this->config, 'documentation_viewer'); + if (!in_array($documentationViewer, $this->documentationViewers)) { + throw new UnsupportedDocumentationViewerException($documentationViewer); + } } protected function setDriver() diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 0f8cddf..7d3bcee 100755 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -21,6 +21,7 @@ use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingPathPlaceholderException; use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingRefFileException; use RonasIT\Support\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException; +use RonasIT\Support\AutoDoc\Exceptions\UnsupportedDocumentationViewerException; use RonasIT\Support\AutoDoc\Exceptions\WrongSecurityConfigException; use RonasIT\Support\AutoDoc\Services\SwaggerService; use RonasIT\Support\Tests\Support\Mock\TestNotificationSetting; @@ -649,4 +650,24 @@ public function testAddDataWithoutBoundContract() $service->addData($request, $response); } + + public function testSetInvalidDocumentationViewer() + { + config(['auto-doc.documentation_viewer' => 'invalid']); + + $this->expectException(UnsupportedDocumentationViewerException::class); + $this->expectExceptionMessage("The documentation viewer 'invalid' does not exists. Please check that the 'documentation_viewer' key of your auto-doc.php config has one of valid values."); + + app(SwaggerService::class); + } + + public function testSetNullableDocumentationViewer() + { + config(['auto-doc.documentation_viewer' => null]); + + $this->expectException(UnsupportedDocumentationViewerException::class); + $this->expectExceptionMessage("The documentation viewer '' does not exists. Please check that the 'documentation_viewer' key of your auto-doc.php config has one of valid values."); + + app(SwaggerService::class); + } }