Skip to content

Commit

Permalink
feat: add exception for invalid documentation_viewer configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Lapkovsky committed Nov 6, 2023
1 parent 5c824da commit ff0d072
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Exceptions/UnsupportedDocumentationViewerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\AutoDoc\Exceptions;

use Exception;

class UnsupportedDocumentationViewerException extends Exception
{
public function __construct(string $invalidViewer)
{
parent::__construct("The documentation viewer '{$invalidViewer}' does not exists. Please check that the 'documentation_viewer' key of your auto-doc.php config has one of valid values.");
}
}
12 changes: 12 additions & 0 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use RonasIT\Support\AutoDoc\Exceptions\LegacyConfigException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerSpecException;
use RonasIT\Support\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
use RonasIT\Support\AutoDoc\Exceptions\UnsupportedDocumentationViewerException;
use RonasIT\Support\AutoDoc\Exceptions\WrongSecurityConfigException;
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;
use RonasIT\Support\AutoDoc\Traits\GetDependenciesTrait;
Expand Down Expand Up @@ -56,6 +57,12 @@ class SwaggerService
'int' => 'integer'
];

protected $documentationViewers = [
'swagger',
'elements',
'rapidoc'
];

public function __construct(Container $container)
{
$this->openAPIValidator = app(SwaggerSpecValidator::class);
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ff0d072

Please sign in to comment.