Skip to content

Commit

Permalink
Merge pull request #113 from RonasIT/93-add-exception-for-invalid-doc…
Browse files Browse the repository at this point in the history
…umentation-viewer

feat: add exception for invalid documentation_viewer configuration.
  • Loading branch information
DenTray committed Nov 7, 2023
2 parents 5e383d5 + 888439a commit ccda21b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Exceptions/UnsupportedDocumentationViewerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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."
);
}
}
7 changes: 7 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 @@ -94,6 +95,12 @@ protected function initConfig()
if (version_compare($packageConfigs['config_version'], $version, '>')) {
throw new LegacyConfigException();
}

$documentationViewer = (string) Arr::get($this->config, 'documentation_viewer');

if (!view()->exists("auto-doc::documentation-{$documentationViewer}")) {
throw new UnsupportedDocumentationViewerException($documentationViewer);
}
}

protected function setDriver()
Expand Down
27 changes: 27 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,30 @@ 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 ccda21b

Please sign in to comment.