Skip to content

Commit

Permalink
feat: catch and report exception during Open-Api file validation (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodmain committed Oct 26, 2023
1 parent e53c27c commit 654e3f3
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 21 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"require-dev": {
"orchestra/testbench": "^6.25",
"php-coveralls/php-coveralls": "^2.5"
"php-coveralls/php-coveralls": "^2.5",
"php-mock/php-mock-phpunit": "^2.7"
},
"autoload": {
"psr-4": {
Expand Down
212 changes: 210 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,19 +697,19 @@ public function getDocFileContent()
foreach ($additionalDocs as $filePath) {
$fullFilePath = base_path($filePath);

if (!file_exists($fullFilePath)) {
throw new DocFileNotExistsException($fullFilePath);
}
try {
if (!file_exists($fullFilePath)) {
throw new DocFileNotExistsException($fullFilePath);
}

$fileContent = json_decode(file_get_contents($fullFilePath), true);
$fileContent = json_decode(file_get_contents($fullFilePath), true);

if (empty($fileContent)) {
throw new EmptyDocFileException($fullFilePath);
}
if (empty($fileContent)) {
throw new EmptyDocFileException($fullFilePath);
}

try {
$this->validateSpec($fileContent);
} catch (InvalidSwaggerSpecException $exception) {
} catch (DocFileNotExistsException|EmptyDocFileException|InvalidSwaggerSpecException $exception) {
report($exception);

continue;
Expand Down
32 changes: 23 additions & 9 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace RonasIT\Support\Tests;

use Illuminate\Http\Response;
use RonasIT\Support\AutoDoc\Exceptions\DocFileNotExistsException;
use RonasIT\Support\AutoDoc\Exceptions\EmptyDocFileException;
use phpmock\phpunit\PHPMock;
use RonasIT\Support\Tests\Support\Traits\MockTrait;

class AutoDocControllerTest extends TestCase
{
use MockTrait, PHPMock;

protected $documentation;
protected $localDriverFilePath;

Expand Down Expand Up @@ -52,32 +54,44 @@ public function testGetJSONDocumentationWithAdditionalPaths()
$this->assertEqualsJsonFixture('tmp_data_with_additional_paths', $response->json());
}

public function getJSONDocumentationDoesntExist()
public function testGetJSONDocumentationDoesntExist()
{
$mock = $this->getFunctionMock('RonasIT\Support\AutoDoc\Services', 'report');
$mock->expects($this->once());

config([
'auto-doc.additional_paths' => ['invalid_path/non_existent_file.json']
]);

$this->expectException(DocFileNotExistsException::class);
$response = $this->json('get', '/auto-doc/documentation');

$response->assertStatus(Response::HTTP_OK);

$this->json('get', '/auto-doc/documentation');
$response->assertJson($this->documentation);
}

public function getJSONDocumentationIsEmpty()
public function testGetJSONDocumentationIsEmpty()
{
$mock = $this->getFunctionMock('RonasIT\Support\AutoDoc\Services', 'report');
$mock->expects($this->once());

config([
'auto-doc.additional_paths' => ['tests/fixtures/AutoDocControllerTest/documentation__non_json.txt']
]);

$this->expectException(EmptyDocFileException::class);
$response = $this->json('get', '/auto-doc/documentation');

$this->json('get', '/auto-doc/documentation');
$response->assertStatus(Response::HTTP_OK);

$response->assertJson($this->documentation);
}

public function testGetJSONDocumentationInvalidAdditionalDoc()
{
config([
'auto-doc.additional_paths' => ['tests/fixtures/AutoDocControllerTest/documentation__invalid_format__missing_field__paths.json']
'auto-doc.additional_paths' => [
'tests/fixtures/AutoDocControllerTest/documentation__invalid_format__missing_field__paths.json'
]
]);

$response = $this->json('get', '/auto-doc/documentation');
Expand Down

0 comments on commit 654e3f3

Please sign in to comment.